I am looking for an easily implemented html generator for python. I found this one
http://www.decalage.info/python/html
but there is no way to add css elements (id, class) for table.
thx
I am looking for an easily implemented html generator for python. I found this one
http://www.decalage.info/python/html
but there is no way to add css elements (id, class) for table.
thx
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.
If you want programmatic generation rather than templating, Karrigell's 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</A>
There's the venerable HTMLGen by Robin Friedrich, which is hard to find but still available here (dated 2001, but HTML hasn't changed much since then ;-). There's also xist. Of course nowadays HTML generation, as Lennart points out, is generally better done using templating systems such as Jinja or Mako.
Actually you can add any attribute such as id and class to objects in HTML.py (http://www.decalage.info/python/html).
attribs is an optional parameter of Table, TableRow and TableCell classes. It is a dictionary of additional attributes you would like to set. For example, the following code sets id and class for a table:
import HTML
table_data = [
['Last name', 'First name', 'Age'],
['Smith', 'John', 30],
['Carpenter', 'Jack', 47],
['Johnson', 'Paul', 62],
]
htmlcode = HTML.table(table_data,
attribs={'id':'table1', 'class':'myclass'})
print htmlcode
The same parameter can be used with TableRow and TableCell objects to format rows and cells. It does not exist for columns yet, but should be easy to implement if needed.
I have made a simple module for doing this. It works and has the same idea as HTML.py. However its a bit different and maybe more customizable? It is very lightweight. The project is called HTMLpy (pronounced HTML pie). You can see more information at http://patx.me/htmlpy.
You might be interested in some of the Python HAML implementations. HAML is like HTML shorthand and only takes a few minutes to learn. There's a CSS version called SASS too.
http://haml.hamptoncatlin.com/
This SO page talks about Python and HAML a bit more:
I'm using HAML as much as possible when I'm programming in Ruby. And, as a footnote, there's also been some work getting modules for Perl which work with the nice MVC Mojolicious:
http://search.cpan.org/~vti/Text-Haml-0.990102/lib/Text/Haml.pm