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

Peter van Kampen news at datatailors.xs4all.nl
Thu Apr 17 03:21:41 EDT 2003


In article <mailman.1050550654.3607.python-list at python.org>, 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 />
    <form>
    <input type="text" name="username" value="%(username)s" />
    <input type="password" name="password" value="%(password)s" />
    <input type="submit" name="submit" value="submit" />
    </form>
""" % (formvars)
            
template = """
    <html>
    <head>
    <title>%(title)s</title>
    %(meta)s
    <link rel="stylesheet" href="%(stylesheet)s">
    </head>
    <body>
    %(body)s
    </body>
    </html>
""" % (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.

Hth,

PterK

-- 
Peter van Kampen
pterk -- at -- datatailors.com




More information about the Python-list mailing list