Generating HTML from python

  • Thread starter Philippe C. Martin
  • Start date
P

Philippe C. Martin

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
 
P

Philippe C. Martin

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
 
M

Michele Simionato

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

Michele Simionato
 
T

Thomas Guettler

Am Thu, 09 Jun 2005 12:43:19 +0000 schrieb Philippe C. Martin:
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 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
 
P

Philippe C. Martin

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

Philippe C. Martin

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 said:
Am Thu, 09 Jun 2005 12:43:19 +0000 schrieb Philippe C. Martin:
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 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
 
C

Cappy2112

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.

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 said:
Am Thu, 09 Jun 2005 12:43:19 +0000 schrieb Philippe C. Martin:
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 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
 
K

Kent Johnson

Philippe said:
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 ?

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
 
P

Philippe C. Martin

Thanks

Kent said:
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
 
?

=?ISO-8859-1?Q?Walter_D=F6rwald?=

Cappy2112 said:
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 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
 
H

Harry George

Philippe C. Martin said:
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


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).
 
P

Philippe C. Martin

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
 
R

Roger Binns

Philippe C. Martin said:
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 ?

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
 
M

Magnus Lycka

Philippe said:
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.

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.
 

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
474,241
Messages
2,571,223
Members
47,856
Latest member
mmorais

Latest Threads

Top