embedding python in HTML

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Salerno

    #1

    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.
  • John Salerno

    #2
    Re: embedding python in HTML

    John Salerno wrote:[color=blue]
    > 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.[/color]

    Also, do I need to give the html file an extension of .py?

    Comment

    • Rene Pijlman

      #3
      Re: embedding python in HTML

      John Salerno:
      [Python alternative for PHP][color=blue]
      >So to do this with Python, do I simply integrate it into the HTML as
      >above, with no extra steps?[/color]

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

      I've used Cheetah:


      Our BDFL seems to prefer Django:


      There's also PSP:


      And many more:


      --
      René Pijlman

      Comment

      • John Salerno

        #4
        Re: embedding python in HTML

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

        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.

        Comment

        • Paul Boddie

          #5
          Re: embedding python in HTML

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

          [...]
          [color=blue]
          > There's also PSP:
          > http://www.ciobriefings.com/psp/[/color]

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




          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

          Comment

          • bruno at modulix

            #6
            Re: embedding python in HTML

            John Salerno wrote:[color=blue]
            > Rene Pijlman wrote:
            >[color=green]
            >> John Salerno:
            >> [Python alternative for PHP]
            >>[color=darkred]
            >>> So to do this with Python, do I simply integrate it into the HTML as
            >>> above, with no extra steps?[/color]
            >>
            >>
            >> You'd need something like the PHP engine, that understands Python rather
            >> than PHP.[/color]
            >
            >
            > 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.[/color]

            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@xiludom. gro'.split('@')])"

            Comment

            • John Salerno

              #7
              Re: embedding python in HTML

              bruno at modulix wrote:
              [color=blue]
              > 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...)[/color]

              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?

              Comment

              • Kirk McDonald

                #8
                Re: embedding python in HTML

                John Salerno wrote:[color=blue]
                > bruno at modulix wrote:
                >[color=green]
                >> 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...)[/color]
                >
                >
                > 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?[/color]

                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:


                PSP handler:


                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.

                Comment

                • John Salerno

                  #9
                  Re: embedding python in HTML

                  Kirk McDonald wrote:
                  [color=blue]
                  > 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.[/color]

                  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?

                  Comment

                  • Georg Brandl

                    #10
                    Re: embedding python in HTML

                    John Salerno wrote:[color=blue]
                    > Kirk McDonald wrote:
                    >[color=green]
                    >> 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.[/color]
                    >
                    > 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?[/color]

                    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

                    Comment

                    • Cameron Laird

                      #11
                      Re: embedding python in HTML

                      In article <bpCdne1jK5jC1G jeRVn-gw@rcn.net>,
                      John Salerno <johnjsal@NOSPA Mgmail.com> wrote:[color=blue]
                      >bruno at modulix wrote:
                      >[color=green]
                      >> 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...)[/color]
                      >
                      >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?[/color]

                      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.

                      Comment

                      • Steve Holden

                        #12
                        Re: embedding python in HTML

                        bruno at modulix wrote:[color=blue]
                        > John Salerno wrote:
                        >[color=green]
                        >>Rene Pijlman wrote:
                        >>
                        >>[color=darkred]
                        >>>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.[/color]
                        >>
                        >>
                        >>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.[/color]
                        >
                        >
                        > 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...)
                        >
                        >[/color]
                        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/

                        Comment

                        • Christoph Zwerschke

                          #13
                          Re: embedding python in HTML

                          Rene Pijlman wrote:[color=blue]
                          > There's also PSP:
                          > http://www.ciobriefings.com/psp/[/color]

                          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

                          Comment

                          • Magnus Lycka

                            #14
                            Re: embedding python in HTML

                            John Salerno wrote:[color=blue]
                            > 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?[/color]

                            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.tm pl).read()
                            print text % ('timestamp':ti me.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.

                            Comment

                            Working...