2009 203: Difference between revisions

From XPUB & Lens-Based wiki
(New page: = 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"...)
 
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 6: Line 6:


The next step is CGI!
The next step is CGI!
* [http://pzwart2.wdka.hro.nl/~techday/pages/PythonCGIChecklist.html Python CGI checklist]
*[[Template.cgi]]
== Reading for Next Week ==
* http://www.oreillynet.com/pub/a/oreilly/tim/news/2005/09/30/what-is-web-20.html
<source lang="python">
#!/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>
"""
</source>

Latest revision as of 14:07, 6 February 2009

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>

"""