Portrait of Edd Dumbill, taken by Giles Turnbull

Subscribe to updates

Feed icon Atom or RSS

or get email updates

What I make

a conference management web application

XTech Conference
a European web technology conference

Cheetah and Quixote: code-behind in Python

I've been looking around for a while for a sensible way of creating Python web applications. As long-standing readers may know, I've used Zope for a considerable time for this web site, but I've come to have two reservations about it:

  • No transparency: as everything is hidden in different directories, it's hard to get an overall view of an application.
  • Too much magic: concepts like acquisition are really cool, but Zope has too much magic floating around in it for me to have any real confidence about the integrity of non-trivial applications.

Mono's ASP.NET functionality has impressed me a great deal, but I really am not sure I want to base things on Mono's ASP.NET implementation right now. In my mind at least, it needs another year or so to firm up.

ASP.NET's code-behind technique is very attractive. This is where a web page extends a class. Here's a very simple example from my XmlIndexer

 <%@ Page Inherits="XmlIndexer.WebSearch" %>
 <% Response.ContentEncoding = new System.Text.UTF8Encoding ();
    Index (Request["basedir"], Request["rules"], Request["filename"]); %>

(A better introduction can be found in the excellent sections Niel Bornstein wrote on ASP.NET in Mono: A Developer's Notebook.)

Code-behind helps in limiting the web page template to display logic only, and keeping all the business logic somewhere else. I'm keen on testing and re-use, and every time non-trivial logic leaks into a web page template we start to lose badly.

So, after some research and advice from friends, I spent yesterday experimenting with Quixote and Cheetah. Quixote is a web application development framework for Python. It provides a sensible way of turning a tree of Python code and templates into a web site. Although Quixote comes with its own templating language, I'm not very keen on it. Hence Cheetah, a four-year-old templating language that seems very well thought out.

Most importantly, Cheetah has support for code-behind in approximately the same way ASP.NET does. Here's how I'd write the above code-behind example in Cheetah:

 #extends XmlIndexer.WebSearch
 #implements respond
 #$Index ($form.basedir, $form.rules, $form.filename)

(The bit you don't see in both examples is that the WebSearch class subclasses the page template class, System.Web.UI.Page in ASP.NET and Cheetah.Template in Cheetah.)

If you Google around, you can find various ways of integrating Cheetah and Quixote, but none of the existing ones were exactly to my liking. The best example is given by Graham Fawcett on the Quixote wiki, but it doesn't cache compiled Cheetah templates.

I amended Graham's code to cache compiled Cheetah templates, and recompile them when the underlying filesystem template changes. Download it here: qcheetah.py. Along with the example tarball posted on the wiki, it's enough to get going writing Cheetah/Quixote applications.

What's frustrating is that this combination isn't adequately presented and described anywhere. To me, Quixote plus Cheetah could be the direct (and free) Python answer to ASP.NET, given the addition of some glue classes. Most of what you'd need is known to Google already, it's just not pulled together in a coherent project.

The current Python alternatives try hard but don't quite make it. Zope has its place, but it isn't the thing to present as the alternative to JSP or ASP.NET. Webware always seems to be an also-ran, although it's often used with Cheetah.

Now I've offended the majority of Python web developers, I ought to point out that what I like about the Quixote plus Cheetah combination is the lack of magic involved. It doesn't feel like I'm installing a huge black box within whose rules I must play. I retain enough control to give myself confidence that the problems that crop up can be fixed by me, rather than waiting for the next point release of the platform.

It took me about a day to get up to speed. Cheetah's user documentation is excellent, and the source code for both Quixote and Cheetah is understandable. As I've already said, Google also knows enough about both to be helpful.

So, that's my cool Python start-up idea for this year: Queetah. Chixote, whatever....

blog comments powered by Disqus

You are reading the weblog of Edd Dumbill, writer, programmer, entrepreneur and free software advocate.
Copyright © 2000-2012 Edd Dumbill