Web devel with python. Whats the best route?

Sam Penrose spenrose at well.com
Thu Jan 11 01:09:57 EST 2001


In article <93hfp1013sm at news2.newsguy.com>, "Alex Martelli" 
<aleaxit at yahoo.com> wrote:

> > In contrast to Alex's comments, however, we do not place Python
> > statements within HTML. We stick with string substitution keys,
> 
> That's nice, but in my very first application I needed to
> generate a variable number of repetitions of a table-row,
> depending on values computed from the Python side of things.
> 
> Rather than reinvent loop-syntax &c, I found it simplest to
> embed a for statement in the HTML template, a la:
> 
> +for i in range(len(results)):
>     <TR> <TD><B>@i@</B></TD> <TD>@results[i]@</TD> </TR>
> -
> 
> All the presentation logic goes into the template, all of
> the computation into the Python CGI script, and my little
> yaptu.py (yet another python template utility, of course)
> bridges the gap (with about 50 SLOC's, plus comments,
> docstrings, and test pushing it to a bit over 100 lines).

1) There is an irreducible need to either put Python statements into 
HTML or HTML tidbits into Python. You've chosen the former; my 
preference is for the latter:

localList = []
row = '<TR> <TD><B>%s</B></TD> <TD>%s</TD> </TR>'
for i in range(len(results)):
   localList.append(row % (i, results[i]))
rows = string.join(localList, '')
print myTemplate % {'rows':rows}

Typically what I do is print a crude page with interwoven data, give it 
to an HTML specialist to make it look nice, and then dig out of their 
version a sample row with their contributions ('<td align = center 
height="17" bgcolor="blahblahblah">'), and swap string substitution 
markers back in for the data to get the final version of my variable.

2) You appear to use '+' and '@' (and '-'?) as non-Python, non-HTML 
markers to indicate the border between the two languages. One of the 
reasons I don't like systems involving executable language statements 
embedded in HTML, such as Zope, is that they all seem to require such 
markers. There are two costs associated with this:
   i. Yet Another Syntax to learn. Someone who knows Python and HTML 
(me, for example) will not know how the syntax works. It looks as though 
you are using the '+' (an otherwise valid HTML character) to indicate 
the beginning of a Python block and the paired '@' markers for string 
substitution--but there might be regular expressions involved. I can't 
tell.
   ii. Another layer of magic characters to parse in BOTH HTML and 
Python--quick, how do you tell the difference between the '@' marking a 
Python variable, the '@' in an email address and the '@' and end user 
typed as an abbreviation for 'at' in a data chunk that you are 
re-inserting into your template for some reason? What about '+' in URLs 
and Python statements? Presumbaly there are rules (Yet Another Syntax), 
conventions (documented or otherwise), workarounds for troublesome 
cases, etc., but you've inherently added a third layer of logic, whose 
interaction with the Python and HTML layers will, in non-trivial 
projects, require non-trivial management.

3) Those costs seem to have been worth it for you and many others 
(Digital Creations not least). I don't wish to argue with how others get 
their work done if that's what works for them. I do want to note that 
straight Python and HTML work fine for projects involving thousands of 
lines of Python in a couple dozen modules and hundreds of HTML files.



More information about the Python-list mailing list