By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,767 Members | 1,303 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,767 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a

Hi,

Look at http://cheetahtemplate.org, you'll have the possibility to put
python code into the template and have automatic and elegant cache...

This page describe it and show very clear examples
http://cheetahtemplate.org/docs/user...g.regions.html

It's just an example, there are others possibility, with many web
frameworks.
http://www.python.org/cgi-bin/moinmoin/WebProgramming
Robert Ferber <ro*@nospam.net> writes:
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


--
Wilk - http://flibuste.net
Jul 18 '05 #2

P: n/a
Robert Ferber wrote:
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.


There are a number of templating systems available in Python. EmPy is
one of them:

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

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ A wise man never loses anything if he have himself.
-- Montaigne
Jul 18 '05 #3

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Fri, 16 Apr 2004 12:38:16 +0200
Robert Ferber <ro*@nospam.net> wrote:

[...]
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)

iD8DBQFAf8Dc2wyvT2WDeWYRAnL4AJ9ZwW2VVNDwmliE5y2mZj Oh1nayUgCgtezc
SaYvtxHSFSDSqb8H5goGpQM=
=ZOzo
-----END PGP SIGNATURE-----
Jul 18 '05 #4

P: n/a
Robert Ferber wrote:
[...]

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


Here a page with link to several template system in python :
http://webware.sourceforge.net/Papers/Templates/

--
Yermat

Jul 18 '05 #5

P: n/a
>>>>> "rob" == Robert Ferber <ro*@nospam.net> writes:

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/2...ver_pages.html

A teaser:

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

--
Ville Vainio http://tinyurl.com/2prnb
Jul 18 '05 #6

P: n/a
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

Jul 18 '05 #7

P: n/a
Robert Ferber <ro*@nospam.net> wrote in message news:<5e************@pilz.hasos.com>...

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


Funny you ask. See here:

http://www.onlamp.com/pub/a/python/2...ver_pages.html

Enjoy!

-Ruben
Jul 18 '05 #8

P: n/a
On Fri, Apr 16, 2004 at 12:38:16PM +0200, Robert Ferber wrote:
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
--
Marcel Molina Jr. <ma****@vernix.org>

Jul 18 '05 #9

P: n/a
has
Robert Ferber <ro*@nospam.net> wrote in message news:<5e************@pilz.hasos.com>...
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.s...ltemplate.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.
Jul 18 '05 #10

P: n/a
Thomas Guettler wrote:
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?


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.
--
Harte Arbeit zahlt sich später mal aus. Faulheit sofort!

Jul 18 '05 #11

P: n/a
Ville Vainio wrote:
>> "rob" == Robert Ferber <ro*@nospam.net> writes:


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/2...ver_pages.html


This looks very promising, thanks a lot.
--
"Statistics are like bikinis. What they reveal is suggestive,
but what they conceal is vital" - Aaron Levenstein

Jul 18 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.