Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to generate html table from "table" data?

Reply
Thread Tools

how to generate html table from "table" data?

 
 
petr.jakes.tpc@gmail.com
Guest
Posts: n/a
 
      12-25-2007

Hi group,
I would like to convert the output of the SQL query, or more generally
I would like to convert any "table" data to the html table.

I would like to set some rules to format cells, columns or rows (font,
colour etc.) of the html table, according to the values in the
specific cells.

Googling for a while I have found only this tool:
http://pasko.net/PyHtmlTable/

Your tips to some other tools or/and your suggestion how to solve
above mentioned will be very helpful.

Thanks and regards

Petr Jakes
 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      12-26-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) a écrit :
> Hi group,
> I would like to convert the output of the SQL query, or more generally
> I would like to convert any "table" data to the html table.


There's MoreThanOneWayToDoIt... from simple string formatting to a
full-blown template engine.

> I would like to set some rules to format cells, columns or rows (font,
> colour etc.) of the html table, according to the values in the
> specific cells.


<ot>
Markup should only convey semantic informations - presentation is best
done using css. IOW : presentation-related stuff in the html should be
restricted to css class declarations.
</ot>

> Googling for a while I have found only this tool:
> http://pasko.net/PyHtmlTable/
>
> Your tips to some other tools or/and your suggestion how to solve
> above mentioned will be very helpful.


As I said, wrt/ html generation, there are quite a lot of possible
solutions - FWIW, generating an html table from a set of tabular data is
nothing difficult. So without more information on the context, it's hard
to give any valuable advice. Are you doing a web application ? If yes,
you should already use a template engine, so just use it. Else, why is
your application generating html at all ?
 
Reply With Quote
 
 
 
 
vasudevram
Guest
Posts: n/a
 
      12-26-2007
> (E-Mail Removed) a écrit :
>
> > Hi group,
> > I would like to convert the output of the SQL query, or more generally
> > I would like to convert any "table" data to the html table.

>
> > I would like to set some rules to format cells, columns or rows (font,
> > colour etc.) of the html table, according to the values in the
> > specific cells.

>

Why not try writing your own code for this first?
If nothing else, it'll help you learn more, and may also help you
understand better, the other options.

Vasudev Ram
-----------
Dancing Bison Enterprises
Software consulting and training
Biz site: http://www.dancingbison.com
Blog (on software innovation): http://jugad.livejournal.com
Quick and easy PDF creation toolkit: http://www.dancingbison.com/products.html

 
Reply With Quote
 
petr.jakes.tpc@gmail.com
Guest
Posts: n/a
 
      12-26-2007
>
> Why not try writing your own code for this first?
> If nothing else, it'll help you learn more, and may also help you
> understand better, the other options.
>
> Vasudev Ram


Thanks for your reply even it was not really helpful.
Of course some attempts to generate html from tabular data are behind
me. I am trying to find help here, because I think I am not the first
one, who is trying to "generate" html tables.

The purpose of my effort is to automatically and repeatedly read ¦QL
table and save the output of the SQL SELECT to the file in the html
format. Such a "reports" can be read by user using web browser later
on.

I was just trying to find if somebody here can point me to the
existing tool, which is suitable for such a task.

Anyway thank you for trying me help.

Petr Jakes
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      12-26-2007
(E-Mail Removed) a écrit :
> Vasudev Ram wrote:
>>Why not try writing your own code for this first?
>>If nothing else, it'll help you learn more, and may also help you
>>understand better, the other options.
>>

>
> Thanks for your reply even it was not really helpful.


The answers boil down to:
- use the templating engine that comes with your web framework
or
- use whatever templating engine you like
or
- just do it by hand

The remaining work is obviously such a no-brainer that there's no need
for a specific package, and so application specific that there's
probably nothing like a one-size-fits-all answer. IOW : you're not
likely to find more "helpful" answer - and your above comment won't
help. FWIW, I just wrote a function generating an html table from a list
of header and a list of rows. I wrote the most Q&D, straightforward,
braindead way, it's 17 lines long, doesn't even need an import
statement, and took me less than 2 minutes to write - that is, far less
work than reading your post and answering it.
 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      12-26-2007
On Wed, 26 Dec 2007 10:34:57 -0800 (PST), (E-Mail Removed)
declaimed the following in comp.lang.python:

>
> I was just trying to find if somebody here can point me to the
> existing tool, which is suitable for such a task.
>

Practically any of the web-template tools...

