0

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.

Rob Lourens
  • 15,081
  • 5
  • 76
  • 91
  • What kind of "simple HTML"? What sort of capabilities do you need this system to have - variable substitution? Looping? Conditionals? Syntax checking? – David Z Jun 30 '10 at 01:38
  • 1
    How is Jinja2 "complex"? A couple of lines of code are all it takes to fill a template. Can you provide some concrete definition of "complex"? – S.Lott Jun 30 '10 at 02:21
  • I need to generate some HTML to display a report- just a bunch of tables and some images. It's a small project and I figured anything that would require me to learn (what looks like, at first glance) a large API would be a waste of time. I was using ElementTree, but http://stackoverflow.com/questions/3145015/multiple-text-nodes-in-pythons-elementtree-html-generation. – Rob Lourens Jun 30 '10 at 02:40
  • Please UPDATE your question with your definition of complex. Please include some reference or link that shows that the two API calls required to make a template package like Jinja2 work are complex. Please do not add comments to a question which you own. – S.Lott Jun 30 '10 at 10:31

4 Answers4

2

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
  • 854,459
  • 170
  • 1,222
  • 1,395
1

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
  • 431
  • 3
  • 7
1

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
  • 384,516
  • 81
  • 508
  • 779
0

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

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358