Dynamic HTML controls

Pierre Quentel quentel.pierre at wanadoo.fr
Tue Jan 12 10:07:21 CET 2010


On 12 jan, 04:26, Alan Harris-Reid <aharrisr... at googlemail.com> wrote:
> Hi,
>
> Does anyone know where I can find any decent dynamically-constructed
> HTML control classes (dropdown list, table, input field, checkbox, etc.)
> written in Python.  For example, for a HTML table I would like something
> like...
>
> MyTable = html_table()       # instantiate class
> MyTable.data = data_list    # data-list (eg. cursor from SQL SELECT
> statement)
> MyTable.border = 1          
> MyTable.width = 987  
> MyTable.column_headers = col_headers    # list or tuple of column-headers
> table_code = MyTable.table.create()           # returns string
> containing appropriate HTML code
>
> I don't mind writing my own classes (it will be good practice for me),
> but I don't want to re-invent the wheel if it can be avoided.
>
> TIA,
> Alan Harris-Reid

Hi,

There are a few modules to generate HTML from Python : there is a list
at http://wiki.python.org/moin/Templating, section HTML Generation
packages

With HTMLTags, your example would be coded like this :

from HTMLTags import *
table = TABLE(border=1,width=987)
table <= TR(Sum([TD(header) for header in col_headers]))
for result in data_list:
    table <= TR(Sum([TD(value) for value in result]))
print table

The operator <= means "add child" in the DOM tree structure, it avoids
having to nest tags with brackets

- Pierre



More information about the Python-list mailing list