By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,922 Members | 1,493 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,922 IT Pros & Developers. It's quick & easy.

Generating HTML from python

P: n/a
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

Jul 19 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
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:
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


Jul 19 '05 #2

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

Michele Simionato

Jul 19 '05 #3

P: n/a
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

--
Thomas GŁttler, http://www.thomas-guettler.de/
Jul 19 '05 #4

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

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

Michele Simionato


Jul 19 '05 #5

P: n/a
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:
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


Jul 19 '05 #6

P: n/a

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:
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:
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


Jul 19 '05 #7

P: n/a
Philippe C. Martin wrote:
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
Jul 19 '05 #8

P: n/a
Thanks

Kent Johnson wrote:
Philippe C. Martin wrote:
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


Jul 19 '05 #9

P: n/a
Cappy2112 wrote:
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
Jul 19 '05 #10

P: n/a
"Philippe C. Martin" <ph******@philippecmartin.com> writes:
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).


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


--
ha************@boeing.com
6-6M21 BCA CompArch Design Engineering
Phone: (425) 294-4718
Jul 19 '05 #11

P: n/a
Thanks
Walter Dörwald wrote:
Cappy2112 wrote:
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


Jul 19 '05 #12

P: n/a
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:
Cappy2112 wrote:
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


Jul 19 '05 #13

P: n/a

"Philippe C. Martin" <ph******@philippecmartin.com> wrote in message news:HP****************@newssvr12.news.prodigy.com ...
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

Jul 19 '05 #14

P: n/a
Philippe C. Martin wrote:
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.
Jul 19 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.