Connecting Tech Pros Worldwide Forums | Help | Site Map

Generating HTML from python

Philippe C. Martin
Guest
 
Posts: n/a
#1: Jul 19 '05
Hi,

I wish to use an easy way to generate reports from wxPython and feel
wxHtmlEasyPrinting could be a good solution.

I now need to generate the HTML wxHtmlEasyPrinting can print: I need to have
a title followed by lines of text that do not look too ugly. If possible I
would like to use an existing module.

Q1) Is there such a module ?
Q2) Is my approach fairly good ?

Regards,

Philippe




Philippe C. Martin
Guest
 
Posts: n/a
#2: Jul 19 '05

re: Generating HTML from python


PS: I am looking at the formatter module which seems to be related to HTML
somehow, but without any code sample I'm a bit lost


Philippe C. Martin wrote:
[color=blue]
> Hi,
>
> I wish to use an easy way to generate reports from wxPython and feel
> wxHtmlEasyPrinting could be a good solution.
>
> I now need to generate the HTML wxHtmlEasyPrinting can print: I need to
> have a title followed by lines of text that do not look too ugly. If
> possible I would like to use an existing module.
>
> Q1) Is there such a module ?
> Q2) Is my approach fairly good ?
>
> Regards,
>
> Philippe[/color]

Michele Simionato
Guest
 
Posts: n/a
#3: Jul 19 '05

re: Generating HTML from python


You could generate your report in reStructuredText
format (Google is your friend) and then convert
them in HTML, PS, PDF, etc.

Michele Simionato

Thomas Guettler
Guest
 
Posts: n/a
#4: Jul 19 '05

re: Generating HTML from python


Am Thu, 09 Jun 2005 12:43:19 +0000 schrieb Philippe C. Martin:
[color=blue]
> Hi,
>
> I wish to use an easy way to generate reports from wxPython and feel
> wxHtmlEasyPrinting could be a good solution.
>
> I now need to generate the HTML wxHtmlEasyPrinting can print[/color]

I don't know wxPython, but generating HTML is very easy.

Some people say mixing HTML and programming code is not good.

But if you want to create dynamic pages with more logic than HTML
this is the best solution.

Example: Multiplication Table

rows=[]
heading=[]
for i in range(1, 11):
heading.append('<th bgcolor="grey">%s</th>' % i)
cols=[]
for j in range(1, 11):
cols.append('<td align="right">%s</td>' % (i*j))
row='<tr><th bgcolor="grey">%s</th>%s</tr>' % (i, ''.join(cols))
rows.append(row)
html="""
<html>
<head><title>Multiplication Table</title></head>
<body>
<table border="1">
<tr>
<th>&nbsp;</th> %s
</tr>
%s
</table>
</body>
</html> """ % (''.join(heading), ''.join(rows))

I guess this looks more ugly in most template languages.

HTH,
Thomas

--
Thomas Güttler, http://www.thomas-guettler.de/


Philippe C. Martin
Guest
 
Posts: n/a
#5: Jul 19 '05

re: Generating HTML from python


I'll take a pick thanks - I like the fact it's buit-in (no extra
installation)



Michele Simionato wrote:
[color=blue]
> You could generate your report in reStructuredText
> format (Google is your friend) and then convert
> them in HTML, PS, PDF, etc.
>
> Michele Simionato[/color]

Philippe C. Martin
Guest
 
Posts: n/a
#6: Jul 19 '05

re: Generating HTML from python


Thanks a bunch,

I'm currently playing with HTMLGen (great but not in Python distrib ...) and
it look very good - Yet your code example looks simple enough for me to
look at that alternative.




Thomas Guettler wrote:
[color=blue]
> Am Thu, 09 Jun 2005 12:43:19 +0000 schrieb Philippe C. Martin:
>[color=green]
>> Hi,
>>
>> I wish to use an easy way to generate reports from wxPython and feel
>> wxHtmlEasyPrinting could be a good solution.
>>
>> I now need to generate the HTML wxHtmlEasyPrinting can print[/color]
>
> I don't know wxPython, but generating HTML is very easy.
>
> Some people say mixing HTML and programming code is not good.
>
> But if you want to create dynamic pages with more logic than HTML
> this is the best solution.
>
> Example: Multiplication Table
>
> rows=[]
> heading=[]
> for i in range(1, 11):
> heading.append('<th bgcolor="grey">%s</th>' % i)
> cols=[]
> for j in range(1, 11):
> cols.append('<td align="right">%s</td>' % (i*j))
> row='<tr><th bgcolor="grey">%s</th>%s</tr>' % (i, ''.join(cols))
> rows.append(row)
> html="""
> <html>
> <head><title>Multiplication Table</title></head>
> <body>
> <table border="1">
> <tr>
> <th>&nbsp;</th> %s
> </tr>
> %s
> </table>
> </body>
> </html> """ % (''.join(heading), ''.join(rows))
>
> I guess this looks more ugly in most template languages.
>
> HTH,
> Thomas
>[/color]

Cappy2112
Guest
 
Posts: n/a
#7: Jul 19 '05

re: Generating HTML from python



I looked at HTMLGen a while ago- I didn't see what the advantage was.
I wrote soem code similar to the example above, to generate a page..
It worked out fine.

However, I want to add HTML ouput to many of my other python programs,
and I don't want to re-write this for each program. So some higher
level of abastraction is needed, but I don't know how just yet.

HTMLGen is no longer maintained, so I don't know what the best choice
is.

If you know the basics of HTMl, you can write a simple class to write
the html, and just call a method from your program, so your app does
not have any HTML in it.


Philippe C. Martin wrote:[color=blue]
> Thanks a bunch,
>
> I'm currently playing with HTMLGen (great but not in Python distrib ...) and
> it look very good - Yet your code example looks simple enough for me to
> look at that alternative.
>
>
>
>
> Thomas Guettler wrote:
>[color=green]
> > Am Thu, 09 Jun 2005 12:43:19 +0000 schrieb Philippe C. Martin:
> >[color=darkred]
> >> Hi,
> >>
> >> I wish to use an easy way to generate reports from wxPython and feel
> >> wxHtmlEasyPrinting could be a good solution.
> >>
> >> I now need to generate the HTML wxHtmlEasyPrinting can print[/color]
> >
> > I don't know wxPython, but generating HTML is very easy.
> >
> > Some people say mixing HTML and programming code is not good.
> >
> > But if you want to create dynamic pages with more logic than HTML
> > this is the best solution.
> >
> > Example: Multiplication Table
> >
> > rows=[]
> > heading=[]
> > for i in range(1, 11):
> > heading.append('<th bgcolor="grey">%s</th>' % i)
> > cols=[]
> > for j in range(1, 11):
> > cols.append('<td align="right">%s</td>' % (i*j))
> > row='<tr><th bgcolor="grey">%s</th>%s</tr>' % (i, ''.join(cols))
> > rows.append(row)
> > html="""
> > <html>
> > <head><title>Multiplication Table</title></head>
> > <body>
> > <table border="1">
> > <tr>
> > <th>&nbsp;</th> %s
> > </tr>
> > %s
> > </table>
> > </body>
> > </html> """ % (''.join(heading), ''.join(rows))
> >
> > I guess this looks more ugly in most template languages.
> >
> > HTH,
> > Thomas
> >[/color][/color]

Kent Johnson
Guest
 
Posts: n/a
#8: Jul 19 '05

re: Generating HTML from python


Philippe C. Martin wrote:[color=blue]
> Hi,
>
> I wish to use an easy way to generate reports from wxPython and feel
> wxHtmlEasyPrinting could be a good solution.
>
> I now need to generate the HTML wxHtmlEasyPrinting can print: I need to have
> a title followed by lines of text that do not look too ugly. If possible I
> would like to use an existing module.
>
> Q1) Is there such a module ?
> Q2) Is my approach fairly good ?[/color]

There are many ways to do this. The simplest is just to mix HTML and Python code in your own module, as Thomas has shown. This is quick and easy to do but IMO it doesn't scale well to large pages or large number of pages; the mix of raw HTML and Python code is hard to work with.

This page lists many alternatives: http://wiki.python.org/moin/WebProgramming

The interesting categories from that page:
"Templating Engines" provide a way to describe the HTML that is more-or-less independent of the code. They vary widely in style. If you want a lot of control over the generated HTML you probably want to use one of these.

"HTML Shorthand Processors" provide an alternate way to specify markup and a program to convert the markup to HTML. reStructuredText is in this category. To use one of these you would still have to generate the alternate markup in your program similar to the simple HTML method.

"HTML Generation class libraries" are libraries that make it easier to create HTML programmatically. This can be a good approach if the HTML is simple. I guess you can add many Python XML libraries to this category as well; for example you could use ElementTree to generate a model of an XHTML page and output it.

Choosing within the categories depends a lot on personal preference, you just have to find a package whose style you like and whose features fit your needs.

HTH,
Kent
Philippe C. Martin
Guest
 
Posts: n/a
#9: Jul 19 '05

