TWO IMPLEMENTATIONS

There are actually two separate table implementations in HTMLgen now. The first was historically taken from the old HTMLsupport.py function library. It was designed to take a list of lists and construct a table correctly sized to contain the data, and allowed for some limited customization. For general table display it works fine and is named Table in this module.

The newer implementation was a result of feedback I got during the 1.2 beta releases. It is a collection of classes for the lower level table primitives, TD, TR, TH and Caption along with a simple container class called TableLite. I called it TableLite because it does very little for you, (but it does get out of the way). The user is thus responsible for structuring the contents of each row of the table as well as all other heading and border specifications with the appropriate mix of these classes. Although this requires more coding work on the user's part it does provide complete flexibility and control over the table construction. For those with special table needs, building custom classes on top of TableLite and friends may be the favored approach.
N.B.Please be aware though, that this approach can become a performance problem as all the low level elements are implemented as class instances. It'll be at least two times as slow as a more hardwired approach such as Table. (In particular, the start_tag() method is cool from a reuse perspective but is expensive in CPU cycles.)

FEATURES

Table class

The Table class is instantiated with the table's name (which becomes it's caption), and then is tailored with various keyword parameters or direct attribute assignments. Several attributes control alignment, spacing, border characteristics. The default settings result in a table which looks much like the following. Border is set to 2, cell padding is 4, and overall width is 100%. For example the following code was used to generate the next table.

>>> t = HTMLgen.Table('Caption')
>>> h = ['head 1', 'head 2', 'head 3']
>>> t.heading = h
>>> l = ['one', 'two','three']
>>> t.body = [l]
>>> print t

Caption
head 1head 2head 3
one two three

The body attribute contains a list of lists, the length of which determines the number of rows in the table. The heading attribute is just a list of strings and determines the number of columns. The intent behind the Table class is to provide a simple interface using fairly natural Python datatypes as arguments. See the main manual for detailed documentation.

The TableLite class(es)

The TableLite class is a general container class to be populated by instances from the TD, TR, TH, and Caption classes. All these classes inherit from AbstractTag like most other HTML markup classes. AbstractTag supports such things as append, prepend, copy, markup, as well as others. The following is a usage example.

>>> TDlist = map(HTMLgen.TD, ['one', 'two', 'three'])
>>> body = HTMLgen.TR()
>>> body = body + TDlist
>>> THlist = map(HTMLgen.TH, ['head 1', 'head 2', 'head 3'])
>>> heading = HTMLgen.TR()
>>> heading = heading + THlist
>>> cap = HTMLgen.Caption('Caption')
>>> t = HTMLgen.TableLite(border=2, cellpadding=4, cellspacing=1,width="100%")
>>> t.append(cap, heading, body)

This is obviously more complicated but is necessary when using low level classes such as these. Note: the defaults are only what the browser might use; the TableLite class provides no defaults like the Table class.

The examples below use the barchart module to generate tables which use the TableLite class.