473,396 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

embedding python in HTML

I'm hoping someone can give me the basics for how to do very simple
things with Python scripts from within my HTML. For example, I know that
I can do this in PHP:

<h1>Here is a header</h1>
<?php include("file.html"); ?> // include some external html
<p>More html</p>

So to do this with Python, do I simply integrate it into the HTML as
above, with no extra steps? Also, which symbols do I use to denote
Python language?

Thanks.
Feb 16 '06 #1
13 9072
John Salerno wrote:
I'm hoping someone can give me the basics for how to do very simple
things with Python scripts from within my HTML. For example, I know that
I can do this in PHP:

<h1>Here is a header</h1>
<?php include("file.html"); ?> // include some external html
<p>More html</p>

So to do this with Python, do I simply integrate it into the HTML as
above, with no extra steps? Also, which symbols do I use to denote
Python language?

Thanks.


Also, do I need to give the html file an extension of .py?
Feb 16 '06 #2
John Salerno:
[Python alternative for PHP]
So to do this with Python, do I simply integrate it into the HTML as
above, with no extra steps?


You'd need something like the PHP engine, that understands Python rather
than PHP.

I've used Cheetah:
http://www.cheetahtemplate.org/

Our BDFL seems to prefer Django:
http://www.artima.com/weblogs/viewpo...?thread=146606

There's also PSP:
http://www.ciobriefings.com/psp/

And many more:
http://wiki.python.org/moin/WebProgramming

--
René Pijlman
Feb 16 '06 #3
Rene Pijlman wrote:
John Salerno:
[Python alternative for PHP]
So to do this with Python, do I simply integrate it into the HTML as
above, with no extra steps?


You'd need something like the PHP engine, that understands Python rather
than PHP.


My web server can run Python, fortunately. Now that they've turned it on
for me, I wanted to try it out, but I didn't know how to go about
writing a bit of code to stick into an HTML file.
Feb 16 '06 #4
Rene Pijlman wrote:
John Salerno:
[Python alternative for PHP]
So to do this with Python, do I simply integrate it into the HTML as
above, with no extra steps?
You'd need something like the PHP engine, that understands Python rather
than PHP.


[...]
There's also PSP:
http://www.ciobriefings.com/psp/


For "straight Python Server Pages" without advertising various (and
possibly quite different) technologies, take a look at mod_python's
implementation:

http://www.modpython.org/
http://www.modpython.org/live/curren...pyapi-psp.html

I haven't actually used this particular implementation - I'm not a fan
of embedding programming languages in HTML - but I imagine that it's
one of the more actively developed projects of its kind.

Paul

Feb 16 '06 #5
John Salerno wrote:
Rene Pijlman wrote:
John Salerno:
[Python alternative for PHP]
So to do this with Python, do I simply integrate it into the HTML as
above, with no extra steps?

You'd need something like the PHP engine, that understands Python rather
than PHP.

My web server can run Python, fortunately. Now that they've turned it on
for me, I wanted to try it out, but I didn't know how to go about
writing a bit of code to stick into an HTML file.


You've got to understand that Python is *not* a 'ServerPage' language
(-> php, asp, jsp etc) in itself. Your server can now run python, fine,
but *how* ? CGI ? FastCGI ? mod_python ? other ? (hint: it's probably
just plain old CGI...)
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Feb 16 '06 #6
bruno at modulix wrote:
You've got to understand that Python is *not* a 'ServerPage' language
(-> php, asp, jsp etc) in itself. Your server can now run python, fine,
but *how* ? CGI ? FastCGI ? mod_python ? other ? (hint: it's probably
just plain old CGI...)


So does that mean I need to have something further on the server? Or is
this something I can do on my end? How do I find out what I need?
Feb 17 '06 #7
John Salerno wrote:
bruno at modulix wrote:
You've got to understand that Python is *not* a 'ServerPage' language
(-> php, asp, jsp etc) in itself. Your server can now run python, fine,
but *how* ? CGI ? FastCGI ? mod_python ? other ? (hint: it's probably
just plain old CGI...)

So does that mean I need to have something further on the server? Or is
this something I can do on my end? How do I find out what I need?


If you really want to use Python as a server page language, mod_python
has support for Python Server Pages via its PSP handler:

