FAQ
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.

Search Discussions

  • John Salerno at Feb 16, 2006 at 8:54 pm

    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?
  • Rene Pijlman at Feb 16, 2006 at 9:10 pm
    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?thread6606

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

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

    --
    Ren? Pijlman
  • John Salerno at Feb 16, 2006 at 9:44 pm

    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.
  • Bruno at Feb 16, 2006 at 10:15 pm

    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 'onurb at xiludom.gro'.split('@')])"
  • John Salerno at Feb 17, 2006 at 3:55 am

    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?
  • Kirk McDonald at Feb 17, 2006 at 6:20 am

    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.
  • John Salerno at Feb 17, 2006 at 2:56 pm

    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?
  • Georg Brandl at Feb 17, 2006 at 3:00 pm

    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
  • Magnus Lycka at Feb 22, 2006 at 12:37 pm

    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.


    From http Wed Feb 22 14:09:06 2006
    From: http (Paul Rubin)
    Date: 22 Feb 2006 05:09:06 -0800
    Subject: Python vs. Lisp -- please explain
    References: <1140324074.458446.227480@z14g2000cwz.googlegroups.com>
    <Kq2dnZipbMY_nmXenZ2dnUVZ_tmdnZ2d@comcast.com>
    <43f8ba1e$0$2895$626a54ce@news.free.fr>
    <yfsmzgnp2ub.fsf@oc.ex.ac.uk>
    <43f8cb3f$0$4225$626a54ce@news.free.fr>
    <87bqx3glb1.fsf@wilson.homeunix.com>
    <43f984dc$0$27151$626a54ce@news.free.fr>
    <873bienwlx.fsf@wilson.homeunix.com> <43fbc788@usenet01.boi.hp.com>
    <87u0ar3gba.fsf@wilson.homeunix.com>
    <pan.2006.02.22.11.35.14.90948@REMOVETHIScyber.com.au>
    <1140612537.685150.68330@o13g2000cwo.googlegroups.com>
    Message-ID: <7xu0arbkwd.fsf@ruckus.brouhaha.com>

    "Kay Schluehr" <kay.schluehr at gmx.net> writes:
    I talked to Richard Emslie recently and he told me that the PyPy team
    works on a mechanism to create CPython-extension modules written in
    RPython i.e. a statically translateable subset of Python. So even
    without dynamic code specialization there will be an optimization path
    based on the PyPy toolchain that is amazing.
    Sounds great but is that a whole lot different from pyrex?
  • Cameron Laird at Feb 17, 2006 at 10:08 pm
    In article <bpCdne1jK5jC1GjeRVn-gw at rcn.net>,
    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?
    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.
  • Steve Holden at Feb 18, 2006 at 8:33 am

    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/
  • Paul Boddie at Feb 16, 2006 at 9:44 pm

    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
  • Christoph Zwerschke at Feb 18, 2006 at 12:20 pm

    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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedFeb 16, '06 at 8:53p
activeFeb 22, '06 at 12:37p
posts14
users10
websitepython.org

People

Translate

site design / logo © 2022 Grokbase