keeps changing the color used in the main window, and another that keeps expanding
the main window label’s font. Be careful if you do run this, though; the colors flash,
and the label font gets bigger 10 times per second, so be sure you are able to kill the
main window before it gets away from you. Hey—I warned you!
Step 6: Adding a Web Interface
GUI interfaces are easier to use than command lines and are often all we need to simplify
access to data. By making our database available on the Web, though, we can open it
up to even wider use. Anyone with Internet access and a web browser can access the
data, regardless of where they are located and which machine they are using. Anything
from workstations to cell phones will suffice. Moreover, web-based interfaces require
only a web browser; there is no need to install Python to access the data except on the
single-server machine. Although traditional web-based approaches may sacrifice some
of the utility and speed of in-process GUI toolkits, their portability gain can be
compelling.
As we’ll also see later in this book, there are a variety of ways to go about scripting
interactive web pages of the sort we’ll need in order to access our data. Basic server-
side CGI scripting is more than adequate for simple tasks like ours. Because it’s perhaps
the simplest approach, and embodies the foundations of more advanced techniques,
CGI scripting is also well-suited to getting started on the Web.
For more advanced applications, a wealth of toolkits and frameworks for Python—
including Django, TurboGears, Google’s App Engine, pylons, web2py, Zope, Plone,
Twisted, CherryPy, Webware, mod_python, PSP, and Quixote—can simplify common
tasks and provide tools that we might otherwise need to code from scratch in the CGI
world. Though they pose a new set of tradeoffs, emerging technologies such as Flex,
Silverlight, and pyjamas (an AJAX-based port of the Google Web Toolkit to Python,
and Python-to-JavaScript compiler) offer additional paths to achieving interactive or
dynamic user-interfaces in web pages on clients, and open the door to using Python in
Rich Internet Applications (RIAs).
I’ll say more about these tools later. For now, let’s keep things simple and code a CGI
script.
CGI Basics
CGI scripting in Python is easy as long as you already have a handle on things like
HTML forms, URLs, and the client/server model of the Web (all topics we’ll address
in detail later in this book). Whether you’re aware of all the underlying details or not,
the basic interaction model is probably familiar.
In a nutshell, a user visits a website and receives a form, coded in HTML, to be filled
out in his or her browser. After submitting the form, a script, identified within either
52 | Chapter 1: A Sneak Preview