Note that this is addressing the issue of
Unlike the case of a maple worksheet (which you can just download, then load into maple from the File menu), sometimes you want to read the contents of a web-based file into your maple session. As an example, we will assume that we want to read the data from the web address
-
Here are some ways you can accomplish this.
- Cut and Paste
- You can open the page with the data in your web browser, then use highlight the relevant stuff and copy it to the clipboard, then paste into a worksheet. This is easy, but error-prone, and tedious if there is a lot of data.
- Download file
- You can right-click on the link to the file and select
Save Link As... or Save Target As... to save this to a
file on your local computer. You should take note of the name and location
of the file you saved. For example, if you saved the above link to your
desktop, the full path might be something like
D:\Users\netid\Desktop\09-10-wiggly.txt.
Now, in maple, issue a command like
read("D:/Users/netid/Desktop/09-10-wiggly.txt");
This will execute the commands in the 09-10-wiggly.txt file.Note that when typing the name of the file on a windows system, you will need to either write a forward slash (/) instead of the backslashes (\) in the name, or double the backslashes, as in D:\\Users\\netid\\Desktop\\09-10-wiggly.txt.
- Read directly from the web
-
You can read the file directly into maple using the HTTP library.
Let's assume the variable URL contains the address of the web
page you want to load as a string. That is,
URL:="http://www.math.stonybrook.edu/~scott/mat331.fall19/daily/extras/09-10-wiggly.txt";
Then the commandwith(HTTP): status,webfile,headers:=HTTP(URL): Code(status);
will read the contents of the file into a variable webfile as a string.But often, you want to evaluate the contents. Here's a way
with(HTTP): status,webfile,headers:=Get(URL): Code(status); n:=0: while (n < length(webfile)) do parse(webfile,statement,lastread='n', offset=n); od:
Here is a Maple procedure which implements this in a slightly more compact way (which you can download here, or paste from this page).ExecuteFromWeb:=proc(URL::string, {printfile::truefalse:=false}) local n,m, status, webfile, headers; status,webfile,headers:=HTTP[Get](URL): if ( HTTP[Code](status) <> "OK") then error(HTTP[Code](status),URL); fi; # now interpret the maple on the web page n:=0: while (n < length(webfile)) do m:=n; parse(webfile,statement,lastread='n', offset=n); if (printfile) then printf("%s",webfile[m+1..n]); fi; od: end:
Note that the above routine takes an optional argument printfile, which, if included, prints out the statements that Maple is parsing. - Just get the text of a webpage
-
Note that the above routine reads a web page and interprets it as
maple code. Sometimes, you just want to get the text of the page. The
maple HTTP[Get] command does this. For example,
Poem:=HTTP[Get]("http://www.math.stonybrook.edu/~scott/mat331.fall19/daily/extras/Whitman.txt")[2];
will read the text in the file Whitman.txt into the string Poem. Note that the second return value is the web page, so we need the [2] at the end. If there is an error (for example, if the URL isn't valid), things get messy. If you prefer, you can wrap this in a little procedure that checks the return code, as inGetWebFile:=proc(URL::string) local status, webfile, headers; status,webfile,headers:=HTTP[Get](URL): if ( HTTP[Code](status) <> "OK") then error(HTTP[Code](status),URL); else return(webfile); fi; end: