2009 203

From XPUB & Lens-Based wiki

Page Generators & CGI

We have seen examples in ThinkPython of python programs that manipulate text.

It is now a small step to changing the output of our program from "plain text" to use some simple markup of HTML.

The next step is CGI!

Reading for Next Week

#!/usr/bin/python

import cgi

fs = cgi.FieldStorage()
text = fs.getvalue("text", "")

print "Content-type: text/html; charset=utf8"
print

print """<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>form building blocks</title>
</head>

<body>
<form action="" method="get">
"""


words = text.split()
newtext = " ".join(words[::-1])

print "<h1>" + newtext + "</h1>"

print """
<textarea name="text">
""" + text.strip() + """
</textarea>
"""

print """
<input type="submit" name="submitbutton" value="go!" />


<h2>Some starting options</h2>
<a href="?text=tomato" >tomato</a>
<a href="?text=once+upon+a+time" >once upon a time</a>



</form>
</body>
</html>

"""