views:

91

answers:

4

What's the easiest way to quickly create some simple HTML in Python? All I've found so far are complex templating systems or classes for HTML generation with APIs that seem much heavier than what I need.

I could just do it myself by sticking strings together but I thought there might be a library that could save me a little time.

edit- I decided to take a closer look at the Jinja2 templating system. It looks like it could be useful at some point in the future and doesn't look like it will take as much time to figure out as I thought.

+1  A: 

Have a look at the Markup module: http://markup.sourceforge.net/

Edit: Here's another module that looks quite similar: http://www.decalage.info/en/python/html

tobiw
They both look promising, thanks for the suggestions.
Rob Lourens
A: 

If you're looking for basic and barebones then the ancient HTMLgen will do. Otherwise, use a template engine.

Ignacio Vazquez-Abrams
Wow, that is old. But thanks for the suggestion, I'll check it out.
Rob Lourens
+2  A: 

quixote is an old-ish but still fascinating framework -- it basically "embeds HTML in Python" (rather than vice versa, as all popular templating systems do).

One simple example from the overview:

def format_row [html] (head, value):
    "<tr valign=top align=left>\n"
    "  <th align=left>%s</th>\n" % head
    "  <td>%s</td>\n" % value
    "</tr>\n"

In Python proper, the first of those strings would be the docstring, the others would be ignored, and the [html] part would be a syntax error. In Quixote, the [html] marks this function as being "PTL" (Python Template Language) rather than Python proper, the file extension to use for modules with such functions is .ptl, but they can still be imported from Python and those strings are output.

I doubt you want to adopt Quixote in preference to modern Python templating approaches, but it does make for interesting reading, IMHO.

Further along similar lines is nevow (though it's more geared to generating XML, not HTML per se), esp. stan, where the canonical example is...:

>>> from nevow import flat, stan
>>> html = stan.Tag('html')
>>> p = stan.Tag('p')
>>> someStan = html[ p(style='font-family: Verdana;')[ "Hello, ", "world!" ] ]
>>> flat.flatten(someStan)
'<html><p style="font-family: Verdana;">Hello, world!</p></html>'

Kind of "even cooler"... because you don't have to worry about closing tags correctly;-).

In the end, though, for production work templating systems like jinja2 or mako are typically preferred these days -- the main practical reason being the better separation of presentation logic (in the template) from other layers (in Python code proper) than they offer wrt the "embed HTML/XML within Python" approaches, I guess.

Alex Martelli
Thanks for the suggestions. nevow looks promising.
Rob Lourens
+1  A: 

The Python string.Template may do what you want. And it's built-in.

http://docs.python.org/library/string.html#template-strings

I can't understand "APIs that seem much heavier" without further definition or examples.

S.Lott