(For gurus) Embed functions in webpages like in PHP?

Robert Ferber rob at nospam.net
Fri Apr 16 06:38:16 EDT 2004


Hi,

I'm  a PHP-programmer who evaluates Python for a new project.
I really like a lot of concepts of Python, especially the shell, but there
is one great feature of PHP which I don't know how to replace in Python:

For database-intensive webpages, I like to cache the HTML-output on the fly
in the filesystem and put out the file later whenever the same URL is
requested again, for example:

/cache/some_url:
<html>
important database result that took 20 minutes to generate
</html>

and:
/www/master_script.php:
<?
if (is_file("/cache/$URL")) include("/cache/$URL");
else include("/scripts/soo_long_and_complicated.php");
?>

So far so good, shouldn't be too much of problem to do something similar in
Python.

However, it's sometimes necessary to still access the database, for example
to rotate banners, show user-specified style-sheets, change output
depending on the used browser, etc.
In PHP it's trivial to do this, instead of caching the output of a function,
I just cache "<? function(); ?>", so the example looks like this:

<html>
<? echo random_banner(); ?>
important database result that took 20 minutes to generate
</html>

I've used this technique for about a year now and it works great for
semi-static pages (which are manually changed) or some not too fast
changing dynamic pages, in some cases I reduced the database-load to 20%.
The beauty of it is that I can use the same "random_banner_()" function for
the cache and on the fly.

As far as I've seen, there is no way to embed Python-code in a webpage with
"<? ?>" or anything equivalent and I don't see any other way to solve this
problem.

But maybe somebody else does, I'd apreceate all ideas,

Thanks a lot

-- 
"I don't suffer from insanity, I enjoy every minute of it" 
 - Calvin




More information about the Python-list mailing list