Friday, October 09, 2009

Try Python: Interactive Python Tutorial with Silverlight (and rst2xaml)

Silverlight is the Microsoft browser plugin runs on the Mac and Windows and in Safari, IE and Firefox browsers. As an added bonus it also supports IronPython, making it an ideal tool to build an interactive Python tutorial with.

Try Python is an open source project that takes the official Python tutorial and puts it alongside an interactive Python interpreter running in the browser. All of the code examples have a button to execute them in the interpreter, or you can just follow the tutorial trying out the examples yourself.
The Mono team are working on an implementation of Silverlight for Linux called Moonlight. The version capable of running IronPython code is Moonlight 2, but unfortunately the preview release can't yet run Try Python. Hopefully that will be fixed in the final release.

Try Python has the following features:
  • Auto-resizes with the browser
  • Navigation bar through the tutorial pages at the top and bottom
  • Individual pages are bookmarkable and the URL fragment auto-updates when you change page
  • Mouse scroll wheel supported over the tutorial and console scroll viewers
  • Control-C interrupts the running code with a keyboard interrupt
  • raw_input and input work in the interpreter
  • Basic auto-indent and auto-dedent in the console
  • Console history, including multiline history
  • Syntax highlighting in the console
  • reset command to clear the console
  • Assign to sys.ps1 and sys.ps2 from the console
There are various things still left to do, the project issues page tracks the ones I'm aware of. In particular, I've almost completed an implementation of the file type in pure Python, using browser local storage to actually store the files. This will allow the parts of the tutorial that teach file access to work in the browser. As the backend can be swapped out (for example to use a dictionary based storage) it could be useful to anyone wanting to unit test code that does file I/O (transparently swap out real file access for your tests). I'll blog about this once it is complete.

The Python tutorial is formatted using ReStructured Text, the popular text markup format implemented in Python. To render a nicely formatted version in Silverlight I've implemented a XAML writer for ReStructured Text and a XAML formatter for Pygments which does nice syntax highlighting of Python source code. XAML is the XML dialect for describing user interfaces in the WPF (Windows Presentation Foundation) and Silverlight user interfaces.

The XAML writer / formatter are in the rst2xaml project:
Try Python has already spawned at least one imitator. Try Powershell is a tutorial application for Powershell. It runs on the desktop with WPF and is inspiring me to create a desktop version of Try Python. rst2xaml can already output XAML for WPF and it could share a lot of code (but not all the code unfortunately) with the browser version, I just need to find the time...

No comments:

Post a Comment

Note: only a member of this blog may post a comment.