[Tutor] Looking for an HTMLgen table example

Michael P. Reilly arcege@speakeasy.net
Sat, 16 Jun 2001 07:14:56 -0400 (EDT)


F. Tourigny wrote
> I'm looking for a simple example of how to convert
> ASCII strings into an html table with HTMLgen and,
> more specifically, how to append rows, and put data
> into cells.  I'm just starting with Python and the
> HTMLcalendar.py and barchart.py demo modules coming
> with the distribution are way over my head at this
> point.  (They're proving to be a good read, though.)

I'd use the TableLite class; it is easier and more like the other
classes in HTMLgen.  Think about it like creating creating nested
lists.

>>> mylist = []
>>> for x in range(5):
...   sublist = []
...   for y in range(5, 10):
...     sublist.append( (x, y) )
...   mylist.append( sublist )
...

At this point you have a list of five sublists, each sublist
will have five tuples of the coordinates.
>>> print mylist
[[(0, 5), (0, 6), (0, 7), (0, 8), (0, 9)], [(1, 5), (1, 6), (1, 7), (1, 8),
(1, 9)], [(2, 5), (2, 6), (2, 7), (2, 8), (2, 9)], [(3, 5), (3, 6), (3, 7),
(3, 8), (3, 9)], [(4, 5), (4, 6), (4, 7), (4, 8), (4, 9)]]

Applying this to the HTMLgen, you can do something similar.
>>> import HTMLgen
>>> table = HTMLgen.TableLite()
>>> for x in range(5):
...   row = HTMLgen.TR()
...   for y in range(5, 10):
...     row.append( HTMLgen.TD( '%d, %d' % (x, y) ) ) # needs to be string
...   table.append( row )
...
>>> print table
<TABLE><TR><TD>0, 5</TD><TD>0, 6</TD><TD>0, 7</TD><TD>0, 8</TD><TD>0, 9</TD></TR>
<TR><TD>1, 5</TD><TD>1, 6</TD><TD>1, 7</TD><TD>1, 8</TD><TD>1, 9</TD></TR>
<TR><TD>2, 5</TD><TD>2, 6</TD><TD>2, 7</TD><TD>2, 8</TD><TD>2, 9</TD></TR>
<TR><TD>3, 5</TD><TD>3, 6</TD><TD>3, 7</TD><TD>3, 8</TD><TD>3, 9</TD></TR>
<TR><TD>4, 5</TD><TD>4, 6</TD><TD>4, 7</TD><TD>4, 8</TD><TD>4, 9</TD></TR>
</TABLE>

>>>

Captions are added as rows are and headers are added as columns.

HTH,
  -Arcege

-- 
+----------------------------------+-----------------------------------+
| Michael P. Reilly                | arcege@speakeasy.net              |