468,738 Members | 1,772 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,738 developers. It's quick & easy.

Any python HTML generator libs?

Hi, everyone. Simply put, what I need most now is a python lib to
generate simple HTML.

I am now using XML to store my lab report records. I found python
really convinient to manipulate XML, so I want to make a small on-line
CGI program to help my colleagues to build their lab report records
into XML, for storage, HTML display (for others to browse) and search.

With python's standard lib, the DOM object could realize the XML
storage and search quite easily, but for HTML generation, it is a
great headache.

I tried to turn to the in-line server-side python script PSP(something
like asp and php) instead of CGI. However, since the report data is
always of very complex data structures, it is really hard to write most
things in-line. For example, a PCR reaction is expected to be shown in
this format or alike on a web browser:

PCR
Sample: Sm1032
Operater: SullivanZ
TimeStamp: hh:mm mm-dd-yyyy
Reaction:
Reagent1:
Name:XXXX
Concentration:XXXX mM
Volumn:XXX uL
Reagent2:
.........................
.........................

Since there are hundreds of PCR reaction and other operations in the
lab report, in-line PSP is not a suitable solution. But writing HTML
directly with print statement is a great pain.

Will XSTL be useful? Is my problem somewho related with XML-SIG?
Looking forward to your precious suggestion.

Mar 9 '06 #1
10 3079
Will XSTL be useful? Is my problem somewho related with XML-SIG?
Looking forward to your precious suggestion.

XSLT is powerful but a royal pain in the arse. Just writing some Python
to generate your HTML would probably be a lot easier for you.

--
Michael McGlothlin, tech monkey
Tub Monkey
http://www.tubmonkey.com/

Mar 9 '06 #2
Sullivan WxPyQtKinter napisa³(a):
Hi, everyone. Simply put, what I need most now is a python lib to
generate simple HTML.

I am now using XML to store my lab report records. I found python
really convinient to manipulate XML, so I want to make a small on-line
CGI program to help my colleagues to build their lab report records
into XML, for storage, HTML display (for others to browse) and search.

With python's standard lib, the DOM object could realize the XML
storage and search quite easily, but for HTML generation, it is a
great headache.


I use HTMLTemplate + ElementTree combo to generate static HTML documents
from data in XML files. Other way might be using "object oriented XSL",
as ll-xist is often advertized.

--
Jarek Zgoda
http://jpa.berlios.de/
Mar 9 '06 #3
Sullivan WxPyQtKinter wrote:
Hi, everyone. Simply put, what I need most now is a python lib to
generate simple HTML.

I am now using XML to store my lab report records. I found python
really convinient to manipulate XML, so I want to make a small on-line
CGI program to help my colleagues to build their lab report records
into XML, for storage, HTML display (for others to browse) and search.

With python's standard lib, the DOM object could realize the XML
storage and search quite easily, but for HTML generation, it is a
great headache.

I tried to turn to the in-line server-side python script PSP(something
like asp and php) instead of CGI. However, since the report data is
always of very complex data structures, it is really hard to write most
things in-line. For example, a PCR reaction is expected to be shown in
this format or alike on a web browser:

PCR
Sample: Sm1032
Operater: SullivanZ
TimeStamp: hh:mm mm-dd-yyyy
Reaction:
Reagent1:
Name:XXXX
Concentration:XXXX mM
Volumn:XXX uL
Reagent2:
........................
........................

Since there are hundreds of PCR reaction and other operations in the
lab report, in-line PSP is not a suitable solution. But writing HTML
directly with print statement is a great pain.

Will XSTL be useful? Is my problem somewho related with XML-SIG?
Looking forward to your precious suggestion.

The triple-quoted string with string substitution is your friend. Try
writing something(s) like:

results = {'secnum': 1, 'type': 'string', 'color': 'blue'}

print """\
<h1>Section %(secnum)s</h1>
<p>Elements of type %(type)s should be coloured %(color)s</p>
""" % results

Alternatively you might want to take a look at the HTMLgen module. It's
old, but still usable.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd www.holdenweb.com
Love me, love my blog holdenweb.blogspot.com

Mar 9 '06 #4
Sorry I am completely a green-hand in HTML. What is HTMLTemplate and
ElementTree? Would you please post some source code as an example?

Of course I would Google them to find out more.

Thank you so much.

Mar 9 '06 #5
That lib can help.
But still, I have to code a lot using that lib. Maybe my program is
quite strange, far from common.

