Is this roughly what you want to do?
import argparse
parser = argparse.ArgumentParser(description='sample parser')
arg1 = parser.add_argument('--foo')
print parser
print arg1
html = """
<form name="%(prog)s action="%(action)s" method="get">
%(description)s
<input type="text" name="%(dest)s"><br>
<input type="submit" value="Submit">
</form>
"""
d = vars(parser)
d.update(vars(arg1))
d['action'] = 'html_form.xxx'
print html%d
which displays:
ArgumentParser(prog='stack21586315.py', usage=None, description='sample parser',
version=None, formatter_class=<class 'argparse.HelpFormatter'>,
conflict_handler='error', add_help=True)
_StoreAction(option_strings=['--foo'], dest='foo', nargs=None, const=None,
default=None, type=None, choices=None, help=None, metavar=None)
<form name="stack21586315.py action="html_form.xxx" method="get">
sample parser
<input type="text" name="foo"><br>
<input type="submit" value="Submit">
</form>
I'm generating a dictionary from the attributes of the parser
and its action
, and using those to fill in slots in the form
string.
A basic WSGI function involves sending a response
via start_response
, and receiving a request
(here in the envirion
argument). Details with vary with the framework.
The url of this module (and function) is specified in the form's action
attribute. How the values are sent depends on the method
(get/post). The values are found in environ['QUERY_STRING']
. There are various tools for parsing this string, either in the cgi
or urlparse
modules (or see your framework). The parsed request should end up looking much like vars(args)
.
def my_send_app(environ, start_response):
status = '200 OK' # HTTP Status
headers = [('Content-type', 'text/plain')] # HTTP Headers
start_response(status, headers)
# The returned object is going to be printed
# create the HTML including the form; return as list of strings
return [...]
def my_receive_app(envirion, start_responce):
# Returns a dictionary containing lists as values.
d = urlparse.parse_qs(environ['QUERY_STRING'])
# d should be similar to vars(args)
# apply the values in d
# use start_responce to reply to the browser