View Source

28 August 2008

I have written before about how I'm running my website on custom developed Python code. Back then I said:

The code isn't publicly available at this point, although I do intend to release it when I feel it's ready.

Well, stuff like this never is truly ready, but I'm putting it out there anyway: the basis is a custom little web framework called “Diva”, the project site is here, Subversion repository here. The code for both my blog and my scratchpad site is included as examples.

A Custom Framework You Say?

Releasing this code feels weird, as there are of course already dozens of options if you're looking for ways to do Python web application development. So let me try to make this clear: I'm not trying to compete with anyone here, and if you're looking for a Python web framework, you likely want to look elsewhere. That's not because the code is bad, but because I have no intention of maintaining this project in a way you'd expect from a production-ready Python web framework. Rather, it's my little sandbox where I play with various ideas on web application development. See the Big Fat Warning on the project site for more background.

But while publishing this code feels weird, hacking on it has been great fun. As far as frameworks go, this is definitely one of the simpler ones, more like than, say, Django or TurboGears. I do think that there aren't many solid options in that space, and if you want something that provides really good integration with Genshi out of the box, you're out on your own with pretty much all of the options.

Framework Features

Diva provides tight Genshi integration: Request handler functions can (and usually do) return markup streams, which can be processed in various different ways before they actually get serialized to a response body. This is used to automatically fill in form values and to (optionally) add form tokens for CSRF protection, to name a few examples.

Beyond that, there's support for easy but flexible URL routing and generation, HTML form processing, a simple but flexible framework for authentication and authorization, Babel integration for internalization and localization, developer-oriented error pages that let you drill-down into stack frame locals and template data, an easy to use development server, pervasive logging (including support for logging to FirePHP), and so on.

There's no built-in support for connecting to a database, or integration with something like SQLAlchemy. That's primarily because I currently use CouchDB for my own sites, but also because I think that not being able to assume a particular data storage model is a rather healthy constraint on the design and scope of the framework.

Another thing Diva doesn't do that so many other frameworks seem to do these days is generate code for you. There are no scripts for starting projects, adding controllers or models, and so on. Personally, I always strongly disliked how those go and just dump code into your project. You need to track what they added, understand whether that's really the code you wanted in the first place, and potentially fix it up or revert to a previous version. Plus the scripts create the (often inaccurate) impression that you can't do whatever magic they do by just writing a couple of lines of code.

That's All

Anyway, there you go. Check out the code, the examples, and the early bits of documentation if you have some time to kill, or even try it on your own toy projects. And let me know what you think.