recommended way of generating HTML from Python

Pierre Quentel quentel.pierre at wanadoo.fr
Sun Feb 20 15:37:25 EST 2005


Here are a couple of pointers. I agree with Michele that it would be 
nice to have some kind of standardization. Maybe this would be worth a 
post to the Web-SIG ?

- I posted a 70-line recipe on the Python Cookbook, a sort of poor man's 
HTMLGen called HTMLTags
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/366000
with a syntax like this :

# print HTML( HEAD(TITLE('Test document')) +
#    BODY(H1('This is a test document')+
#        TEXT('First line')+BR()+
#        TEXT('Second line')))

The use of addition allows for shortcuts like
     print Sum([ TR(TD(i)+TD(i*i)) for i in range(100) ])

where Sum works like the built-in sum, but sum only works for numbers 
apparently

- in the Cookbook someone mentioned the HyperText package, published in 
2000 by Andy Dustman : http://dustman.net/andy/python/HyperText/

It uses this syntax :

# print TABLE(
#    TR((TH('SPAM'), TH('EGGS')),
#    TR(TD('foo','bar', colspan=2))
# )

The package also handles XML, SGML etc.

- I wrote to Andy and he said there was also Living Logic's XIST :
http://www.livinglogic.de/Python/xist/index.html

An example taken from the site :

#node = xsc.Frag(
#  xml.XML10(),
#  html.DocTypeXHTML10transitional(),
#  html.html(
#      html.head(
#         meta.contenttype(),
#         html.title("Example page")
#      ),
#      html.body(
#         html.h1("Welcome to the example page"),
#         html.p(
#            "This example page has a link to the ",
#            html.a("Python home page", href="http://www.python.org/"),
#            "."
#         )
#      )
#   )
# )
#
# print node.conv().asBytes(encoding="us-ascii")



More information about the Python-list mailing list