By Jake Edge
August 26, 2009
Maintaining a complex web application that uses a lot of Javascript for
client-side, "AJAX"-style interactivity is rather difficult. The
clumsiness of the Javascript language itself, as well as the various tricks
needed to make an application work consistently across multiple browsers,
all of which must be wrapped up inside HTML, makes for a jumble of issues
for the application developer. Pyjamas is
meant to ease that development, by allowing client-side applications to be
written in Python, then translating that code to Javascript for use by the
browser.
Pyjamas is a port of
the Google Web Toolkit (GWT), which
pioneered the technique, but GWT uses Java instead of Python.
For developers who prefer
Python, pyjamas
provides an environment that almost completely insulates them from the
code that actually runs in the user's browser. As described on the home page:
You can write web applications in python - a readable programming language
- instead of in HTML and Javascript, both of which become quickly
unreadable for even medium-sized applications. Your application's design
can benefit from encapsulating high level concepts into classes and modules
(instead of trying to fit as much HTML as you can stand into one page); you
can reuse - and import - classes and modules.
Also, the AJAX library takes care of all the browser interoperability
issues on your behalf, leaving you free to focus on application development
instead of learning all the "usual" browser incompatibilities.
Essentially, a developer uses the pyjamas-supplied libraries that provide
user interface widgets and Document Object Model (DOM) control from Python.
This code is then combined with a library that implements various
Javascript language features in Python—an impedance-matching layer between the
two languages called pyjslib—and turned into Javascript and
HTML that can be loaded into a browser. The pyjs program
"translates Python code to Javascript by walking the Python abstract
syntax tree and generating Javascript".
But, using pyjamas is not at all like "normal" web programming. As the
web site and FAQ are quick to point
out, pyjamas is not just another AJAX (Asynchronous Javascript and XML)
framework, it is more akin to writing a desktop program that is translated
into web application. In fact, Pyjamas-Desktop allows the same
code that is developed for the web application to be run, unmodified, on the
desktop. The same Python source that gets fed into the translator can,
instead, be run, and, more importantly, debugged, on the developer's desktop.
The tool is best suited to writing one-page web applications that rely
entirely on AJAX techniques to do their job—things like Gmail,
Mozilla's Bespin, and many others. Each page load in a pyjamas application
requires loading all of the Javascript that makes up the application. That
includes code generated from the application as well as pyjamas libraries,
so, ideally, that would only be done once. That is quite a contrast from
the traditional, multi-page-oriented web application, but is certainly
in keeping with the direction of web interaction.
The key to understanding pyjamas is to note that, unlike AJAX frameworks,
it is not meant to add a bit of interactivity, or some desktop-like
features, to an existing web application. Instead, the entire application
is written in Python, likely debugged on the desktop, and then turned into
a big blob (or, really, blobs) of Javascript for deployment. The
application code will look very familiar to Python GUI developers. For
example, the
canonical "Hello
World" program—which does a bit more than Kernighan and
Ritchie's original C program—looks like:
import pyjd # this is dummy in pyjs.
from pyjamas.ui.RootPanel import RootPanel
from pyjamas.ui.Button import Button
from pyjamas.ui.HTML import HTML
from pyjamas.ui.Label import Label
from pyjamas import Window
import pygwt
def greet(fred):
print "greet button"
Window.alert("Hello, AJAX!")
if __name__ == '__main__':
pyjd.setup("public/Hello.html?fred=foo#me")
b = Button("Click me", greet, StyleName='teststyle')
h = HTML("<b>Hello World</b> (html)", StyleName='teststyle')
l = Label("Hello World (label)", StyleName='teststyle')
base = HTML("Hello from %s" % pygwt.getModuleBaseURL(),
StyleName='teststyle')
RootPanel().add(b)
RootPanel().add(h)
RootPanel().add(l)
RootPanel().add(base)
pyjd.run()
Running the build.sh script on that example, as described on the
"getting started" page,
creates output and public directories that hold the
generated code. There is a tiny bit of HTML and CSS, along with roughly
9,000 lines of Javascript that implement the example. Much of that is
likely boilerplate code to implement pyjamas itself. A
better example might be something like TimeSheet,
which implements a more realistic application, and weighs in around 23,500
lines.
There are numerous examples on
the pyjamas web page, many of which were ported from GWT. Both the source
code and the running application are available, so one can get a sense for
how much code it takes to create the examples—as well as use them as
templates for other applications. There is quite a bit of documentation,
though the FAQ would indicate that there have been complaints about that, on
the site as well, including the in-progress Pyjamas Book (which
is implemented as a pyjamas book reader application).
The project has just released
version 0.6 of the tool, with many new features outlined in the CHANGELOG. There are some 140
changes from the version 0.5, including a rework of pyjs to make
more Python features available (multiple inheritance and superclasses are
specifically mentioned) and bringing Pyjamas-Desktop into the standard
distribution. The pace of development is relatively quick; 0.5 was
released in March, and 0.6 adds quite a bit of functionality on top of that.
Pyjamas is definitely worth a look for anyone considering building a
new-style web application, and who would rather use Python than Java.
Because GWT was released as free software, Pyjamas could leverage much of
that work to give developers another language choice. Writing—and
worse, debugging—complex Javascript applications is a major chore, so
any tools that make that easier should be quite welcome. Those that just want a
bit more interactivity in their existing web applications, though, might
find the Pyjamas (and GWT) approach to be too heavy-handed for their needs.
(
Log in to post comments)