Thank you, after all~!

Mar 9 '06 #6

Sullivan WxPyQtKinter wrote:
Hi, everyone. Simply put, what I need most now is a python lib to
generate simple HTML.

I am now using XML to store my lab report records. I found python
really convinient to manipulate XML, so I want to make a small on-line
CGI program to help my colleagues to build their lab report records
into XML, for storage, HTML display (for others to browse) and search.

With python's standard lib, the DOM object could realize the XML
storage and search quite easily, but for HTML generation, it is a
great headache.

I tried to turn to the in-line server-side python script PSP(something
like asp and php) instead of CGI. However, since the report data is
always of very complex data structures, it is really hard to write most
things in-line. For example, a PCR reaction is expected to be shown in
this format or alike on a web browser:

PCR
Sample: Sm1032
Operater: SullivanZ
TimeStamp: hh:mm mm-dd-yyyy
Reaction:
Reagent1:
Name:XXXX
Concentration:XXXX mM
Volumn:XXX uL
Reagent2:
........................
........................

Since there are hundreds of PCR reaction and other operations in the
lab report, in-line PSP is not a suitable solution. But writing HTML
directly with print statement is a great pain.

Will XSTL be useful? Is my problem somewho related with XML-SIG?
Looking forward to your precious suggestion.


If you don't mind a learning curve, you could use a CherryPy/Picket
combination - Picket is an XSLT 'filter' that uses 4Suite to transform
XML on the server:

http://www.cherrypy.org/wiki/Picket

XSLT is powerful but a bit...wearying, at first.

Gerard

Mar 9 '06 #7
Sullivan WxPyQtKinter a écrit :
Hi, everyone. Simply put, what I need most now is a python lib to
generate simple HTML.

I am now using XML to store my lab report records. I found python
really convinient to manipulate XML, so I want to make a small on-line
CGI program to help my colleagues to build their lab report records
into XML, for storage, HTML display (for others to browse) and search.

With python's standard lib, the DOM object could realize the XML
storage and search quite easily, but for HTML generation, it is a
great headache.


You may want to have a look at Kid:
http://kid.lesscode.org/trac/wiki/Si...ocessingRecipe
Mar 9 '06 #8

Jarek Zgoda wrote:
I use HTMLTemplate + ElementTree combo to generate static HTML documents
from data in XML files. Other way might be using "object oriented XSL",
as ll-xist is often advertized.


HTMLTemplate + ElementTree works for me too. Additionally I use CSS
(Cascading Style Sheets) to add style (e.g. fonts, colors and spacing). The
CSS also allows for different styles for display/print (e.g. not printing
menus). If you want to see artistic CSS google for "css Zen Garden" .

Nearly two years after initially using HTMLTemplate, the python code,
templates and css are easy to maintain. Whereas some code/markup written
with a python HTML generator is difficult to maintain.

Stephen D Evans
Mar 9 '06 #9
HTMLTemplate + ElementTree works for me too. Additionally I use CSS
(Cascading Style Sheets) to add style (e.g. fonts, colors and spacing). The
CSS also allows for different styles for display/print (e.g. not printing
menus). If you want to see artistic CSS google for "css Zen Garden" .

Nearly two years after initially using HTMLTemplate, the python code,
templates and css are easy to maintain. Whereas some code/markup written
with a python HTML generator is difficult to maintain.

CSS is awesome. It can lead to some real headaches when it comes to IE
though if you're doing anything very complex. Firefox, Safari, and Opera
are almost identical when it comes to CSS support now which makes life a
lot easier. I suggest using Javascript behaviors too whenever you need
to use Javascript. Pretty handy because it keeps your HTML clean and
lets you apply code in almost identical way to CSS.

I know of a UI tool (for Java on mobile devices) that lets you style the
UI of normal apps with CSS. That'd rock if it was available for Python
programs.

--
Michael McGlothlin, tech monkey
Tub Monkey
http://www.tubmonkey.com/

Mar 9 '06 #10
Steve Holden wrote:
Sullivan WxPyQtKinter wrote:
Hi, everyone. Simply put, what I need most now is a python lib to
generate simple HTML.

I am now using XML to store my lab report records. I found python
really convinient to manipulate XML, so I want to make a small on-line
CGI program to help my colleagues to build their lab report records
into XML, for storage, HTML display (for others to browse) and search.

