2009 203

From Media Design: Networked & Lens-Based wiki
Jump to navigation Jump to search

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


import cgi

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

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

print """<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

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

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

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

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

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

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>