how to generate html table from "table" data?

P

petr.jakes.tpc

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
 
B

Bruno Desthuilliers

(e-mail address 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.
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 ?
 
P

petr.jakes.tpc

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
 
B

Bruno Desthuilliers

(e-mail address removed) a écrit :
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.
 
D

Dennis Lee Bieber

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 address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
P

petr.jakes.tpc

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
 
D

Dennis Lee Bieber

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 address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
R

Ricardo Aráoz

Bruno said:
(e-mail address removed) a écrit :

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
 
B

Bruno Desthuilliers

Ricardo Aráoz a écrit :
Bruno Desthuilliers wrote: (snip)

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top