Python: module PyHtmlTable

PyHtmlTable | index /home/pasko/python/PyHtmlTable/PyHtmlTable.py

`-----------------------------------------
Class PyHtmlTable
What:
Html Table generation on the fly.
Why:
Couldn't find an equivalent to Table.pm or Table.php
Features:
Allows setting of individual row and cell attributes via arbitrary
dictionaries
Allows spanning of rows and columns
Autogrows table if cells set outside initial
Range.
Allows dynamic insertion of new rows and columns anywhere in the table
Allows bulk population of table data via arrays to arbitrary locations
in the table.
Provides default cell attributes for tablewide uniformity
and the ability to override these on a cell by cell basis.
NOTE: Row indexes start at 0 like God and Kernighan demand!
Influenced heavily by Table.pm and Table.php
Example
print "<b> 2 by 2 table </b> "
t = PyHtmlTable(2,2, {'width':'400','border':2,'bgcolor':'white'})
t.setCellcontents(0,0,"T1 Cell 00")
t.setCellcontents(0,1,"T1 Cell 01")
t.setc(1,0,"T1 Cell 01") # New shorthand: setc == setCellcontents
t.setc(1,1,"T1 Cell 11") # Most of the main functions now have horthand equivalents
t.setCellattrs(0,0,{'bgcolor':'red','width':100})
t.setca(1,1,{'bgcolor':'red'}) # New shorthand: setca == setCellattrs
t.display()
t.setCellcontents(2,0,"T1 Cell 20") # Grow outside initial bounds
t.setCellcontents(2,1,"T1 Cell 21")
t.display()
print "<p><b> adding row </b>"
t.add_row(1) #Add a row after row index 1
t.display()
print "<p><b> adding col </b>"
t.add_col(1) #Add a col after col index 1
t.display()
print "<HR><b> AFTER row and col SPANNING </b>"
t.setCellRowSpan(1,0,2) # Span cell at index row 1,col 0, make 2 high
t.setCellColSpan(1,1,2) # colSpan cell at index row 1, col 1, make 2 wide
t.display()
print "<HR><b> Embed in new table </b>"
htmlstr = t.return_html()
nt = PyHtmlTable(1,4, {'width':'800','border':2,'bgcolor':'green'})
nt.setCellcontents(0,0,"Cell TH....text left")
nt.setCellcontents(0,1,"Text right")
nt.setCellcontents(0,2,htmlstr)
nt.setCellattrs(0,0,{'bgcolor':'blue','width':200,'align':'left'})
nt.setCellattrs(0,1,{'width':200,'align':'right'})
nt.setCelltype(0,0,"TH")
nt.display()
Current-Version: $Revision: 1.13 $
Last update: $Date: 2003/07/24 14:21:21 $
Home-Page: http://pasko.net/PyHtmlTable
Primary-Site: http://pasko.net/PyHtmlTable
Software-requirements: twodarr.py
Written and debugged under Python 2.2.2 by Joe Pasko`

Modules
`` |  |
copy re | string sys | twodarr |

