Online Python exercises

Friday 9 September 2011

I’ve been thinking about online Python learners. There have been some cool examples of online code exercises, like Nathan’s Javascript Lessons. These are great because they require absolutely no setup, and can run right on the web page that describes the concepts involved.

But of course, it’s easy to run Javascript in a browser. What are the options for doing something similar for Python?

CodingBat provides exercises for Java and Python. Your code is actually run on the server. Generally, this is risky, because Python’s dynamic nature makes it notoriously difficult to limit its powers. CodingBat does this aggressively, and manages it only because it limits the code it will run to very simple functions. For example, you cannot import anything.

Another server-side solution is the NCSS Challenge, which uses an elaborate sandboxing technique on the server to run arbitrary Python code. The sandbox is explained in this PyCon AU video by Tim Dawborn. It seems prohibitively complex, but makes it possible to run sophisticated Python code from the browser.

There are other server-side online Python execution pages:

Server-side execution solve the problem of executing Python, but introduces the new problem of keeping the server safe. Can we instead execute Python in the browser? There have been some interesting implementations of this also:

  • Try Python is an in-browser Python implementation using IronPython running in Silverlight. This limits its platform viability, somewhat blunting the benefits of in-browser execution. Also, most of the standard library is missing, and OS services are necessarily limited.
  • Gumby is astounding: Tim Dawborn (again!) and Ben Taylor took CPython, compiled it using llvm, then ran it through an llvm-to-Javascript compiler, to produce a full implementation of CPython that runs natively in the browser. This is one of those dancing bear moments where it isn’t that it does it well, it’s amazing that it does it at all. The problems are the 790Kb Javascript download, and the limited environmental support.
  • Skulpt implements Python in Javascript, from scratch, so it’s really its own Python implementation. Alone among these tools, it experiments with Python access to the browser environment, but still suffers from the in-browser problem of how to provide OS services. Also, because it’s implementing from the ground up, it’s incomplete.

Reviewing all these possibilities, none are perfect, and some are far from it. And I started to wonder, are online exercises worth all this? After all, the point of learning a language is to eventually write your own code, and that won’t happen in a sandbox in a browser. The student will have to install a real Python environment at some point, so why not start with that, and get a better learning environment right off the bat?

Are there other possibilities? Is it important? What about an environment on the student’s computer that can communicate result back to the online environment automatically? Or an online environment that can use the student’s local resources so the server doesn’t need protection?

Comments

[gravatar]
eric 1:23 PM on 9 Sep 2011

Have you considered running jython in Java Applet?

[gravatar]
Alejandro 2:53 PM on 9 Sep 2011

Python anywhere is a good alternative for running Python on the server side: http://www.pythonanywhere.com

[gravatar]
André Roerge 3:52 PM on 9 Sep 2011

You write: Or an online environment that can use the student's local resources so the server doesn't need protection?. Crunchy uses the student's local resources. Titus Brown has just started writing an online tutorial that uses Crunchy. With Crunchy, you can have unitest or doctest-based exercises.

[gravatar]
Etienne 6:13 PM on 9 Sep 2011

There's also http://pythonfiddle.com/ (inspired by http://jsfiddle.net/).

[gravatar]
Ben Poole 3:23 AM on 10 Sep 2011

My feeling is that these web-based tools are excellent learning resources if someone simply wants to try a language out. As you point out, sooner or later if one is serious, an install of some kind is going to be required.

For example, I played around with a mirror of why's online resource for Ruby, and that gave me enough to want to use it properly on my machine. I don't think I could have asked for more.

[gravatar]
Ed Davies 6:18 AM on 10 Sep 2011

Perhaps you could run CPython under Linux running on a PC emulator written in JavaScript, http://bellard.org/jslinux/tech.html , or maybe that would be silly.

[gravatar]
Nick Coghlan 7:48 PM on 10 Sep 2011

For the NCSS crew, the appeal of in-browser execution is that their educational exercises need to work with the government supplied laptops offered to high schools, and those are a locked down, Windows-only system (apparently our government consider it more appropriate to treat students as nascent digital criminals rather than as future adult citizens and potential IT professionals that are better off learning to control their computers rather than getting used to being controlled *by* them).

Victor Stinner's PySandbox is another interesting tool for safer server side execution, but doesn't protect against various things like memory exhaustion. You need OS level tools for that, hence the elaborate NCSS approach.

[gravatar]
Ned Batchelder 7:38 AM on 11 Sep 2011

Hmm, there seem to be more of these than I knew. Also PyCloud (http://www.jitouch.com/pycloud/)

[gravatar]
André Roberge 7:46 AM on 11 Sep 2011

There is also the interactive version of How to Think Like a Computer Scientist.

[gravatar]
Andrew Fort 10:38 AM on 14 Sep 2011

If you're building a tutorial, AppEngine could be an option (with the usual "(most of) standard lib only" proviso). That environment is already very heavily sand boxed (as they have a relatively homogenous compute environment, this was a requirement from day 1).

There's also a shell example already, with source:

http://shell.appspot.com/

[gravatar]
Mikle 3:22 AM on 16 Sep 2011

A friend of mine opened:
http://learnpython.org/

It's pretty fun and everyone can contribute.

Add a comment:

Ignore this:
Leave this empty:
Name is required. Either email or web are required. Email won't be displayed and I won't spam you. Your web site won't be indexed by search engines.
Don't put anything here:
Leave this empty:
URLs auto-link and some tags are allowed: <a><b><i><p><br><pre>.