LetterType.cgi: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
Line 1: Line 1:
Example of a simple "typing" interface using CGI. Two variables are used for state, one to represent the "current word", and a second to represent the "current letter" to be typed.
Example of a simple "typing" interface using CGI. Two variables are used for state, one to represent the "current word", and a second to represent the "current letter" to be typed.


[[techdazecgi:lettertype.cgi]]
[[techdazecgi:Lettertype]]


<source lang="python">
<source lang="python">

Revision as of 17:42, 9 February 2009

Example of a simple "typing" interface using CGI. Two variables are used for state, one to represent the "current word", and a second to represent the "current letter" to be typed.

techdazecgi:Lettertype

#!/usr/bin/python
 
import cgi
import cgitb; cgitb.enable()
 
fs = cgi.FieldStorage()
curword = fs.getvalue("curword", "")
addletter = fs.getvalue("addletter", None)
 
if addletter:
  curword += addletter
 
print "Content-type: text/html; charset=utf8"
print
 
print "<html><head><title>lettertype</title></head><body>"

letters = "abcdefghijklmnopqrstuvwxyz"
print "<p>"
for l in letters:
  print """<a href="?curword=%s&addletter=%s">%s</a>""" % (curword, l, l)
print """<a href="?">clear</a>"""
print "</p>"

print "<h2>%s</h2>" % curword

print """
</body>
</html> 
"""

Note how the "clear" links works by clearing all state.

Exercise

Implement a "backspace" link.