Python HTML generator

Python HTML generator

Dominate is an HTML generation library that lets you easily create tags. In dominate, python reserved words are prefixed with an underscore, so it would look like this:

from dominate.tags import *
t = div(table(_id=the_table), _class=tbl)

<div class=tbl>
  <table id=the_table></table>

Disclaimer: I am the author of dominate

If you want programmatic generation rather than templating, Karrigells HTMLTags module is one possibility; it can include e.g. the class attribute (which would be a reserved word in Python) by the trick of uppercasing its initial, i.e., quoting the doc URL I just gave:

Attributes with the same name as
Python keywords (class, type) must be
capitalized :

print DIV(bar, Class=title)  ==>  <DIV class=title>bar</DIV>

Python HTML generator

HTML Generation is usually done with one of the infinite amounts of HTML templating languages available for Python. Personally I like Templess, but Genshi is probably the most popular. There are infinite amounts of them, there is a list which is highly likely to be incomplete.

Otherwise you might want to use lxml, where you can generate it in a more programmatically XML-ish way. Although I have a hard time seeing the benefit.

Leave a Reply

Your email address will not be published. Required fields are marked *