Pygr.Data in A Web Browser!
I’ve been hacking a bit with Silverlight, Microsoft’s environment for running dynamic languages like Python and Ruby directly within a web browser like Firefox or IE. It seems to work quite well, and it’s easy to get Python code up and running in Silverlight. Indeed, it’s been surprisingly easy to get quite significant chunks of Python running in Silverlight — specifically, pygr, including large portions of code originally written for Pyrex (a mixed C / Python language environment). First I’ll describe my impressions of Silverlight and its implications for Python, then what I’ve accomplished with it.
I think Silverlight potentially has a lot of importance for Python for several reasons. Its basic function is fairly amazing: it lets you run Python inside your web browser with complete control over the web page contents and interactivity. Those of us who feel that the web browser is the logical place for many kinds of interactive documents, visualization and rapid prototyping, but want to use a real language (instead of Javascript!), finally have a solution. This is exciting! It is thrilling to be creating interactive web pages whose interactivity is all done by Python code — forget all that JavaScript!
But Pythonistas should also pay attention to Silverlight as a potential threat: this is Microsoft’s attempt to hijack Python’s many virtues and put them to work for Microsoft. For example, Silverlight runs IronPython, and as such lacks most of the Python standard libraries that Pythonistas know and rely on. In their place it gives you access to .NET. Similarly, it redirects you away from open standards like SVG and into Microsoft’s WPF (Windows Presentation Framework?). The concern here is that huge numbers of .Net and web developers will be taught a Python that is Not Python, i.e. they will be taught to write code that can’t run in standard Python, or indeed anywhere outside of Microsoft’s IronPython. The good news, I think, is that Python is too dynamic to box in this way; it is very easy to port Python standard libraries to run in Silverlight. I think Pythonistas should push very hard to provide as complete as possible a standard library for IronPython and Silverlight, so that developers in those environments are not forced over to the Dark Side.
Another interesting possibility is that Silverlight is the “restricted execution environment” Python has always lacked. Python code running in Silverlight supposedly has no access to the file system or system calls, and no way to read or alter user files. Since it runs in the web browser, and executes code sent by potentially malicious websites, logically it would have to be secure in this sense, or it will be worse than useless for Microsoft’s purposes. Taking a cynical view, if Silverlight takes off, hackers are going to be attacking it from every angle, and Microsoft will be forced to patch up the holes. So over time, Silverlight could evolve into a safe “sandbox” where you can run code that you want to confine to very limited privileges.
OK, so what have I done with Silverlight?
- I ported a fair number of missing standard library modules to get my own code working. The most significant are pickle, types, xmllib, xmlrpclib, and urllib (only some functions; Silverlight already provides http connection objects). This was amazingly easy.
- I mainly did this to create an XML-RPC connection back to the server. Silverlight 1.1 alpha refresh is restricted to only access the same server that the code was retrieved from. I’ve written a combined HTTP/XML-RPC server (in Python, naturally, it’s dead easy) so that my Python code in Silverlight can communicate with the server via XML-RPC. This is incredibly easy, just as you’re used to doing in Python:
import xmlrpclib
server = xmlrpclib.ServerProxy('http://localhost:8000')
result = server.some_method(*args)
I will make all this available so Pythonistas can start playing with this by quickly launching the server and simply pointing their web browser at it. - I’ve got almost all of Pygr, including Pygr.Data, running in Silverlight. The one module that required some porting was cnestedlist, which was written in a combination of C and Python. I had to convert some things to pure Python and get rid of other things that aren’t relevant in Silverlight (e.g. file IO).
- I have pygr.Data working great in Silverlight. That is, the server is running an XML-RPC pygr.Data server, and in Silverlight I can just import pygr.Data as usual and immediately do everything I would usually do i.e. get both sequence databases / sequences and alignment databases (NLMSA) and work with them exactly as if I were in my usual Python interpreter. This is a little mind blowing. I can’t wait to demo this with access to a 28 vertebrate genome alignment in Firefox!
- I’ve been learning all this in part via Michael Foord’s very helpful Silverlight / IronPython pages. I used his simple interactive console code to try all this out by hand. But I hit a lot of crashes that killed Firefox. At first I just thought Silverlight 1.1 alpha is really buggy, but finally I realized that the problem is not Silverlight itself, but apparently the interactive console. When I just run my code directly in Silverlight (not through his interactive console) the crashing goes away. I wrote my own much simpler interactive console to eliminate all that crashing.
- Things I haven’t figured out: proper traceback printing; Silverlight’s odd handling of hierarchical module names. It doesn’t seem to be able to handle them, so to start with I just put the pygr modules in a single directory and access them as import seqdb instead of from pygr import seqdb. I had to kluge pickle.unpickling to follow this convention too.
I will post a tar of the source code tomorrow, and update this page to link to it. But now to bed.