[Tutor] Can someone help me to improve this segment?

Lloyd Kvam lkvam@venix.com
Tue, 30 Oct 2001 17:22:02 -0500


This suggestion may not fit in with the rest of what you are doing, but HTMLgen is reasonably quick and easy for creating HTML output.  A table is built from nested lists.  Since the HTML text is built in one swoop, not by modifying an existing
string, it seems quick enough.  It is simply necessary to build the lists of data.  Here is my block of code.  The table being generated runs 6 to 12 pages depending upon the selected data.

def toHTML( self, rptname="Cumulative Findings Report", rptinfo="Test Version"):
	doc = HTMLgen.SimpleDocument()
	h = HTMLgen.Heading( 1, rptname)
	doc.append(h)
	table = HTMLgen.Table( rptinfo)
	table.heading = [' '] + map( lambda a: a.label, self.cols)
	table.body = []
	# r is the first row and contains column totals	
	r = [' ']	# first column generally contains row labels
	for col in self.cols:
		r.append(get0( col.counts))
	table.body.append(r)
	for block in self.blocks:
		table.body.append( [block.title] + [' ']*len(self.cols))
		for row in block.rows:
			drow = block.tagRows.get(row.subcol_name, None)
			table.body.append( [row.label] + row.pct0( self.cols, drow))
	doc.append( table)
	doc.write


My only serious glitch in doing this was insuring that each row contains the same number of columns.

In terms of your snippet of code:
	...
	table = HTMLgen.Table('Table Title')
	table.body = result
	doc.append(table)

The HTMLgen code is available at:
http://starship.python.net/crew/friedrich/HTMLgen/html/main.html

You can see for yourself how the HTML tags get produced.


Titu Kim wrote:
> 
> Hi,
> I am little obssessed with my own coded after i
> found out it is slow. I am trying to construct a html
> table string from a result that i obtain from
> database.My code segment look like this: assume result
>  holds 2D list.
>  *******  method********************************
>  def f1(item):
>      return "<td>"+str(item)+"</td>"
>  *****************************************
>  ************main program piece***********
>  tab = "<table align='center' border='1'>\n"
>  for x in result:
>     tab = tab + "<tr>"
>     tab = tab + string.join(map(f1,x),"")+"</tr>\n"
>  tab = tab + "</table>"
>  **********************************************
>  Can anyone improve this code segment? Thanks alot
> 
>  Regards,
> 
>  Kim Titu
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Make a great connection at Yahoo! Personals.
> http://personals.yahoo.com
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-443-6155
fax:	801-459-9582