[Zope] Database query -> HTML table
Mike Orr
mso@sense-sea-MegaSub-1-220.oz.net
Thu, 13 Apr 2000 17:42:34 -0700
I'm trying to find a generic way to turn a database query into an
HTML table the way the ZSQL Method's "test" routine does.
I can do this with a literal list calling a Python method:
Python method: table
Parameter list:
headers, data,
table='<TABLE BORDER>', ntable='</TABLE>', tr='<TR>', ntr='</TR>',
td='<TD ALIGN="left" VALIGN="top">', ntd='</TD>',
th='<TH ALIGN="left" VALIGN="top">', nth='</TH>'
Code:
"""
in : headers, list of strings, the column titles.
: data, list of tuples, each row of data.
: table, ntable, tr, ntr, td, ntd, th, nth,
strings, how to render these HTML tags.
"""
headers_len = len(headers)
data_len = len(data)
if data_len == 0:
return "<!-- No data for table -->"
if data_len > headers_len:
# Why doesn't Zope have a list() function?
headers = headers[:] # Make a copy.
ext = [ " " ] * (data_len - headers_len)
headers.extend(ext)
ret = [table, tr]
for h in headers:
ret.extend([th, h, nth])
ret.append(ntr)
for row in data:
ret.append(tr)
for d in row:
ret.extend([td, d, ntd])
ret.append(ntr)
ret.append(ntable)
return _.string.join(ret, '\n')
I can then call it with a literal list:
<dtml-var expr="table(['header1', 'header2'], [('value 1a', 'value 1b'),
('value2a', 'value2b')])">
What I want is a Python method that constructs the header and value
lists and calls table(header, value). So that my documents can have a nice,
clean:
<dtml-var expr="sql_table( my_zsql_method(arg1, arg2, ...) )">
Where can I find some documentation on using a ZQSL Method object in
Python?
Or is there a way to access the table-creation code Zope uses in the
"test" routine?
--
-Mike Orr, mso@mso.oz.net -or- mso@oz.net (permanent: mso@jimpick.com)
http://mso.oz.net/ English * Esperanto * Russkiy * Deutsch * Espan~ol