HTML form pre-populating functionality or hints on building my own

Peter van Kampen news at
Thu Apr 17 09:21:41 CEST 2003

In article <mailman.1050550654.3607.python-list at>, Scott
Chapman wrote:
> Hi!
> I'm wanting to find (or make) a module that will take a HTML file, locate the 
> input-related form tags (<INPUT, <TEXTAREA, <SELECT, <OPTION) and modify them 
> by putting in values from a dictionary (i.e. pre-populate the form). I want a 
> module that is implemented 100% in python and does _not_ use any special tags 
> to do this (such as webware, albatross, spyce, etc.)

100% pure python...but it doesn't really 'locate' form-tags...

--------- cut here ---------------
#!/usr/bin/env python

templatevars = {}
templatevars['title'] = 'Demo'
templatevars['meta'] = ''
templatevars['stylesheet'] = 'demo.css'

formvars = {}
formvars['username'] = 'Scott'
formvars['password'] = ''

templatevars['body'] = """
    <div class="fancy">Python rocks!</div>
    <br />
    <input type="text" name="username" value="%(username)s" />
    <input type="password" name="password" value="%(password)s" />
    <input type="submit" name="submit" value="submit" />
""" % (formvars)
template = """
    <link rel="stylesheet" href="%(stylesheet)s">
""" % (templatevars)
print template

--------- cut here ---------------

> The only thing I've seen that does this is EmbPerl (I'm tired of Perl) and 
> Integratis' DFP module (implemented in C and not a straight-forward install).

I don't know EmbPerl (nor do I want to ;-)) but if the above isn't
what you mean maybe you can try to subclass the new HTMLParser module
that comes with python 2.2. It's surprisingly (to me anyway) easy to
use. You can feed it plain HTML (ie. read from a file or from urlopen)
and it will call handle_starttag functions where you can check for
input tags. It passes the attributes as a list of tuples so you have
to do little (or no) parsing yourself.



Peter van Kampen
pterk -- at --

More information about the Python-list mailing list