[Tutor] Database driven web sites with python

Magnus Lycka magnus@thinkware.se
Sat Jan 18 18:20:02 2003


At 19:12 2003-01-18 +0100, Charlie Clark wrote:
>I've just looked at some of the CherryPy documentation. I don't think it
>would be for me (too much code mixed in the HTML) which isn't to say its
>bad!!!

*I* think mixing HTML and code is typically bad, but having looked
at a lot of stuff, CherryPy is the package I like best in this field.
You CAN mix HTML and code, although in a fairly clean way, but you
don't have to, just forget the masks, and use views that read in
HTML files with your CHTL tags. This is certainly a better way of
working if HTML content is produced by someone who is not a
programmer.

In the simplest case just use something like:
view:
     def index(self):
         return file('index.html').read()

In many real cases I'd let HTML writers write complete HTML files,
but then I'd place the "meat" of that file in a different context.
Something like:

view:
     def index(self):
         raw = file('index.html').read()
         bodyPattern = re.compile(r'<body>(.*)</body>', re.DOTALL)
         body = bodyPattern.findall(raw)[0]
         return self.makePage('index', body)

where self.makePage would contain something like
     def makePage(pageName, content)
         return '<html>%s%s%s</html>' % (self.getHeader(pageName),
                                         content, self.getFooter())

Another good thing is that it's fairly small. This means that there
are not so many concepts that have to be mastered at once. It also
fits in the way I want to work, assembling small components that
follow the old unix tenet of doing one thing and doing it well.
WebWare for instance tries to be a web tool, a maiddleware and
a lot of other things as well. This means that I get in trouble
if I want to move my app from a web UI to a GUI or if I decide
that one of the components don't quite fit.

Another thing I like with CherryPy is the templating language
CHTL which puts all the dynamic data in attributes. This means
that the guy who writes HTML can use sample data in his files,
and see what the page will look like in a web browser without
running the application. He can simply open his HTML file in
Mozilla or whatever. When it's running in the application, his
sample data will be replaced by live data. The attributes can
also survive most HTML editors, so an HTML file can go back and
forth between HTML writer and Python coder without things getting
messed up. (Unless the HTML writer is stupid enough to erase
those funny attributes like py-eval that he doesn't understand,
but he won't see them in WYSIWYG mode...)

What I dislike a bit, is that it's a python pre processor. What
you write is almost Python, and it slightly violates python syntax,
although that is only in very specific locations. If I remember
correctly, there are also stricter rules on whitespace in CherryPy,
but I'm sure that could be fixed fairly easily if someone really
cared.

A more serious problem is that as far as I understand it has to be
run like a long running process, and I don't always have the ability
to set up such processes on web sites I work with.

>Marcus' objection

Who's Marcus? ;)

>about storage is now only partly true. You can now have
>stuff saved in files or CVS though none of the solutions are entirely
>stable.

Right... As in "Except that it doesn't work, how do you like this
software?" ;)

>I really like being able (re)organise parts of the site as it
>develops: Zope is quite nice when you change things and I've become a big
>fan of PageTemplates. But it has all taken time. Expect to need about 6
>weeks before feeling comfortable with Zope

I always gave up faster than that every time I tried. :(

Other Zope critisism can be found at
http://www.amk.ca/python/writing/why-not-zope.html

>because it is an application
>server and can be compared with the big boys. If you enjoy working on
>source then you might enjoy joining in Zope 3 development which is going to
>tidy up a lot of the things that have been put in over time.

That's what I'm waiting for. Well, I'll probably wait for 3.1...

For the past two years, I've used the object database in Zope,
ZODB, and it's served us well I think.

>You might also want to look at the eGenix Web Application server.
>http://www.egenix.com/
>It has rock-solid database support and a nice approach to embedding Python
>code in HTML and will have excellent web service support.

You mean if he wants to buy a service from Guru Lemburg? Or is
that available some other way?

"We currently do not market the eGenix Application Server as stand-alone
product. It is only available as part of solutions we provide to our
customers, such as the Siena Web Service Architecture where it is used for
the server side implementation of the service engine."


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se