-=-=-=-=-=- code snippet from site generation script:
def b_conventions(self):
data = {}
myDB = MySQLdb.connect(host="localhost",
user="BestiariaCP",
db="bestiaria")
myC = myDB.cursor()

myC.execute("""select name, URL, banner, width, height, dates,
site, notes
from conventions
where sortdate > curdate()
order by sortdate""")
data["UpComing"] = myC.fetchall()

myC.execute("""select name, URL, banner, width, height, dates,
site, notes
from conventions
where sortdate <= curdate()
order by sortdate""")
data["History"] = myC.fetchall()

myC.close()
myDB.close()

fid = open("./static/b_conventions.html", "w")
fid.write(renderTemplate(file="./templates/b_conventions.html",
loc=data))
fid.close()
-=-=-=-=-=-
-=-=-=-=-=- snippet from template file -- using CherryTemplate
<!-- CONVENTION TABLE START DO NOT REMOVE OR EDIT -->

<table border="1"
cellspacing="2"
cellpadding="2"
align="center"
frame="box"
rules="all">
<TR>
<TH align="center" nowrap><B><FONT
size="+2">Convention</FONT></B></TH>
<TH align="center" nowrap><B><FONT
size="+2">Dates</FONT></B></TH>
<TH align="center" nowrap><B><FONT
size="+2">Location</FONT></B></TH>
</TR>
<py-for="(name, URL, banner, width, height, dates, site, notes) in
UpComing">
<TR><TD align="center" nowrap>
<py-if="URL"><A href=<py-eval="'\x22%s\x22' % URL">></py-if>
<py-if="banner"><img src=<py-eval="'\x22%s\x22' % banner">
<py-if="width">width=<py-eval="'\x22%s\x22' %
width"></py-if>
<py-if="height">height=<py-eval="'\x22%s\x22' %
height"></py-if>
alt=<py-eval="'\x22%s\x22' %
name">></py-if><py-else>
<B><FONT
size="+1"><py-eval="name"></FONT></py-else></B><py-if="URL"></A></py-if></TD>
<TD align="center"><py-if="dates"><py-eval="dates"></py-if></TD>
<TD align="center"><py-if="site"><py-eval="site"></py-if>
<py-if="notes"><BR><FONT
size="-1"><py-eval="notes"></FONT></py-if></TD></TR>
</py-for>

<py-for="(name, URL, banner, width, height, dates, site, notes) in
History">
<TR><TD align="center" nowrap>
<py-if="URL"><A href=<py-eval="'\x22%s\x22' % URL">></py-if>
<py-if="banner"><img src=<py-eval="'\x22%s\x22' % banner">
<py-if="width">width=<py-eval="'\x22%s\x22' %
width"></py-if>
<py-if="height">height=<py-eval="'\x22%s\x22' %
height"></py-if>
alt=<py-eval="'\x22%s\x22' %
name">></py-if><py-else>
<B><FONT
size="+1"><py-eval="name"></FONT></py-else></B><py-if="URL"></A></py-if></TD>
<TD align="center"><py-if="dates"><py-eval="dates"></py-if></TD>
<TD align="center"><py-if="site"><py-eval="site"></py-if>
<py-if="notes"><BR><FONT
size="-1"><py-eval="notes"></FONT></py-if></TD></TR>
</py-for>
</TR>
</TABLE>

<br><br>

<!-- CONVENTION TABLE END DO NOT REMOVE OR EDIT -->
-=-=-=-=-=-=-
-=-=-=-=-=-=- the generated web site (the site is static, I have to
run the Python to generate the pages, then upload them)

http://www.bestiaria.com/b_conventions.html
--
Wulfraed Dennis Lee Bieber KD6MOG
(E-Mail Removed) (E-Mail Removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (E-Mail Removed))
HTTP://www.bestiaria.com/
 
Reply With Quote
 
petr.jakes.tpc@gmail.com
Guest
Posts: n/a
 
      12-26-2007
Dennis,
Thank you very much for your code snippet.
I will try to install CherryTemplate and use it.

I did not work with any template tool before and I am not the *****
class programmer as other people here, so my questions maybe look
"strange" or "stup..".

I didn't mean to offend somebody here and I am really grateful for all
replies.

Thank you

Petr Jakes
 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      12-27-2007
On Wed, 26 Dec 2007 15:28:24 -0800 (PST), (E-Mail Removed)
declaimed the following in comp.lang.python:

