how to generate html table from "table" data?

Discussion in 'Python' started by petr.jakes.tpc@gmail.com, Dec 25, 2007.

  1. Guest

    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
    , Dec 25, 2007
    #1
    1. Advertising

  2. 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 ?
    Bruno Desthuilliers, Dec 26, 2007
    #2
    1. Advertising

  3. vasudevram Guest

    > 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
    vasudevram, Dec 26, 2007
    #3
  4. Guest

    >
    > 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
    , Dec 26, 2007
    #4
  5. 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.
    Bruno Desthuilliers, Dec 26, 2007
    #5
  6. On Wed, 26 Dec 2007 10:34:57 -0800 (PST),
    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

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
    Dennis Lee Bieber, Dec 26, 2007
    #6
  7. Guest

    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
    , Dec 26, 2007
    #7
  8. On Wed, 26 Dec 2007 15:28:24 -0800 (PST),
    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

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
    Dennis Lee Bieber, Dec 27, 2007
    #8
  9. Bruno Desthuilliers wrote:
    > 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
    Ricardo Aráoz, Dec 27, 2007
    #9
  10. 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.
    Bruno Desthuilliers, Dec 28, 2007
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Weng Tianxiang
    Replies:
    5
    Views:
    1,298
    Christophe
    Feb 16, 2006
  2. cardinallijr

    Help - Generate N x 2 html table

    cardinallijr, Jan 2, 2006, in forum: XML
    Replies:
    4
    Views:
    409
    Janwillem Borleffs
    Jan 3, 2006
  3. Kamal Ahmed
    Replies:
    0
    Views:
    466
    Kamal Ahmed
    Jun 28, 2006
  4. Replies:
    3
    Views:
    396
    red floyd
    Apr 7, 2006
  5. Harlan Messinger
    Replies:
    2
    Views:
    2,279
    John Bell
    Mar 28, 2010
Loading...

Share This Page