Monday, December 12, 2016

Reeborg's answer to pip

If you are not familiar with Reeborg's World, then this post is likely not of interest to you and I apologize for the title.

As Python programmers know, pip is extremely convenient to install Python modules.  However, when creating a website like Reeborg's World which does not store any user code on the server and run everything in the client (browser), the idea of installing a Python module may seem to be totally nonsensical: how can one install a Python module in a web browser?

Yet, since Reeborg's World aims to teach not only basic Python programming but also Python's concepts like creating and using a library and third-party modules, it only made to me that I should attempt to reproduce the basic idea of installing third-party modules.  After all, it already allow users to write their own functions in a library and import them; so why not finding a way to "install" third party modules from the web.

Before I explain how to do so, let me recap what can be done with Reeborg's World (just in case you are not familiar with it and did not get put off by the opening sentence of this blog post).

The user (student) enters Python code [1] in the editor and runs it, making Reeborg accomplish various tasks.



The student can also enter functions in the library and import them as usual. Reeborg's World uses Brython as its Python interpreter.


World creators (educators) can create their own worlds, making use potentially of six different editors:

  • The Python code editor: one can save some program (e.g. a stub) together with a world as a starting point for the students.
  • The library - which can be prepopulated by the educator
  • The pre- and post- editors. When a program is run, the code found in the "pre-" editor is concatenated with the code found in the main editor followed by the code found in the "post-" editor.  This allow the world creator to set up some particular condition, going beyond what is possible to do with the graphical world editor.
  • The "description" editor. The world creator can add information about the world using html; this can include images.  Some information (position of robot, final position to reach, etc., is extracted automatically from the world file content and added to the information.
  • The "onload" editor: this allow the world creator to add some (Javascript) code that will be automatically executed once when the world is loaded. One can use this to load new images to use for the robot, define new objects types (with pictures) that can be used, etc.  The images can be loaded from anywhere on the web.
In theory, an educator could design worlds that would include either in the "post-" editor or in the library some code to be used by the students.  However, this would have to be done every time a world is created, which can be quite tedious.  With the new functionality, one can load a script from anywhere on the web and have it made available as the content of a module named "extra".




Instead of calling the new function "install_extra()", I was toying with the idea of calling it "pip_install()" ... but I thought this might be 1) too cheeky and 2) potentially confusing for beginners when they moved to a "real" Python environment and had to use pip from the command line, and not from within an editor.  However, I am  not totally convinced that "extra" is the best name for this library nor that "install_extra()" is the best name for the new function.  If you have any suggestion or comments, feel free to do so.

For those that are curious and would like to try it, click on this link http://reeborg.ca/reeborg2.html?lang=en&mode=python&url=http://pastebin.com/raw/t9Vvj7nG&name=Install%20extra and then make sure to click on the button to have the content of the editor replace by that included with this sample world.
Note that this link is for a development version and may not work in the future.

This feature has just been implemented and may very well be buggy. To load modules, I make use of cross-domain http requests and a third-party server (http://cors-anywhere.herokuapp.com). When I first attempted to write code on pastebin, and then edit it, I found that the original version was cached ... which was not very helpful as I did not realize it at first and was wondering how come the changes I made to the code were not reflected in what I saw.  Coders beware...

Suggestions and comments, as usual, are most welcome.

[1] In addition to Python, Javascript is also supported. Instead of a traditional editor, one can also use Blockly or a Python REPL to program the robot.