Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

newbie : using python to generate web-pages

0 views
Skip to first unread message

biner

unread,
Nov 28, 2003, 1:57:31 PM11/28/03
to
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!

Ali El Dada

unread,
Nov 28, 2003, 2:05:11 PM11/28/03
to
i think you want HTMLgen from:

http://starship.python.net/crew/friedrich/HTMLgen/html/main.html

worked fine for me :)

cheers,
ali dada

Rene Pijlman

unread,
Nov 28, 2003, 2:38:03 PM11/28/03
to
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/

--
René Pijlman

GerritM

unread,
Nov 28, 2003, 2:43:43 PM11/28/03
to
"Ali El Dada" <eld...@mdstud.chalmers.se> schreef in bericht
news:3FC79C67...@mdstud.chalmers.se...

> i think you want HTMLgen from:
>
> http://starship.python.net/crew/friedrich/HTMLgen/html/main.html
>
> worked fine for me :)

I fully agree. The entire Gaudi site at:
<www.extra.research.philips.com/natlab/sysarch/>
is generated by means of Python+HTMLgen

regards Gerrit


Ville Vainio

unread,
Nov 28, 2003, 3:17:03 PM11/28/03
to
Rene Pijlman <reply.in.th...@my.address.is.invalid> writes:

Quoting:
> For advanced templating tasks, I recommend Cheetah (available at
> http://www.cheetahtemplate.org)."

And I recommend EmPy.

http://www.alcyone.com/software/empy/

It appears to be more Pythonic and elegant to me (disclaimer: I've
only looked at cheetah docs, not tried it).

Shameless plug: also check out my very own pywiz, a prompting system
for EmPy, at:

http://www.students.tut.fi/~vainio24/pywiz/


--
Ville Vainio http://www.students.tut.fi/~vainio24

Logan

unread,
Nov 28, 2003, 3:19:56 PM11/28/03
to
On Fri, 28 Nov 2003 10:57:31 -0800, biner wrote:
> 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.


--
mailto: logan@phreaker(NoSpam).net

Robert Brewer

unread,
Nov 28, 2003, 3:38:15 PM11/28/03
to pytho...@python.org
Logan wrote:
> 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.)

And if you're going to use named placeholders, you could just use named
substitution (rather than writing your own parser to look for xx__xx):

html = """
<html>
<head>
<title>%(Title)s</title>
</head>
<body>
%(Body)s
</body>
</html>
"""

print html % ({"Title": "The Title", "Body": "Content Area"})

Robert Brewer
MIS
Amor Ministries
fuma...@amor.org

Bryan

unread,
Nov 28, 2003, 5:57:42 PM11/28/03
to

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

bryan

Eric Baker

unread,
Nov 28, 2003, 10:33:36 PM11/28/03
to

> 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


0 new messages