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
<ma**@pollenation.net></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.