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

R

Robert Ferber

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
 
M

Milan =?ISO-8859-15?Q?Holz=E4pfel?=

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Fri, 16 Apr 2004 12:38:16 +0200

[...]
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'm sure you'll find something on the sites wilk posted, but I just
remembered that I recently read an article which is exactly this done
in: <URL:http://www-106.ibm.com/developerworks/web/library/us-gentoo/>
(scroll down to "Technology demo: pytext" if you don't want to read the
whole thing)

I think one should be able to use the "pytext interpreter" as
interpreter for a CGI script. (or use
"#!/usr/bin/python /path/to/pytext" as first line of the CGI script...
or do whatever, just in case one doesn't know :) )

(If anyone wants all four parts of the article nicely linked on one
page, here it is: <URL:http://www.gentoo.org/proj/en/site.xml>)

But I think you could also use a script which reads the database result
from a file and also does the banner rotation or whatever (so you don't
have your result in the script which is called, but you read the result
from a file nevertheless)

Regards,
Milan

- --


Milan Holzäpfel alias jagdfalke alias jag

Öff. GnuPG-Schlüssel / GnuPG Publiy Key
<URL:http://con.mjh.name/>

GnuPG Fingerabdruck / GnuPG Fingerprint
4C8A 5FAF 5D32 6125 89D1 0CE5 DB0C AF4F 6583 7966


Hinweis:
Die Domain jagdfalke.net gehört mir nicht mehr wirklich.
Deshalb bitte keine jagdfalke.net-eMail-Adressen mehr
benutzen. Neue Kontaktmöglichkeiten gibts auf
<URL:http://con.mjh.name/>, meine neue Domain ist mjh.name.

NOTE:
I don't really own the domain jagdfalke.net any logner.
Therefore don't use any jagdfalke.net eMail-addresses
any more. You can look at <URL:http://con.mjh.name/> for new
adresses, my new domain is mjh.name.


Hinweis 2:
Wenn sich hier nicht findet, was in der Absender-Zeile ver-
sprochen wurde, dann schau mal ein paar Zeilen weiter oben ;-)

Note 2:
If you can't find what I've promised in the From-line, then
then have a look at some of the lines above ;-)

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFAf8Dc2wyvT2WDeWYRAnL4AJ9ZwW2VVNDwmliE5y2mZjOh1nayUgCgtezc
SaYvtxHSFSDSqb8H5goGpQM=
=ZOzo
-----END PGP SIGNATURE-----
 
V

Ville Vainio

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

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

Check out the new mod_python, it offers exactly the functionality you
need. Some discussion is here:

http://www.onlamp.com/pub/a/python/2004/02/26/python_server_pages.html

A teaser:

<html>
<%
import time
%>
<h1>Current time is
<%= time.ctime() %> </h1>
</html>
 
T

Thomas Guettler

Am Fri, 16 Apr 2004 12:38:16 +0200 schrieb Robert Ferber:
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:

The problem with caching: When is
the data outdate?

You could use this approach:
Run a cron-job every hour which does the complicated
query which needs long to compute. Dump
the result into a file (Look for "pickle"
in the module index of the docs).

Load this file for every request
and insert this into the template.

HTH,
Thomas
 
M

Marcel Molina Jr.

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,

there is also spyce, which is not a templating system but a way to embed
python in html a la php or mason, etc.

http://spyce.sf.net/

marcel
 
H

has

Robert Ferber said:
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.

If you're open to alternatives, there's also several simplified-DOM
templating engines:

http://www.entrian.com/PyMeld/

http://www.divmod.org/Home/Projects/Nevow/

http://freespace.virgin.net/hamish.sanderson/htmltemplate.html

I think Nevow's maybe still a work in progress, but the other two have
been around awhile. (I recommend HTMLTemplate myself, though may be
biased as it's one of mine.:)

These systems are much simpler and more flexible than traditional
'code-in-markup' systems, compiling an HTML template into a simple
object model that can be manipulated directly from Python code. Bit
different to what you're used to, but you really should give 'em a
look. Simple APIs, low cruft/bloat, and total separation between
presentation logic (in Python) and HTML markup. Can be used on their
own, or in conjunction with a web framework like Quixote.

Not sure of the others, but HTMLTemplate certainly allows you template
both full pages and HTML chunks so you can easily compose a page from
multiple parts, so implementing selective caching and reuse of
semi-static page areas should be straightforward.
 
R

Robert Ferber

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

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

Check out the new mod_python, it offers exactly the functionality you
need. Some discussion is here:

http://www.onlamp.com/pub/a/python/2004/02/26/python_server_pages.html

This looks very promising, thanks a lot.
 
R

Robert Ferber

Thomas said:
Am Fri, 16 Apr 2004 12:38:16 +0200 schrieb Robert Ferber:


The problem with caching: When is
the data outdate?

Currently I just use the file creation date. I set it for example 2 weeks
into the future and whenever the creation date is in the past it is
assumend out of date.
You could use this approach:
Run a cron-job every hour which does the complicated
query which needs long to compute. Dump
the result into a file (Look for "pickle"
in the module index of the docs).

That wouldn't work, in fact it would make matters worse.

First of all, I couldn't have different times for caches to get out of date,
then, more importantly, I'd have to run it for every possible combination
at once, for example for script.php?a=1&b=2&c=3, I'd have to do all
combinations for a, b and c which could lead to thousands, sometimes
millions of combinations.
Also, there is no way to know which values of a, b and c are even possible,
so I wouldn't even know what to generate.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top