Making a Flickr Killer With TurboGears – Part 1: Introduction To TurboGears

One month ago, I gave an introductory speech about TurboGears at the Israeli Pythoneers Meeting. The discussion consisted of two parts. The first part introduced TurboGears, and the second part included live coding of a Flickr clone.

I hereby give the lecture again, in a written format. These are the original slides used in the lecture. Click on a slide to enlarge it.

Because it was only the second time I had attended one of these meetings, I briefly introduced myself. The two slides above show photos that were taken in my home town, Adi, which is a small town in Northern Israel. In the right photo, you can see my cat Uzi watching the cows in our backyard. If you have taken part in the Python Challenge, you might have already met Uzi.

TurboGears is a framework that enables the rapid development of Web applications in Python. But what is a Web application? Great question!

Wikipedia provides this explanation:

In software engineering, a Web application or webapp is an application that is accessed with a Web browser over a network such as the Internet or an intranet.
Web applications are popular due to the ubiquity of the browser as a client, sometimes called a thin client. The ability to update and maintain Web applications without distributing and installing software on potentially thousands of client computers is a key reason for their popularity. Web applications are used to implement Webmail, online retail sales, online auctions, wikis, discussion boards, Weblogs, MMORPGs, and many other functions.

What makes TurboGears stand out from the crowd is its unique philosophy of reusing. Instead of developing its own Web server, HTML-templating language and database-to-object relational mapper, it integrates existing mature and best-of-breed components. The main actors are CherryPy, which makes adding a page as easy as writing a method; Kid, which is a designer-friendly XHTML-templating language; and SQLObject, which allows you to use an SQL database without writing a single line of SQL. MochiKit is a pill library that eases your pains while coding in Javascript…

Why should you choose TurboGears as your Web-application development framework for your next project? Well, the short answer is that PHP is the devil – it tempts you to mix HTML, logic, and database queries into the same piece of code. Rails, as great as it may be, is written in Ruby, and we like Python better. For Django, I borrowed (err, stole) Adrian Holovaty’s port of Mark Pilgrim’s piece:

I was walking across a bridge one day, and I saw a man standing on the edge, about to jump off. So I ran over and said, “Stop! Don’t do it!”
“I can’t help it,” he cried. “I’ve lost my will to live.”
“What do you do for a living?” I asked.
He said, “I work with computers.”
“Me too!” I said. “What do you do with computers?”
He said, “I’m a Web developer.”
“Me too!” I said. “Design, client-side programming or server-side programming?”
He said, “Server-side programming.”
“Me too!” I said. “Do you use dynamically typed languages or statically typed languages?”
He said, “Dynamically typed languages.”
“Me too!” I said. “Do you use a Web framework, or do you roll things on your own?”
He said, “I use a Web framework.”
“Me too!” I said. “TurboGears or Django?”
He said, “Django.”
“Die, heretic scum!” I shouted, and I pushed him over the edge.

The moral of the story is that TurboGears and Django have much more in common than they have in the way of differences. It has been said that Django is like a Macintosh—it simply works—while TurboGears is more like Unix—its power comes from its being made of many smaller building blocks, each one of them designed to do one thing well.

TurboGears and Django try to solve different types of problems. Django was born in a publishers’ newsroom environment, where Web applications needed to be launched within hours. By its very nature, Django is geared toward the development of content-oriented Web sites. TurboGears, on the other hand, evolved while Kevin Dangoor was developing Zesty News, a news-reader application. As a result, it is more suitable for Web applications (see earlier definition).

Having said that, I found it very easy to develop content-oriented Web sites in TurboGears. Currently, TurboGears’ CRUD features and administration panel are somewhat lacking in comparison to Django’s. As such you have to manually code these aspects of your application if you want a non-tech author to be able to fill your project’s database with content or change various aspects of the Web site.

TurboGears goes a long way in encouraging you to design your application in the so-called MVC paradigm. MVC stands for Model, Viewer, and Controller. The model consists of the classes that represent the information on which the application operates. That information is usually stored in an SQL database. The view is composed of the HTML page templates that are used to render the model. The controller responds to user input (HTTP requests) by operating on the model and returning a view for subsequent interaction.

In a nutshell, MVC is all about separating metadata, logic, and presentation.

The above is an example of how you would model a user with SQLObject. It has first_name and last_name fields as well as a date-time field, which represents the time at which the user was last logged in to the system.

The above class definition can automatically generate the SQL table creation statement:

The “Person” class gives you a very comfortable interface with the database objects:

In the next installment, I will show you how easy it is to create a Flickr killer application with TurboGears in 37 minutes.

Make sure you won’t miss it by subscribing to the RSS feed.

This entry was posted in python, turbogears. Bookmark the permalink.