> Dennis,
> Thank you very much for your code snippet.
> I will try to install CherryTemplate and use it.
>

There are many other templating systems around -- I'm not even sure
if CherryTemplate is still supported (or considered "complete")... My
generation usage has the templates dated April of 2006.

Even the latest version of CherryPy doesn't use CherryTemplate...
Unless CherryTemplate mutated into some other name <G>

The idea behind templating systems is that one separates the data
retrieval (the MySQL snippet) from the HTML markup... One can change the
generated web page by just changing the template HTML, and never touch
the actual code that handles the database. Conversely, you could change
the database logic completely and not have to change the HTML template.

Many times though, I find it faster to do a "one-off" by just coding
loops that output the pieces of HTML needed, a la (pseudo-code):

htm = open("some.html", "w")
htm.write("""<table>\n\t<tr>\n""")
for fld in list_of_fieldnames:
htm.write("""\t\t<th>%s</th>\n""" % fld)
htm.write("""\t</tr>\n""")
for rcrd in list_of_retrieved_data:
htm.write("""\t<tr>\n""")
for fld in rcrd:
htm.write("""\t\t<td>%s</td>\n""" % fld)
htm.write("""\t</tr>\n""")
htm.write("""</table>\n""")
htm.close()

This quick&dirty solution is useful when my final goal is to just
include the table (cut&paste) into a word document.
--
Wulfraed Dennis Lee Bieber KD6MOG
(E-Mail Removed) (E-Mail Removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (E-Mail Removed))
HTTP://www.bestiaria.com/
 
Reply With Quote
 
Ricardo Aráoz
Guest
Posts: n/a
 
      12-27-2007
Bruno Desthuilliers wrote:
> (E-Mail Removed) a écrit :
> > Vasudev Ram wrote:
>>> Why not try writing your own code for this first?
>>> If nothing else, it'll help you learn more, and may also help you
>>> understand better, the other options.
>>>

>> Thanks for your reply even it was not really helpful.

>
> The answers boil down to:
> - use the templating engine that comes with your web framework
> or
> - use whatever templating engine you like
> or
> - just do it by hand
>
> The remaining work is obviously such a no-brainer that there's no need
> for a specific package, and so application specific that there's
> probably nothing like a one-size-fits-all answer. IOW : you're not
> likely to find more "helpful" answer - and your above comment won't
> help. FWIW, I just wrote a function generating an html table from a list
> of header and a list of rows. I wrote the most Q&D, straightforward,
> braindead way, it's 17 lines long, doesn't even need an import
> statement, and took me less than 2 minutes to write - that is, far less
> work than reading your post and answering it.


Hi.
Bruno, could you please post those 17 lines? I'm not actually doing HTML
work but I would find it interesting going through your code.

TIA



 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      12-28-2007
Ricardo Aráoz a écrit :
> Bruno Desthuilliers wrote:

(snip)
>> FWIW, I just wrote a function generating an html table from a list
>> of header and a list of rows. I wrote the most Q&D, straightforward,
>> braindead way, it's 17 lines long, doesn't even need an import
>> statement, and took me less than 2 minutes to write - that is, far less
>> work than reading your post and answering it.

>
> Hi.
> Bruno, could you please post those 17 lines? I'm not actually doing HTML
> work but I would find it interesting going through your code.
>

I'm afraid I throw that code away - as I said, this was braindead Q&D
code, and certainly not even worth the time you'd spend reading it. But
I can rewrite it if you want:

def generate_html_table(headers, rows):
html = []

if headers:
html.append("<tr>")
for header in headers:
html.append("<th>%s</th>" % header)
html.append("</tr>")

if rows:
for row in rows:
html.append("<tr>")
for cell in row:
html.append("<td>%s</td>" % cell)
html.append("</tr>")

if html:
html = ["<table>"] + html + ["</table>"]

return "\n".join(html)


Nothing interesting here, as you can see. And if you're going to do
anything serious in web development, you'll be better using a templating
system anyway.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Generate html table with merged cells Greg ASP .Net Web Controls 1 07-25-2007 02:18 PM
How to generate data driven HTML Table like CrossTab Kamal Ahmed ASP .Net 0 06-28-2006 02:00 PM
How to generate variable labels for same component within a generate loop Weng Tianxiang VHDL 5 02-16-2006 01:45 PM
Help - Generate N x 2 html table cardinallijr XML 4 01-03-2006 03:49 PM



Advertisments