re: Generating HTML from python


Thanks

Kent Johnson wrote:
[color=blue]
> Philippe C. Martin wrote:[color=green]
>> Hi,
>>
>> I wish to use an easy way to generate reports from wxPython and feel
>> wxHtmlEasyPrinting could be a good solution.
>>
>> I now need to generate the HTML wxHtmlEasyPrinting can print: I need to
>> have a title followed by lines of text that do not look too ugly. If
>> possible I would like to use an existing module.
>>
>> Q1) Is there such a module ?
>> Q2) Is my approach fairly good ?[/color]
>
> There are many ways to do this. The simplest is just to mix HTML and
> Python code in your own module, as Thomas has shown. This is quick and
> easy to do but IMO it doesn't scale well to large pages or large number of
> pages; the mix of raw HTML and Python code is hard to work with.
>
> This page lists many alternatives:
> http://wiki.python.org/moin/WebProgramming
>
> The interesting categories from that page:
> "Templating Engines" provide a way to describe the HTML that is
> more-or-less independent of the code. They vary widely in style. If you
> want a lot of control over the generated HTML you probably want to use one
> of these.
>
> "HTML Shorthand Processors" provide an alternate way to specify markup and
> a program to convert the markup to HTML. reStructuredText is in this
> category. To use one of these you would still have to generate the
> alternate markup in your program similar to the simple HTML method.
>
> "HTML Generation class libraries" are libraries that make it easier to
> create HTML programmatically. This can be a good approach if the HTML is
> simple. I guess you can add many Python XML libraries to this category as
> well; for example you could use ElementTree to generate a model of an
> XHTML page and output it.
>
> Choosing within the categories depends a lot on personal preference, you
> just have to find a package whose style you like and whose features fit
> your needs.
>
> HTH,
> Kent[/color]

Walter Dörwald
Guest
 
Posts: n/a
#10: Jul 19 '05

re: Generating HTML from python


Cappy2112 wrote:[color=blue]
> I looked at HTMLGen a while ago- I didn't see what the advantage was.
> I wrote soem code similar to the example above, to generate a page..
> It worked out fine.
>
> However, I want to add HTML ouput to many of my other python programs,
> and I don't want to re-write this for each program. So some higher
> level of abastraction is needed, but I don't know how just yet.
>
> HTMLGen is no longer maintained, so I don't know what the best choice
> is.[/color]

If you want an alternative to HTMLGen that is still maintained, you
might want to try XIST (http://www.livinglogic.de/Python/xist)

A few simple examples can be found here:
http://www.livinglogic.de/Python/xist/Examples.html

Bye,
Walter Dörwald
Harry George
Guest
 
Posts: n/a
#11: Jul 19 '05

re: Generating HTML from python


"Philippe C. Martin" <philippe@philippecmartin.com> writes:
[color=blue]
> PS: I am looking at the formatter module which seems to be related to HTML
> somehow, but without any code sample I'm a bit lost[/color]


As others have noted, if you need any computation at all, it is easier
to write directly in python.

I came to python from perl, where I used CGI.pm. To get that effect,
I wrote my own CGIpm.py and used it for a while.
http://www.seanet.com/~hgg9140/comp/index.html

But (at the suggestion of others in this newsgroup), I then tried
writing directly. The net effect is trivial html generation, with all
the power of python at your fingertips.

Note:
To save even more time, I made a CGI template that includes this main:

#============================
if __name__=="__main__":
mystart()
#cgi.print_environ_usage()
#cgi.print_environ()
form = cgi.FieldStorage()
try:
if len(form)==0:
send_form1()
else:
form_name=form['form_name'].value
if form_name=='form1':
recv_form1()
except StandardError, e:
print "\n<BR>ERROR: %s\n" % e
myend()

To support a stateless world:

1. Each form has a send_xyz and recv_xyz function. The end of each
recv_xyz decides what send_xyz to do next.

2. mystart and myend handle opening and closing the http and html.
They also handle state save/restore as needed (pickle or database).
[color=blue]
>
>
> Philippe C. Martin wrote:
>[color=green]
> > Hi,
> >
> > I wish to use an easy way to generate reports from wxPython and feel
> > wxHtmlEasyPrinting could be a good solution.
> >
> > I now need to generate the HTML wxHtmlEasyPrinting can print: I need to
> > have a title followed by lines of text that do not look too ugly. If
> > possible I would like to use an existing module.
> >
> > Q1) Is there such a module ?
> > Q2) Is my approach fairly good ?
> >
> > Regards,
> >
> > Philippe[/color]
>[/color]

