473,799 Members | 3,822 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.p hp:
<?
if (is_file("/cache/$URL")) include("/cache/$URL");
else include("/scripts/soo_long_and_co mplicated.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
11 1763

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.p hp:
<?
if (is_file("/cache/$URL")) include("/cache/$URL");
else include("/scripts/soo_long_and_co mplicated.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
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
-----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öglichk eiten 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)

iD8DBQFAf8Dc2wy vT2WDeWYRAnL4AJ 9ZwW2VVNDwmliE5 y2mZjOh1nayUgCg tezc
SaYvtxHSFSDSqb8 H5goGpQM=
=ZOzo
-----END PGP SIGNATURE-----
Jul 18 '05 #4
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
>>>>> "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
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
Robert Ferber <ro*@nospam.net > wrote in message news:<5e******* *****@pilz.haso s.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
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
has
Robert Ferber <ro*@nospam.net > wrote in message news:<5e******* *****@pilz.haso s.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

77
5696
by: nospam | last post by:
Reasons for a 3-tier achitecture for the WEB? (NOTE: I said, WEB, NOT WINDOWS. DON'T shoot your mouth off if you don't understand the difference.) I hear only one reason and that's to switch a database from SQL Server to Oracle or DB2 or vice versa... and that's it.... And a lot of these enterprises don't need it as they already know what database they are going to use and they don't plan on switching in and out database in the first...
3
5222
by: Tony Sutton | last post by:
I have this following code in my html file: <object classid="clsid:166B1BCA-3F9C-11CF-8075-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab# version=7,0,0,0" width="320" height="320"> <param name="src" value="file.dcr" /> <embed src="file.dcr" pluginspage="http://www.macromedia.com/go/getflashplayer" width="320" height="320"> </embed>
9
6459
by: MTriggs886 | last post by:
There isn't a command that exists that can serve as an alternative to EMBED in HTML 4.01 Transitional coding, is there? I've historically used this command to play MIDI files in the background of my webpages, and I'm finding out now it's kind of obsolete, even though most browsers still accept it. Thanks, Michael Triggs
761
28978
by: Neo-LISPer | last post by:
Hey Recently, I researched using C++ for game programming and here is what I found: C++ game developers spend a lot of their time debugging corrupted memory. Few, if any, compilers offer completely safe modes. Unsurprisingly, there is a very high failure rate among projects using C++ for modern game development.
2
1867
by: Wezzy | last post by:
Hi, is there a tool that automatically expose an object to python? i have an instance of a C++ (or ObjC) object and i want to pass it to the embed interpreter that runs inside my program. Python code have to call c++ method and register some callback. I know that swig helps when python creates c++ instance but i've the instance and i want to use it with python. Thanks
1
3149
by: babylon | last post by:
if I have a Win32 DLL - X.DLL can I embed it to a .Net Application and invoke it's exported functions? normally i have to put the X.DLL in the file system and use DllImport to import/invoke the function I just wanna combine everything into a single .exe thank you
29
2911
by: gs | last post by:
let say I have to deal with various date format and I am give format string from one of the following dd/mm/yyyy mm/dd/yyyy dd/mmm/yyyy mmm/dd/yyyy dd/mm/yy mm/dd/yy dd/mmm/yy mmm/dd/yy
42
2812
by: =?Utf-8?B?UGxheWE=?= | last post by:
I have an if statement that isn't working correctly and I was wondering how I check for a blank string. My Code Example if me.fieldname(arrayIndex) = "" then ----- end if When I do this and there is no characters in the variable it does not enter my IF statement like I would think it should. How do I properly check for a
2
1979
by: hardieca | last post by:
Hi, I'd like to know if anyone knows of any resources detailing the best practices of validating rules in the business tier and providing helpful error messages to users in the UI tier. All the information I have come across seems to revolve around using validation controls on the webpages themselves, which is not always feasible. Regards,
0
9543
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10488
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10237
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10029
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4144
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.