16 Responses to Making a Flickr Killer With TurboGears – Part 1: Introduction To TurboGears

  1. Pingback: links for 2007-02-09 at Blue Sky On Mars

  2. Arui says:

    I have translated this to Chinese,and post here:
    wish you don’t mind…..

  3. Nadav Samet says:

    Arui, thanks for the translation! See you around, and do not forget to translate the next installment when it is out :)

  4. Pingback: Making a Flickr Killer With TurboGears - Part 2: A Flickr Clone in 37 Minutes Flat · Nadav Samet's Blog

  5. Pingback: Screenshot l10n · Nadav Samet's Blog

  6. celebrities says:

    Excellent way of explaining, and good post to take information about my presentation topic, which i
    am going to present in institution of higher education.

  7. himalaje k2 says:

    You may create a pastime away from helping others.
    This really is one particular activity that’ll keep you active for several weeks!

    An excellent hobby to obtain is riding.

  8. Igo says:

    I’m very new to Turbogears. I downloaded your full srucoe code of a minimal identity testable project. from the WindowsXP commandline I ran:python start-idtest.pywhich loads up the webserver to port 8080 (just like all the other tutorials etc I’ve run).I thought I’d view the site in a browser (IE 6.0) by going to the following URL: I’m shown an ERROR page: 500 Internal errorThe server encountered an unexpected condition which prevented it from fulfilling the request.Traceback (most recent call last): File c:\python25\lib\site-packages\cherrypy-2.2.1-py2.5.egg\cherrypy\ , line 103, in _run applyFilters( before_main’) File c:\python25\lib\site-packages\cherrypy-2.2.1-py2.5.egg\cherrypy\filters\ , line 151, in applyFilters method() File c:\python25\lib\site-packages\TurboGears-\turbogears\visit\ , line 146, in before_main visit = _manager.new_visit_with_key(visit_key) File c:\python25\lib\site-packages\TurboGears-\turbogears\visit\ , line 44, in new_visit_with_key visit= visit_class( visit_key=visit_key, ) File c:\python25\lib\site-packages\SQLObject-0.9.1-py2.5.egg\sqlobject\ , line 98, in _wrapper return fn(self, *args, **kwargs) File c:\python25\lib\site-packages\SQLObject-0.9.1-py2.5.egg\sqlobject\ , line 1218, in __init__ self._create(id, **kw) File c:\python25\lib\site-packages\SQLObject-0.9.1-py2.5.egg\sqlobject\ , line 1246, in _create self.set(**kw) File c:\python25\lib\site-packages\SQLObject-0.9.1-py2.5.egg\sqlobject\ , line 1093, in set kw[name] = dbValue = from_python(value, self._SO_validatorState) File c:\python25\lib\site-packages\SQLObject-0.9.1-py2.5.egg\sqlobject\ , line 596, in from_python (, type(value), value), value, state)Invalid: expected an int in the IntCol user_id’, got instead I’ve also run nosetests’ from the root folder of the project and 3 of the 4 tests fail. I decided to print out self.user.response and it mentions the INTERNAL 500 ERROR in the response. Here is the body text: 500 Internal error The server encountered an unexpected condition which prevented it from fulfilling the request. Traceback (most recent call last): File c:\python25\lib\site-packages\cherrypy-2.2.1-py2.5.egg\cherrypy\ , line 103, in _run applyFilters( before_main’) File c:\python25\lib\site-packages\cherrypy-2.2.1-py2.5.egg\cherrypy\filters\ , line 151, in applyFilters method() File c:\python25\lib\site-packages\TurboGears-\turbogears\visit\ , line 146, in before_main visit = _manager.new_visit_with_key(visit_key)AttributeError: NoneType’ object has no attribute new_visit_with_key’ Powered by I’m running Python 2.5SQL:Liteall defaults as far as I know, I just used easy_install etc.

  9. AKAIK you’ve got the answer in one!

  10. Good to find an expert who knows what he’s talking about!

  11. Vous semblez oublier qu’on peut être de gauche et contre l’adoption homosexuelle (avez-vous pensé à l’enfant ?). On peut être de gauche et catholique.Les extrémistes ne sont pas toujours du côté que l’on croit.

  12. Salut Aurélien,la crise a agravé les choses et il y a moins de boulots, c’est une certitude. En revanche tu as bien raison de dire que les médias véhiculent un climat pésimiste et qu’il serait bon de donner de l’espoir aux gens pour qu’ils aient envie de se dépasser. L’autre fait est que le Français aime avoir le dernier iphone, d’aller au restaurant, d’avoir internet chez soi, …. et il faut donc gagner un peu plus pour pouvoir avoir TOUT.

  13. Esta reacción infantil suele desencadenar nerviosismo y alteración por parte de los progenitories, más aún cuando este tiene lugar en un espacio público y el pequeño moleta a otras personas con su actitud.

  14. Hola, mi nombre es Clara Ruiz. Descubrí el blog hace unos días y estoy enganchada a éste. La receta de hoy me vendrá muy bien puesto que en casa no tenemos horno, una grn desventaja…Me gustaría hacerte una pregunta Sonia, terminas la tarta espolvoreando azucar glass pero en la foto creo que aparece una fina capa del mismo bizcocho que la base de la tarta, puede ser?Gracias

  15. Robert: Guess I was getting at hiring PJ it wasn’t a sure thing Lakers would win the championship. The Lakers FO has earned our trust by the great offseason and recognizing a mistake by firing Mike Brown. I’m willing to give D’Antoni a chance and let Jim bring us a ring his way not PJ’s.Nash, Dwight and D’Antoni. Can you imagine the devastation that will occur from a Nash/ Dwight PnR? Perkins just lost a lot of value with this hire.

Leave a Reply

Your email address will not be published. Required fields are marked *