views:

58

answers:

2

Hi, I am having some trouble generating a web form using fields specified in an ascii file. What I want to do is the following: 1) Read in the ascii file. This is of the form (can have N elements):

Object1 value1

Object2 value2

Object3 value3

Object4 value4

...

2) Generate a web form from the ascii file contents. Each line in the ascii file should represent a form checkbox option.

...

3) Display the form at a specific url.

4) On "submit", call a cgi script to process the form.

My problem is in step 2. I can easily generate a static form with fixed values and save it as a standard html form but I need something that will read in the ascii file and generate the html file on the fly when visiting that url.

Any advice on what the easiest way of doing this is?

A: 

Simplest way to set up HTTP service in Python: get CherryPy ( http://www.cherrypy.org )

For example, this program:

import cherrypy

class HelloWorld(object):
    def index(self):
        return "Hello World!"
    index.exposed = True

cherrypy.quickstart(HelloWorld())

Sets up a web server on http://127.0.0.1/8080/ and prints "Hello World!" when opened. Just return your custom HTML instead and you're ready :)

Alternatively, if you want to host the form at a specific server, you can use the python ftplib module to put it there.

wump
A: 

Given that you cite step 2 (template processing) as your primary concern, you should look into one of python's templating libraries. The good ones (that I can remember right now) are Mako, Genshi, Jinja and Cheetah. Python's website has a whole list of them: http://wiki.python.org/moin/Templating
I personally find Mako easy to use, but others might be better suited to your particular problem.
You might also want to look at a web application framework, which will have templating plus the cgi stuff described in your 3rd and 4th steps. Again, check out python's website for details and options.

yarmiganosca