[Tutor] Generating simple HTML from within Python
duncan at thermal.esa.int
Mon May 14 10:24:53 CEST 2007
> I've been searching for a module to allow simple HTML generation, but
> most of them appear to be more concerned with *parsing* HTML and XML.
> The only one that looks promising from reviews dating back to 1998 or
> so is HTMLgen, but the link on starship.python.net appears long dead.
> So my question is, what is the preferred way of generating simple HTML
> or XHTML files these days? Is there a 'son of HTMLgen' or similar module?
OK, my thanks to everyone who replied, either on the list or privately.
I followed the links, and the links from those links, and poked around
in the various documentation. What I want to generate depends on the
contents of directories, etc. and is therefore has more variation than
is handled easily in the templating systems.
I chose the HyperText module (http://dustman.net/andy/python/HyperText/)
because this fits more naturally with the code I'm updating, and also
allows subclassing for customisation, etc.
To give you a flavour, one code snippet changes from:
htmlFile.write('<h2>The related file(s) for this case are:</h2>\n')
for related_file in theCase.inputData.relatedFiles:
htmlFile.write('<li><a href="%s">%s</a></li>\n' % (
to the slightly more verbose, but much clearer and less error-prone:
if len(testCaseData.inputData.relatedFiles) > 0:
text = "The related file(s) for this case are:"
heading = HTML.H2(text)
bulletList = HTML.UL()
for fileName in testCaseData.inputData.relatedFiles:
anchor = HTML.A(fileName, href=fileName)
listItem = HTML.LI(anchor)
and the module handles all of the start and end tags, indentation and other
pretty printing. My only concern is that it is almost as old as HTMLgen :-(
More information about the Tutor