31

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
34k10 gold badges69 silver badges107 bronze badges
asked Oct 10, 2009 at 16:56

9 Answers 9

22

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

answered Aug 28, 2014 at 4:51
Sign up to request clarification or add additional context in comments.

Comments

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,4414 gold badges33 silver badges42 bronze badges
answered Oct 10, 2009 at 17:20

Comments

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,9281 gold badge23 silver badges17 bronze badges
answered Oct 10, 2009 at 17:13

1 Comment

As an aside: Genshi is the templating engine used by Trac.
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.

answered Oct 10, 2009 at 17:48

1 Comment

Nowadays? Python templating has been around since at least DTML, 1997. :) ZPT which is better since 2001. :) [Just nitpicking]
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>
answered Jul 16, 2011 at 6:38

Comments

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.

answered Oct 27, 2009 at 20:49

Comments

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 :)

answered Nov 2, 2016 at 9:54

Comments

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

answered Apr 10, 2010 at 7:35

Comments

1

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

answered Aug 9, 2018 at 12:55

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.