MAT 331: Reading Maple from the Web

It is not uncommon to need to access some maple code from the class web page. For example, the data for the first project is stored on the class web page.

Note that this is addressing the issue of executing maple code that is stored on the web. This is related to, but different from, the question of loading text data from a web page. That is dealt with later on this page.

Unlike the case of a maple worksheet (for which you can just use Open URL... from the menu), opening a file from the web is a little trickier. 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\electron.txt.

Now, in maple, issue a command like
This will execute the commands in the electron.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\\electron.txt.

Read directly from the web
You can read the file directly into maple using the HTTP library.
status,webfile,headers:=Get(URL): Code(status);

while (n < length(webfile)) do
 parse(webfile,statement,lastread='n', offset=n);
Below is a maple procedure which implements this in a slightly more compact way (which you can download here, or paste from this page.
ReadFromWeb:=proc(URL::string, {printfile::truefalse:=false})
 local n,m, status, webfile, headers;
 if ( HTTP[Code](status) <> "OK") then
 # now interpret the maple on the web page
 while (n < length(webfile)) do
   parse(webfile,statement,lastread='n', offset=n);
   if (printfile) then printf("%s",webfile[m+1..n]); fi;
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,
will read the text in the file subscrypt.txt into the string Crypto. 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 in
 local status, webfile, headers;
 if ( HTTP[Code](status) <> "OK") then error(HTTP[Code](status),URL);
 else return(webfile);