oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

What Is TurboGears (Hint: Python-Based Framework for Rapid Web Development)
Pages: 1, 2, 3, 4

A Quick Closer Look at the Stack

At the bottom layer of the stack is SQLObject. Essentially, this little marvel removes much of the burden of writing your own SQL queries, and instead, allows you to focus much more of your attention on the modeling process. The basic idea is that classes are flattened into tables and the attributes of the classes become table columns. An especially interesting property of SQLObject is that it tries to be as agnostic as possible with respect to the specific SQL database that you use underneath, so you could transition amongst SQLite and PostgreSQL without hassle.

Here's what's becoming the classic example for illustrating how easy it is to use SQLObject:

from sqlobject import *
from datetime import datetime
class Person(SQLObject):

    firstName = StringCol(length=100)
    middleInitial = StringCol(length=1, default=None)
    lastName = StringCol(length=100)
    lastContact = DateTimeCol(

That code looks remarkably similar to the following SQL query:

         first_name VARCHAR(100) NOT NULL,
         middle_initial CHAR(1),
         last_name VARCHAR(100) NOT NULL,
         last_contact TIMESTAMP NOT NULL

Sitting just above the SQLObject layer of the stack is CherryPy. If you've ever done any kind of server-side scripting before with a technology like servlets, you'll find CherryPy to be a very easy way to pump out dynamic content. Here's the classic "Hello World" where you can optionally set the query string parameter who in the URL to display something in the place of "World."

import cherrypy

class MyRoot:
    def index(self, who="World"):
        return "Hello, %s!" % (who)

It really doesn't get much simpler than that, and if you're already the least bit Python-savvy, you'll be able to surprise yourself with what you can get done with CherryPy. The performance isn't bad either; the server itself is capable of serving several hundred requests a second, which means that your performance is going to depend mostly on your application's specific code.

Kid is the next layer of the stack and provides a robust XML templating system that makes it easy to separate the content from the presentation of your pages, in addition to simplifying mundane tasks like building tables. For example, take a look at the following code to build a table using Kid:

    <tr py:for="gear in turbogears">
        <td><span py:content="">TurboGears</span></td>

That's easy enough. As you can see, templating engines fill the niche of transforming data that's churned out by an application into a nice presentable format via insertion into a spot template itself. Just think about how much easier that approach makes it to maintain complex hierarchies of documents on sites like eBay and Amazon.

Of course, CSS still styles the actual content that's pushed into the template, but the template itself provides the overall structure for the document. Templating systems provide one of those highly sought-after, win-win situations that allow designers to do what they do best (presentation) and developers to do what they do best (deliver content).

Finally, MochiKit tops off the TurboGears stack by providing a rich JavaScript library that you can use to work AJAX capabilities or those final user-interface tweaks into your application. If you've ever done much work with JavaScript, you already know that development often can be painful because of the lack of standard library functions. Fortunately for us, MochiKit significantly lessens the burden of that very problem, allowing us to focus our attention on more interesting things. Consider the following snippet of code for building a table using MochiKit:

    var rows = [
        ["dataA1", "dataA2", "dataA3"],
        ["dataB1", "dataB2", "dataB3"]
    row_display = function (row) {
        return TR(null, map(partial(TD, null), row));
    var newTable = TABLE({'class': 'prettytable'},
            row_display(["head1", "head2", "head3"])),
            row_display(["foot1", "foot2", "foot3"])),
            map(row_display, rows)));
    // put that in your document.createElement and smoke it!
    swapDOM(oldTable, newTable);

A job well done. You'll find all sorts of goodies that'll save you time in MochiKit: logging utilities, AJAX utilities, the ability to fade text, and sorting lists, just to name a few.

And there you have it, the four simple yet elegant components that pack quite a punch when used together -- the power of SQL without writing SQL queries for data storage, a robust and flexible Pythonic web server for generating dynamic content, XML-based templates to provide an overall structure and organization for your documents, and finally, some JavaScript to spruce things up a bit and add some spunk to the user experience.

No doubt, you're wondering how to get started at this point. Well, it's easy. Too easy. Just go out to the TurboGears Mac installation page and follow the steps. All that's required is making sure the correction version of Python is installed and downloading the goods. Then you'll be on the way to creating that Wiki you've always wanted. There's also a massive collection of resources provided to get you up and running. And now, here's a real treat.

Pages: 1, 2, 3, 4

Next Pagearrow