Python Server Pages:
http://modpython.org/live/current/do...pyapi-psp.html

PSP handler:
http://modpython.org/live/current/do.../hand-psp.html

This of course means your server needs to have mod_python installed and
configured. (Consult your server administrator.) However, I've always
found PSP to be somewhat fiddly, and mixing any serious code with the
HTML text is hardly pretty.

A more common (and bare-metal) approach is CGI. In CGI, a request for a
page runs a script, the output of which is the HTML page. I think this
only requires that the server has Python installed, which you have said
is the case. Python has signifigant standard library support for writing
CGI.

You should examine Python's standard cgi module:
http://python.org/doc/2.4.2/lib/module-cgi.html
That page also has some nice examples to get you started.

And maybe its Cookie module, if you ever feel like messing with cookies:
http://python.org/doc/2.4.2/lib/module-Cookie.html

Slightly less bare-metal is using mod_python directly (rather than via
its PSP module). This is probably preferable to plain CGI if mod_python
is available, as it caches scripts as long as they are not changed. This
is faster than reading them off the disk every time. By and large,
mod_python's API replaces (or at least wraps) the standard library's CGI
support if you go this route. Again, this is only available if your
server has mod_python installed, which may or may not be the case.
Feb 17 '06 #8
Kirk McDonald wrote:
A more common (and bare-metal) approach is CGI. In CGI, a request for a
page runs a script, the output of which is the HTML page. I think this
only requires that the server has Python installed, which you have said
is the case. Python has signifigant standard library support for writing
CGI.


Thanks, that makes much more sense to me now. But does this mean I can
still write HTML normally? What would an example be of having HTML
within a Python script? I have a hard time picturing this, because I
imagine that most of my pages will be almost all HTML, with just a bit
of Python here and there, perhaps to insert headers and footers. Is all
the HTML just wrapped in a big print statement, or something like that?
Feb 17 '06 #9
John Salerno wrote:
Kirk McDonald wrote:
A more common (and bare-metal) approach is CGI. In CGI, a request for a
page runs a script, the output of which is the HTML page. I think this
only requires that the server has Python installed, which you have said
is the case. Python has signifigant standard library support for writing
CGI.


Thanks, that makes much more sense to me now. But does this mean I can
still write HTML normally? What would an example be of having HTML
within a Python script? I have a hard time picturing this, because I
imagine that most of my pages will be almost all HTML, with just a bit
of Python here and there, perhaps to insert headers and footers. Is all
the HTML just wrapped in a big print statement, or something like that?


When writing for CGI, it will be.

It will basically look like this:

#!/bin/env python

# these are custom headers, Content-type is mandatory
print "Content-Type: text/html"
# an empty line separates headers from content
print

print "<html>..."
# do stuff here
print "...</html>"

Georg
Feb 17 '06 #10
In article <bp********************@rcn.net>,
John Salerno <jo******@NOSPAMgmail.com> wrote:
bruno at modulix wrote:
You've got to understand that Python is *not* a 'ServerPage' language
(-> php, asp, jsp etc) in itself. Your server can now run python, fine,
but *how* ? CGI ? FastCGI ? mod_python ? other ? (hint: it's probably
just plain old CGI...)


So does that mean I need to have something further on the server? Or is
this something I can do on my end? How do I find out what I need?


While others have provided salient technical details to
you, my recommendation is to start with your provider.
Say to him, "when you tell me you've 'turned Python on',
what exactly does that mean? Have you configured Web
service for CGI? Zope? ..." This is a case where it's
good to talk to a person.
Feb 17 '06 #11
bruno at modulix wrote:
John Salerno wrote:
Rene Pijlman wrote:

John Salerno:
[Python alternative for PHP]
So to do this with Python, do I simply integrate it into the HTML as
above, with no extra steps?
You'd need something like the PHP engine, that understands Python rather
than PHP.

My web server can run Python, fortunately. Now that they've turned it on
for me, I wanted to try it out, but I didn't know how to go about
writing a bit of code to stick into an HTML file.

You've got to understand that Python is *not* a 'ServerPage' language
(-> php, asp, jsp etc) in itself. Your server can now run python, fine,
but *how* ? CGI ? FastCGI ? mod_python ? other ? (hint: it's probably
just plain old CGI...)

