A surprising lack in Python's standard library
Here's something that I am surprised is not already in the Python standard library: a simple module to generate and assemble HTML fragments (or if you prefer, general XML fragments), up to and including full HTML pages.
I find it especially surprising because not only is this something that a lot of people wind up needing to do sooner or later (consider generating error pages from inside a simple CGI program), but there's even a very common simple model for it that everyone seems to write their own version of:
from html import * page = HTML(HEAD(TITLE("Qwerty")), BODY(H1("Qwerty's page"), P("Some text goes here."))) print page
(Details often vary considerably beyond the basic idea of nested objects with optional keyword arguments as the properties of the element.)
Now, you can argue that this is so simple to implement from scratch that it doesn't need to be in the standard library, but I have two replies. First, doing a good implementation is more work than it looks (consider quoting of bare strings and the Unicode issues, for example), and second, if lots of people are going to do it themselves, it makes sense to save everyone the effort and put a single quality implementation in the standard library.
Possibly the Python people consider this a terrible pattern to follow once you start getting beyond the very basics and think that there is a much superior interface. If so, it would be nice if an implementation of the better version was in the standard library; as it is, as far as I can tell there's absolutely nothing that will do this at all, although there are a number of things that will parse HTML and XML.
(It is possible that a simple HTML builder is hiding somewhere in the depths of the standard XML modules. In my defense, I will note that it's not obvious from skimming the library documentation and XML is not where I naturally look for 'simple'.)
Sidebar: pointers to some relevant resources
one of the Perl versions of the same basic idea. Perl's implementation
probably came first. (HTML::LoL is another
interesting take on the overall idea.)
- XIST, which among many other
features can do this. But it looks like a very big package, which brings
up certain issues if I install it myself.
- markup.py is a single Python file and so easy to drop into a project, but it seems to not be intended to let you generate HTML fragments; instead you have to generate all of the HTML page in the proper sequence, which I find confining.
And hopefully I have missed something obvious in the standard library; if so, and if I find out about it, I'll add a note here.
Comments on this page:Written on 12 January 2009.