Any python HTML generator libs?

  • Thread starter Sullivan WxPyQtKinter
  • Start date
S

Sullivan WxPyQtKinter

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.
 
M

Michael

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.
 
J

Jarek Zgoda

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.
 
S

Steve Holden

Sullivan said:
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
 
S

Sullivan WxPyQtKinter

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.
 
S

Sullivan WxPyQtKinter

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~!
 
G

Gerard Flanagan

Sullivan said:
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
 
S

Stephen D Evans

Jarek said:
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
 
M

Michael

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.
 
B

Bruno Desthuilliers

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/SimpleXmlDocumentProcessingRecipe
 
M

Matt Goodall

Steve said:
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 <[email protected]>'}

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 <[email protected]></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 <[email protected]>'}

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;[email protected]&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: (e-mail address removed)
/ \__/ \ t: +44 (0)113 2252500
\__/ \__/
/ \ Any views expressed are my own and do not necessarily
\__/ reflect the views of my employer.
 

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
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top