--
harry.g.george@boeing.com
6-6M21 BCA CompArch Design Engineering
Phone: (425) 294-4718
Philippe C. Martin
Guest
 
Posts: n/a
#12: Jul 19 '05

re: Generating HTML from python


Thanks


Walter Dörwald wrote:
[color=blue]
> Cappy2112 wrote:[color=green]
>> I looked at HTMLGen a while ago- I didn't see what the advantage was.
>> I wrote soem code similar to the example above, to generate a page..
>> It worked out fine.
>>
>> However, I want to add HTML ouput to many of my other python programs,
>> and I don't want to re-write this for each program. So some higher
>> level of abastraction is needed, but I don't know how just yet.
>>
>> HTMLGen is no longer maintained, so I don't know what the best choice
>> is.[/color]
>
> If you want an alternative to HTMLGen that is still maintained, you
> might want to try XIST (http://www.livinglogic.de/Python/xist)
>
> A few simple examples can be found here:
> http://www.livinglogic.de/Python/xist/Examples.html
>
> Bye,
> Walter Dörwald[/color]

Philippe C. Martin
Guest
 
Posts: n/a
#13: Jul 19 '05

re: Generating HTML from python


PS: Just wanted to add that HTMLGen works very well and outputs html that
wxHtmlEasyPrinting and my email client have not problem reading (I output
student grades, missing assignments, ... in tables).

The one gitch is they do not have any installation program (that I've seen)
for windows.

Regards,

Philippe




Walter Dörwald wrote:
[color=blue]
> Cappy2112 wrote:[color=green]
>> I looked at HTMLGen a while ago- I didn't see what the advantage was.
>> I wrote soem code similar to the example above, to generate a page..
>> It worked out fine.
>>
>> However, I want to add HTML ouput to many of my other python programs,
>> and I don't want to re-write this for each program. So some higher
>> level of abastraction is needed, but I don't know how just yet.
>>
>> HTMLGen is no longer maintained, so I don't know what the best choice
>> is.[/color]
>
> If you want an alternative to HTMLGen that is still maintained, you
> might want to try XIST (http://www.livinglogic.de/Python/xist)
>
> A few simple examples can be found here:
> http://www.livinglogic.de/Python/xist/Examples.html
>
> Bye,
> Walter Dörwald[/color]

Roger Binns
Guest
 
Posts: n/a
#14: Jul 19 '05

re: Generating HTML from python



"Philippe C. Martin" <philippe@philippecmartin.com> wrote in message news:HPWpe.2034$I14.173@newssvr12.news.prodigy.com ...[color=blue]
> Hi,
>
> I wish to use an easy way to generate reports from wxPython and feel
> wxHtmlEasyPrinting could be a good solution.
>
> I now need to generate the HTML wxHtmlEasyPrinting can print: I need to have
> a title followed by lines of text that do not look too ugly. If possible I
> would like to use an existing module.
>
> Q1) Is there such a module ?
> Q2) Is my approach fairly good ?[/color]

There is one huge problem with wxHtml and consequently wxHtmlEasyPrinting. It
doesn't understand stylesheets in any way. There are many packages that
generate HTML, but they all expect a lot of the styling to be handled by
CSS. It is also a lot easier to write that way.

I would absolutely love a piece of code that could take CSS and HTML and
apply the CSS to the HTML to get the right appearance, but without any
CSS left. For example it would turn CSS formatting for class/div/span
into HTML attributes, as well as overal formatting instructions.

I did do something similar for BitPim but it requires the HTML to be perfect
and the CSS is actually a Python data structure expressing the attributes and
how they get applied. I'd love to replace it with something better.

But ultimately it does work.

Roger



Magnus Lycka
Guest
 
Posts: n/a
#15: Jul 19 '05

re: Generating HTML from python


Philippe C. Martin wrote:[color=blue]
> I now need to generate the HTML wxHtmlEasyPrinting can print: I need to have
> a title followed by lines of text that do not look too ugly. If possible I
> would like to use an existing module.[/color]

How to do this really depends on what your data looks like, and how you
get it. E.g. if you just want uniform pages with paragraphs of plain
texts with headings in between, you can just make a template HTML file
with the main block of text replaced with %s, and then do something like:

text = []

for heading, paragraph in data_source:
text.append('<h2>%s</h2>' % heading)
text.append(paragraph)

templ = open('template.html').read()
print templ % '\n'.join(text)


If your data has more structure, you might find a tool like Fredrik
Lundh's elementtree useful.
Closed Thread


Similar Python bytes