Classes
`` |  | PyHtmlTable
class PyHtmlTable
`` |
| _PyHtmlTable__adjust_2d_array_cols_right = __adjust_2d_array_cols_right(self, inarr, add_after_this_col) _PyHtmlTable__adjust_2d_array_rows_down = __adjust_2d_array_rows_down(self, inarr, add_after_this_row) _PyHtmlTable__adjust_dbl_indx_dict_cols_right = __adjust_dbl_indx_dict_cols_right(self, indict, shift_after_this_col) _PyHtmlTable__adjust_dbl_indx_dict_rows_down = __adjust_dbl_indx_dict_rows_down(self, indict, add_after_this_row) _PyHtmlTable__adjust_dict_cols_right = __adjust_dict_cols_right(self, indict, add_after_this_col) _PyHtmlTable__adjust_dict_rows_down = __adjust_dict_rows_down(self, indict, add_after_this_row) _PyHtmlTable__dumpattrs = __dumpattrs(self, indict) _PyHtmlTable__end_row = __end_row(self) _PyHtmlTable__end_table = __end_table(self) _PyHtmlTable__getendCellattrs = __getendCellattrs(self, row, col) `Presently unused` _PyHtmlTable__has_only_rowcolsp_attrs = __has_only_rowcolsp_attrs(self, attrstr) _PyHtmlTable__resCell = __resCell(self, row, col) _PyHtmlTable__setendCellattrs = __setendCellattrs(self, row, col, indict) `Presently unused` _PyHtmlTable__start_row = __start_row(self, row) _PyHtmlTable__start_table = __start_table(self) __init__(self, rows, cols, tattr=None) add_array_to_col(self, row, col, inarr, attrs=None) `Adds list of data specified by inarr to table object starting at row,col Optionally specify attributes to set on cells being added by defining the attrs dictionary Note: Cell attribute insertion can be additive or overwriting depending on calls to setReplaceCellattrs() or setAppendCellattrs() Default is to append new attributes` add_array_to_row(self, row, col, inarr, attrs=None) `Adds list of data specified by inarr to table object starting at row,col Optionally specify attributes to set on cells being added by defining the attrs dictionary Note: Cell attribute insertion can be additive or overwriting depending on calls to setReplaceCellattrs() or setAppendCellattrs() Default is to append new attributes` add_col(self, col) `Adds col to table after specified colidx Adding col at colidx -1 adds col to left of table` add_row(self, row) `Adds row to table after specified rowidx. Adding row at rowidx -1 adds row to top of table` addar2c(self, row, col, inarr, attrs=None) `Calls add_array_to_col` addar2r(self, row, col, inarr, attrs=None) `Calls add_array_to_row` clearCellDefattrs(self) `Clears default cell attributes` clearCellattrs(self, row, col) `Clear cells attributes` clearRowattrs(self, row) `Clear row attributes` clearTableattrs(self) `Clear Table attributes` clearcda(self) `Calls clearCellDefattrs` display(self) `Prints html table` getCellDefattrs(self) `Returns default cell attributes` getCellattrs(self, row, col) `Returns attributes set for specific cell at rowidx colidx` getCellcontents(self, row, col) `Get cells stored data values Return an &nbsp if cell is None` getCelltype(self, row, col) `Returns Celltypes which is TD or TH` getColattrs(self, col) `Presently unused` getRowattrs(self, row) `Returns attribute string for given rowidx which was set by setRowattrs` getc(self, row, col) `Calls getCellcontents` getcda(self, indict) `Calls getCellDefattrs` getct(self, row, col) `Calls getCelltype` getmaxCol(self) `Returns index of highest col( number_of_cols -1 )` getmaxRow(self) `Returns index of highest row( number_of_rows -1 )` return_html(self) `Returns html table as string` setAppendCellattrs(self) `After calling this function calls to setCellattrs() will append attribute information to cells If the cell had attributes width="400", after calling this function calls to setCellattrs( x, y, {'bgcolor':'black'}) yields attributes for cell x,y of: width="400" bgcolor="black" Note: This is the default behavior` setCellColSpan(self, row, col, numcells) `Sets COLSPAN starting at rowidx, colidx spanning numcells (Remember rows,cols start at 0,0)` setCellDefattrs(self, indict) `Sets default attributes for cells in table Used if cell does not have specific atributes` setCellRowSpan(self, row, col, numcells) `Sets ROWSPAN starting at rowidx, colidx spanning numcells (Remember rows,cols start at 0,0)` setCellattrs(self, row, col, indict) `Sets cell attributes for cell at rowidx, colidx indict is a dictionary of key=val pairs {'bgcolor':'black', 'width':200} yields <TD BGCOLOR="BLACK" width="200" > on output` setCellcontents(self, row, col, data, attrs=None) `Puts data into cell at rowidx, colidx Takes optional attribute dictionary for cell` setCelltype(self, row, col, ctype='TD') `Celltypes can be TD or TH` setColattrs(self, col, indict) `Presently unused` setReplaceCellattrs(self) `After calling this function calls to setCellattrs() will append attribute information to cells If the cell had attributes width="400", after calling this function calls to setCellattrs( x, y, {'bgcolor':'black'}) yields attributes for cell x,y of: bgcolor="black"` setRowattrs(self, row, indict) `Sets attributes for give rowidx indict is a dictionary of key=val pairs {'bgcolor':'black'} translates to <TR BGCOLOR="BLACK">` setTableattrs(self, indict) `Sets table attributes in <TABLE directive indict is a dictionary of key=val pairs {'width':'800','border':2,'bgcolor':'green'} yields <TABLE WIDTH="800" BORDER="2" BGCOLOR="GREEN">` setc(self, row, col, data, attrs=None) `Calls setCellcontents` setca(self, row, col, indict) `Calls setCellattrs` setcda(self, indict) `Calls setCellDefattrs` setcrspan(self, row, col, numcells) `Calls setCellRowSpan` setcspan(self, row, col, numcells) `Calls setCellColSpan` setct(self, row, col, ctype='TD') `Calls setCelltype` setta(self, indict) `Calls setTableattrs`

Functions
`` |  | main() `#------------------------------------------------------------- # Example html, if run from cmd line #-------------------------------------------------------------`

Data
`` |  | __file__ = './PyHtmlTable.pyc' __name__ = 'PyHtmlTable'