473,698 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.h tml"); ?> // 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 9097
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.h tml"); ?> // 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

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

Similar topics

0
1842
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 explain it in details, 1. Python initialization and finalization is done in the *main* thread. 2. For each new thread I create a separate sub-interpreter . 3. Using PyRun_String("import myModule"...) before execution of python
4
2781
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 working with Python for a year or more now, but only doing simple extending in C/C++. I'm now attempting some embedding and several questions have come to mind. BTW - I'm running Windows 2000 with Python23 and VisualC++ developers
2
2866
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 embedded scripting language (a very limited one). I would like to rip it out and replace it with Python. I am thinking that this would be relatively simple since the scripting language is a very small interface between the UI and the engine --...
8
1823
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 loaded extensions and can be embedded in other programs. This package contains the header files and libraries needed to do these types of tasks. ------------------------------------------------------
2
1985
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 complex and longer -- Best Regards John
6
3001
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 interface is providing a convenient scripting toolkit for the application. One example is that a user can write a python script like: player = Player() game.loadPlayer(player) player.moveTo(location)
4
5685
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 xxx that's in the current directory, I get ImportError: No module named xxx I can work around the problem by setting
3
1670
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??? STEPS: initialize python regsiter foo() ########### don't know how to do this run python-script "c=foo(a,b)"
1
1342
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 and http://docs.python.org/ext/ext.html. Now I want to wrap classes and functions from the associated libraries so that I can write new applications completely in Python, but I'm not entirely sure how to start because I have some problems...
0
8598
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
9014
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...
0
8855
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
7708
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4612
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3037
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
2320
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1995
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.