With python's standard lib, the DOM object could realize the XML
storage and search quite easily, but for HTML generation, it is a
great headache.

I tried to turn to the in-line server-side python script PSP(something
like asp and php) instead of CGI. However, since the report data is
always of very complex data structures, it is really hard to write most
things in-line. For example, a PCR reaction is expected to be shown in
this format or alike on a web browser:

PCR
Sample: Sm1032
Operater: SullivanZ
TimeStamp: hh:mm mm-dd-yyyy
Reaction:
Reagent1:
Name:XXXX
Concentration:XXXX mM
Volumn:XXX uL
Reagent2:
........................
........................

Since there are hundreds of PCR reaction and other operations in the
lab report, in-line PSP is not a suitable solution. But writing HTML
directly with print statement is a great pain.

Will XSTL be useful? Is my problem somewho related with XML-SIG?
Looking forward to your precious suggestion.


The triple-quoted string with string substitution is your friend. Try
writing something(s) like:

results = {'secnum': 1, 'type': 'string', 'color': 'blue'}

print """\
<h1>Section %(secnum)s</h1>
<p>Elements of type %(type)s should be coloured %(color)s</p>
""" % results


Don't forget that you may need to escape the application's data for
inclusion in HTML:

results = {'secnum': 1, 'type': 'string', 'color': 'blue',
'user':'Matt Goodall <ma**@pollenation.net>'}

print """\
<h1>Section %(secnum)s</h1>
<p>Elements of type %(type)s should be coloured %(color)s</p>
<p>Contributed by: %(user)s</p>
""" % results

Will print:

<h1>Section 1</h1>
<p>Elements of type string should be coloured blue</p>
<p>Contributed by: Matt Goodall <ma**@pollenation.net></p>

The '<' and '>' surrounding my email address breaks the HTML.

To fix that you need to escape results['user'] with cgi.escape or
xml.sax.saxutils.escape. Oh, and don't forget to escape anything
destined for an HTML attribute differently, see sax.saxutils.quoteattr.

A triple-quoted string is beautifully simple but it's not quite as much
a friend as it might initially seem. ;-)

I don't intend to get into a XML- vs text- based templating flame war
;-) but, IMHO, the solution is to use a templating language that
understands where the value is used in the template.

Kid is a great example of an XML-based templating language but there are
many others. Some have probably been mentioned in this thread already.

Another interesting solutions is to use something like Nevow's tags module:

from nevow import flat, tags as T

results = {'secnum': 1, 'type': 'string', 'color': 'blue',
'user':'Matt Goodall <ma**@pollenation.net>'}

doc = T.div[
T.h1['Section ', results['secnum']],
T.p['Elements of type ', results['type'],
' should be coloured ', results['color']],
T.p['Contributed by: ', results['user']],
]

print flat.flatten(doc)

This time you get valid HTML with no effort whatsoever:

<div><h1>Section 1</h1><p>Elements of type string should be coloured
blue</p><p>Contributed by: Matt Goodall
&lt;ma**@pollenation.net&gt;</p></div>

You even get to write HTML in a slightly more Pythonic way (even if it
does abuse Python just a little :wink:), but Nevow will happily load a
template containing actual XHTML from disk if you prefer.

The only real "problem" using Nevow for this is that you will need to
install Twisted too. I suspect you'll find a couple of Nevow tag
implementations that don't need Twisted if you ask Google.

Anyway! This was just to demonstrate an alternate approach than to
evangelise about Nevow. I hope it was at least interesting. :)

Cheers, Matt

Nevow:
http://divmod.org/trac/wiki/DivmodNevow
Twisted:
http://twistedmatrix.com/trac

--
__
/ \__ Matt Goodall, Pollenation Internet Ltd
\__/ \ w: http://www.pollenation.net
__/ \__/ e: ma**@pollenation.net
/ \__/ \ t: +44 (0)113 2252500
\__/ \__/
/ \ Any views expressed are my own and do not necessarily
\__/ reflect the views of my employer.
Mar 10 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Mikey At Work | last post: by
reply views Thread by Premshree Pillai | last post: by
1 post views Thread by Peter Newman | last post: by
2 posts views Thread by wipit | last post: by
4 posts views Thread by Pierre Imbaud | last post: by
4 posts views Thread by JediKnight83 | last post: by
5 posts views Thread by Johannes Bauer | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.