TurboGears – First Impression

The good

To be fair, I really only read the 20 Minute Wiki tutorial and Getting Started with TurboGears

TurboGears has a great set of components. This framework has promise. I’ve used SQLObject and FormEncode–both very nice. CherryPy is my favorite web sub-framework. I say sub-framework because IMHO it provides a good base for a web framework, but not much more. I’ve also looked at Kid and think it has a lot of potential.

I really like how easy it is to install. This should help dramatically with acceptance. I also look forward to keeping it up-to-date with easy_install -U.

Not only is it easy to install, but it’s very simple to get a base project up and running. I’ve heard about how Rails does this, but never experienced it in real life (that’s a different subject). Anyway, back to the quickstart feature. I can see this being very helpful, especially in creating a common structure for each project. Consistency enhances maintainability.

The documentation looks excellent. The writers have gone above and beyond the call of duty by not only documenting the new API’s introduced by TurboGears, but also by providing enough on each sub-component to get those unfamiliar with the sub-component(s) up to speed without leaving the TurboGears website. This is especially helpful in gaining an understanding of how each component fits into the overall framework.

Finally, it’s really simple to learn, and there’s no substitute for hitting the ground running!

The mediocre

Redirects are used heavily for control flow. I’m not very fond of this approach. I’m coming from Springframework where redirects were discouraged except when absolutely necessary. The reasoning behind this is that it’s better to simply render a different template than to invoke a roundtrip to the client and back just to get a different view. After all, the job of the controller is to choose the correct model and view for a given request. In fairness, this is only a development style, not a requirement of TG.

I’m not in love with the turbogears.flash mechanism of displaying a message on a redirected page–using a cookie. I’m not saying it’s bad, I’m just not sure it’s good to rely on something that some people have disabled to transmit messages within your site.

It doesn’t support Python 2.3 (yet), which means OS X users can’t use it without upgrading to Python 2.4 (come on Apple, get with the program).

The bad

UPDATE: The following no longer applies. A fix was committed within 12 hours of when this review was originally posted – greate work guys.

Automatic JSON output via /page/path?tg_format=json looks cool, but it could be a security vulnerability. Sometimes I send objects of which I don’t intend the user to see every detail to my page templates. With TurboGears, the user only has to add ?tg_format=json to the end of any TG controlled URL to get the objects returned by the controller method. The JSON output should be disabled by default. It could then be selectively enabled by some declarative procedure such as @turbogears.expose(json=True). I would expect most sites to expose only a subset of their URL’s as an AJAX API.

The ugly

raise cherrypy.HTTPRedirect(turbogears.url("/redirect/here"))

<rant>
Exceptions are for exceptional situations and should not be used for application control flow

A redirect is NOT an exceptional situation. Therefore: thou shalt not raise an exception to redirect to a new page. A redirect is a tool to prevent the user from refreshing a submitted page (and thereby resubmitting the data, i.e. after a save operation) or to change the URL in the client browser window. It should be used sparingly, but it does NOT have “exceptional” status.
</rant>

A better way to handle this would be to place a turbogears.url object in the dictionary returned by the controller like this:
return dict(tg_redirect=turbogears.url("/page"))

Questions and Answers

These are questions I thought of while reading the 20 Minute Tutorial. By the time I was half-way through the Getting Started page they were answered.

Conclusion

Out of the box, TurboGears provides everything needed to create a complex web application. One simple easy_install command and you’ve got it all. With a little more effort I think TurboGears could be one of the best web frameworks available for Python.

4 Responses to “TurboGears – First Impression”

- Tim Lesher

“Exceptions are for exceptional situations and should not be used for application control flow”

I agree with you… if you’re talking about C++.

Python exceptions are not C++ exceptions, and can’t be read the same way. For example, iterators raise StopIteration to signal the end of the iteration–it’s not an error case.

When in Rome…

- Daniel

> For example, iterators raise StopIteration to signal the end of the iteration–it’s not an error case.

Actually, it is an error case–there is nothing more over which to iterate. The iter.next() method can no longer return a meaningful result and therefore must raise an exception when called.

- Adam Hupp

I would consider StopItereration normal flow control, not an error condition. In the normal case an iteration will always throw StopIteration. That goes beyond what I think of as an error. Another example is the common idiom for testing capabilities:

try:

True, False

except NameError:

True, False = 1, 0

- Brendan Nooe

Hi, Neat post. There is a problem with your web site in internet explorer, may test this¡K IE nonetheless is the marketplace leader and a huge section of people will omit your fantastic writing due to this problem.