29

I am looking for an easily implemented HTML generator for Python. I found HTML.py, but there is no way to add CSS elements (id, class) for table.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Meloun
  • 13,601
  • 17
  • 64
  • 93

9 Answers9

20

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")
print(t)


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

Disclaimer: I am the author of dominate

Knio
  • 6,638
  • 3
  • 29
  • 29
13

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</DIV>
Core Xii
  • 6,270
  • 4
  • 31
  • 42
Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
8

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.

zanetu
  • 3,740
  • 1
  • 21
  • 17
Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
6

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.

Vinay Sajip
  • 95,872
  • 14
  • 179
  • 191
5

This is one ultra-simple HTML generator I have written. I use it build-time to generate html. If one is generating html pages run-time then there are better options available

Here is the link

http://pypi.python.org/pypi/sphc

And a quick example

>> import sphw
>> tf = sphw.TagFactory()
>>> div = tf.DIV("Some Text here.", Class='content', id='foo')
>>> print(div)

<DIV Class="content", id="foo">Some Text here.</DIV>
Shekhar
  • 7,095
  • 4
  • 40
  • 45
4

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.

decalage
  • 41
  • 1
4

Ok, here's another html generator, or I prefer to think of it as a compiler.

https://pypi.python.org/pypi/python-html-compiler

This is a set of base classes that can be used to define tags and attributes. Thus a tag class has attributes and children. Children are themselves Tag classes that have attributes and children etc etc. Also you can set parameters that start with your root class and work up the various branches.

This will allow you to define all the tag classes you want thus be able to create customised classes and implement any tags or attributes you want.

Just started on this, so if anybody wants to test :)

MarcNealer
  • 51
  • 3
2

Html generation or any text generatio,jinja is a powerful template engine.

Prabhat Ranjan
  • 400
  • 3
  • 17
1

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/

"Is there a HAML implementation for use with Python and Django" 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://metacpan.org/pod/Text::Haml

Community
  • 1
  • 1
the Tin Man
  • 158,662
  • 42
  • 215
  • 303