It's not an Active Scripting language by default after installation of
the win32all extensions, but it can be made one, giving it access to
Request, Response and the other usual suspects in the ASP environment.

This wouldn't be my preferred way to use it, but (for example) it allows
you to include Python sources in VBScript pages and have your VBScript
code call Python functions and procedures. This alone is sometimes
worthwhile.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Feb 18 '06 #12
Rene Pijlman wrote:
There's also PSP:
http://www.ciobriefings.com/psp/


Another incarnation of PSP can be used as part of Webware for Python
(http://www.w4py.org).

And one of the more modern solutions that should be mentioned is Kid
(http://kid.lesscode.org).

-- Christoph
Feb 18 '06 #13
John Salerno wrote:
Thanks, that makes much more sense to me now. But does this mean I can
still write HTML normally? What would an example be of having HTML
within a Python script? I have a hard time picturing this, because I
imagine that most of my pages will be almost all HTML, with just a bit
of Python here and there, perhaps to insert headers and footers. Is all
the HTML just wrapped in a big print statement, or something like that?


Imagine for instance, that you have an HTML file where you
want to print a current timestamp when the page is displayed.

A simple way to do this would be to just give your HTML file
another extension (e.g. .tmpl, short for template). Keep the
file as it is, just put the text

%(timestamp)s

in the place(s) where you want your timestamp to appear
in the HTML file.

In your CGI script you can then do something like this:

#!/usr/bin/python -u
import time
print "Content-type: text/html\n"
text = open('myfile.tmpl).read()
print text % ('timestamp':time.asctime())

The inital Content-type line is important, and it must be
followed by a blank line before the actual content.

Look at the cgitb module too.

Instead of the common Python % interpolation, you could use
string.Template (with a current Python) or one of the many
templating systems around. Since your needs are likely to
grow, you might also want to have a look at one of the many
tool kits for Python and the web. Right now, it seems that
django and turbogears are the most popular. Cherrypy and
web.py are somewhat smaller and simpler systems. Unless you
use one of these tool kits, your homegrown code might turn
into yet another web tool kit eventually, and we have enough
of them already... (Too many I'd say...)

You should also note that traditional CGI scripts are rather
slow with Python, since Python's startup time is significant.
A system where the Python interpreter is already running, as
mod_python embedded in Apache is faster. But by all means, try
it as CGI. It might well be enough for your needs. It's been
ok for me.
Feb 22 '06 #14

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

Similar topics

0
by: Atul Kshirsagar | last post by:
I am embedding python in my C++ application. I am using Python *2.3.2* with a C++ extention DLL in multi-threaded environment. I am using SWIG-1.3.19 to generate C++ to Python interface. Now to...
4
by: Alicia Haumann | last post by:
I accidentally sent this to webmaster@python.org, so this could be a duplicate if "webmaster" forwards it to this list. :{ Hi, there. Thanks for any help that can be offered. I've been...
2
by: Roose | last post by:
With some googling I have found these resources: http://docs.python.org/ext/win-dlls.html http://www.python.org/doc/faq/windows.html I have a large Win32/MFC/C/C++ application that has an...
8
by: Thomas Bartkus | last post by:
Name: lib64python2.4-devel Summary: The libraries and header files needed for Python development Description: The Python programming language's interpreter can be extended with dynamically...
2
by: John Dean | last post by:
Hi Could somebody, please tell me where I can find information about embedding Python into a C/C++ application. The example in the docs is rather simple. I am looking for something a bit more...
6
by: Qun Cao | last post by:
Hi Everyone, I am a beginner on cross language development. My problem at hand is to build a python interface for a C++ application built on top of a 3D game engine. The purpose of this python...
4
by: David Abrahams | last post by:
I'm seeing highly surprising (and different!) behaviors of PyImport_ImportModule on Linux and Windows when used in a program with python embedding. On Linux, when attempting to import a module...
3
by: anonymisiert85 | last post by:
At the moment i can run python-string-code from C (MinGW, WinXP) But how can i register a C-function in python-RUNTIME and call this C function from python - without wrapper dll's or libs??? ...
1
by: Thomas Troeger | last post by:
Dear all, I've successfully embedded the Python interpreter into a set of C/C++ application programs that use a larger library project with information from http://docs.python.org/api/api.html...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.