newbie : using python to generate web-pages

B

biner

Hello all,

I am taking up on the work of a coleague who developed a big perl
script to generate a bunch of html files. I am taking over his task
and I looked at his code. It is quite messy because he used simple
"print" command to print the html code into files so the script is a
mix of html an perl. Quite ugly.

I don't know much about perl but I know that there are html
shortcuts in the CGI module that could allow me to make the code more
readable. Something like "print em(toto)" that would print
"<em>toto</em>"

Is there anything like that in Python. I did not find anything yet.
I don't know much about python (even less than perl) but I hear good
things about it all the time so I would like to use this project to
get familiar with python.

Any help would be welcome.

Thanks.
Ciao!
 
R

Rene Pijlman

biner:
[Generating HTML]
Is there anything like that in Python.

"Python does not come with tools to generate HTML. If you want an advanced
framework for structured HTML generation, I recommend Robin Friedrich's
HTMLGen 2.2 (available at
http://starship.python.net/crew/friedrich/HTMLgen/html/main.html),
[...]
If your favorite approach is to embed Python code within HTML in the
manner made popular by JSP, ASP, and PHP, one possibility is to use Python
Server Pages (PSP) as supported by Webware, mentioned in Chapter 20.
Another package, focused more specifically on the embedding approach, is
Spyce (available at http://spyce.sf.net/).
[...]
For advanced templating tasks, I recommend Cheetah (available at
http://www.cheetahtemplate.org)."

Source: Python in a Nutshell By Alex Martelli
http://safari.oreilly.com/0596001886/pythonian-CHP-22-SECT-4

Based on this recommendation I've just started using Cheetah. It looks
like a great tool to me.
http://www.cheetahtemplate.org/
 
L

Logan

I am taking up on the work of a coleague who developed a big perl
script to generate a bunch of html files. I am taking over his task
and I looked at his code. It is quite messy because he used simple
"print" command to print the html code into files so the script is a
mix of html an perl. Quite ugly.

I don't know much about perl but I know that there are html
shortcuts in the CGI module that could allow me to make the code more
readable. Something like "print em(toto)" that would print
"<em>toto</em>"

I don't really think that 'print em(toto)' is more readable than
'print "<em>toto</em>"'. But that seems to be a matter of taste.
I would always prefer the second way of writing HTML because I
can see all the closing tags, indentation etc. Makes finding bugs
easier.
Is there anything like that in Python. I did not find anything yet.
I don't know much about python (even less than perl) but I hear good
things about it all the time so I would like to use this project to
get familiar with python.

If there are not many places in your HTML where you have to substitute
strings, then an approach like the following might be enough:

html = """
<html>
<head>
<title>%s</title>
</head>
<body>
%s
</body>
</html>
"""

print html % ("The Title", "Content Area")

(You could also use placeholders like xxTitlexx, xxContentxx in
the string 'html' and replace them to create the output.)

I think, the above solution is really 'readable' :)

If you have to generate 'complicated' HTML files (depending on
various options, with dynamic menus etc.), I would recommend to
use an XML/XSLT approach, combined with a Python script (e.g.
4Suite (Ft) or libxml2/libxslt; the first is a Python package,
the second has Python bindings and both provide nice and easy to
use XSLT-processors).

And finally, if you have to generate your HTML files dynamically
on a web server, you should use a totally different approach; but
that would be another thread :)

HTH, L.
 
B

Bryan

Rene said:
biner:
[Generating HTML]
Is there anything like that in Python.


"Python does not come with tools to generate HTML. If you want an advanced
framework for structured HTML generation, I recommend Robin Friedrich's
HTMLGen 2.2 (available at
http://starship.python.net/crew/friedrich/HTMLgen/html/main.html),
[...]
If your favorite approach is to embed Python code within HTML in the
manner made popular by JSP, ASP, and PHP, one possibility is to use Python
Server Pages (PSP) as supported by Webware, mentioned in Chapter 20.
Another package, focused more specifically on the embedding approach, is
Spyce (available at http://spyce.sf.net/).
[...]
For advanced templating tasks, I recommend Cheetah (available at
http://www.cheetahtemplate.org)."

Source: Python in a Nutshell By Alex Martelli
http://safari.oreilly.com/0596001886/pythonian-CHP-22-SECT-4

Based on this recommendation I've just started using Cheetah. It looks
like a great tool to me.
http://www.cheetahtemplate.org/

i also recommend looking at cheetah. i used it for a project at work and it was great.

bryan
 
E

Eric Baker

I don't really think that 'print em(toto)' is more readable than
'print "<em>toto</em>"'. But that seems to be a matter of taste.
I would always prefer the second way of writing HTML because I
can see all the closing tags, indentation etc. Makes finding bugs
easier.

True, using it like you descibed is not much of an innovation, but look at
this snippet:

import regsub, string, HTMLgen

doc = HTMLgen.SimpleDocument(title='Interrupts')
table = HTMLgen.Table(tabletitle='Interrupts',
border=2, width=100, cell_align="right",
heading=[ "Description", "IRQ", "Count" ])

table.body = [] # Empty list.

doc.append(table) # Add table to document.

interrupts_file = open('/proc/interrupts')

for line in interrupts_file.readlines():
data=regsub.split(string.strip(line),'[ :+]+')
table.body.append([ HTMLgen.Text(data[2]),data[0],data[1] ])

doc.write("interrupts.html")

Now that is much cleaner that a bunch of
print"<table><tr><td></td></tr></table>"

Eric
 

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

Forum statistics

Threads
474,173
Messages
2,570,938
Members
47,474
Latest member
VivianStuk

Latest Threads

Top