embedding python in HTML

2006-02-17 Thread John Salerno
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:

Here is a header
// include some external html
More html

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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: embedding python in HTML

2006-02-16 Thread bruno at modulix
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 '[EMAIL PROTECTED]'.split('@')])"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: embedding python in HTML

2006-02-16 Thread John Salerno
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?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: embedding python in HTML

2006-02-16 Thread Rene Pijlman
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/viewpost.jsp?thread=146606

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

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

-- 
René Pijlman
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: embedding python in HTML

2006-02-17 Thread Paul Boddie
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/current/doc-html/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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: embedding python in HTML

2006-02-17 Thread Kirk McDonald
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/doc-html/pyapi-psp.html

PSP handler:
http://modpython.org/live/current/doc-html/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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: embedding python in HTML

2006-02-17 Thread John Salerno
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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: embedding python in HTML

2006-02-17 Thread John Salerno
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:
> 
> Here is a header
> // include some external html
> More html
> 
> 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?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: embedding python in HTML

2006-02-17 Thread John Salerno
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?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: embedding python in HTML

2006-02-17 Thread Georg Brandl
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 "..."
# do stuff here
print "..."

Georg
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: embedding python in HTML

2006-02-17 Thread Cameron Laird
In article <[EMAIL PROTECTED]>,
John Salerno  <[EMAIL PROTECTED]> 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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: embedding python in HTML

2006-02-18 Thread Steve Holden
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/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: embedding python in HTML

2006-02-18 Thread Christoph Zwerschke
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: embedding python in HTML

2006-02-22 Thread Magnus Lycka
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.
-- 
http://mail.python.org/mailman/listinfo/python-list