Making a Flickr Killer With TurboGears – Part 2: A Flickr Clone in 37 Minutes Flat

This is the second installment of the lecture I gave at the Israeli Pythoneers Meeting. In case that you missed it, it is recommended that you read the first part of it.

At this point, I closed OpenOffice Impress and said that I would demonstrate how quickly you could get a functional Web application up and running with TurboGears.

Setting Things Up

I’ve quick-started a new project called TurboGallery…

$ tg-admin quickstart
Enter project name: TurboGallery
Enter package name [turbogallery]: <Enter>
Do you need Identity (usernames/passwords) in this project? [no] <Enter>

…and gone inside its directory to run it:

$ cd TurboGallery
$ python start-turbogallery.py


I started Firefox and browsed to http://localhost:8080. “There is already something to see on the site,” I said. The TurboGears’ default welcome page was showing up.

“The next thing I always do is to delete the entire body of the welcome template.” So, I opened welcome.kid in my favorite text editor and did just that. I then refreshed the page in Firefox. All the contents were gone except for the TurboGears’ header and the footer, which said, “TurboGears under the hood.” Someone asked if there was any way to remove those two remaining items. “Absolutely,” I said. It was time to mention master.kid. I opened the file and explained that it is used to render the layout that is common to all pages. As such, you do not have to duplicate it in all your templates. I next removed the TurboGears’ footer but left the header in its place.

I then opened model.py and added a class that would represent a single photo:

from datetime import datetime

class Photo(SQLObject):
    title = UnicodeCol()
    date = DateTimeCol(default=datetime.now)
    image = BLOBCol()
    thumbnail = BLOBCol()

A photo will have a title and a date field, which indicates the time it was uploaded. The field’s default argument makes the date of a newly created photo object be the current date. That way, you don’t need to specify this information every time you create a new photo. In the above definition, we don’t give the date field the current time. Rather, we give it a function that returns the current time. Whenever a photo is created, this function will be called to determine the value for this field. We store the images inside the database together with a thumbnail. BLOB stands for binary large object. The thumbnail part was not actually used in the tutorial.

Next, I created a database with

$ tg-admin sql create

I was then asked how TurboGears knows where and how to access the database. I replied that the default TurboGears’ setting uses SQLite, which creates a database-in-a-file. It is very convenient to have your database readily available as a normal file while your project is in its development and testing phases.

To save time, I have prepared in advance a database that contains four photos. I replaced the database file that was just created with my copy. The next thing to do will be to display the photos on the main page. I modified the index() method into…

@expose(template="turbogallery.templates.welcome")
def index(self):
    photos = Photo.select()
    return dict(photos=photos)

…and uncommented the “from model import *” line. The index() method is called whenever the front page of the Web site is viewed. I explained the expose() decorator, which makes a method available to the outside world. Without it, the method could not be accessed from the Web. The template argument specifies which template should be used to render the page.

The index() method gets a list of all photos from the database and returns it inside a dictionary. TurboGears passes this dictionary to the template. I then moved back to welcome.kid and added the following to the body:

<ul>
     <li py:for="photo in photos">${photo.title}</li>
</ul>
<hr/>

I refreshed the page, and the list of photos was displayed. It is educational to view the source of the resulting page, but the people who were listening to my lecture wanted to see the photos straightaway.

Seeing Some Photos

To see the photos, we’ll have to add an IMG element to the list. The IMG element will get the photo file from another URL. I’ve changed the welcome.kid list into…

<ul class="photo_list">
    <li py:for="photo in photos">
        <img src="/images/${photo.id}"
              id="image${photo.id}" width="160"/>
<br/>
        <a href="/photo_info/${photo.id}">${photo.title}</a>
    </li>
</ul>
<hr/>

…and then added the following photo_info() method to the controller:

@expose()
def images(self, photo_id):
    return "Hello "+photo_id

I next browsed to /images/world, and the browser displayed “Hello world.” The goal of this step was to demonstrate to the audience how TurboGears (CherryPy) maps URLs to methods. It was also intended to illustrate how easily a part of the URL can be transformed into positional arguments.
The right thing to put in this function would be a query that would obtain the photo from the database and return the associated jpeg file. Here is the code:

    @expose(content_type=‘image/jpeg’)
    def images(self, photo_id):
        photo = Photo.get(photo_id)
        return photo.image

I refreshed the page, and the photos I’d prepared were now showing. (I also changed the CSS file when no one was looking to have this layout.)
Evangalizing Firefox in Thailand
In the second picture, you can see me evangelizing for the use of Firefox in Thailand.
Since this is just an example application to make things simple, the image is sent in full size and is resized by the browser.
Clicking on the caption below the images sends us to a page we haven’t yet created. This page will display the photo in full size together with some statistics on it. I saved welcome.kid as photo_info.kid and changed the body contents to:

    <h1>Photo Details for "${photo.title}"

    <img id="image${photo.id}" src="/images/${photo.id}"/>

    <ul>
        <li>Image size: ${len(photo.image)} bytes</li>
        <li>Uploaded at: ${photo.date}</li>
    </ul>

The corresponding method in controllers.py() would be:

    @expose(template="turbogallery.templates.photo_info")
    def photo_info(self, photo_id):
        photo = Photo.get(photo_id)
        return dict(photo=photo)

Share Your Photos

An online gallery application is quite useless if its users can’t upload photos from their own computers. As such, we need to create an image upload form. In TurboGears, creating forms and validating user input is very easy. I’ve added a form definition to the top of controllers.py. The first part defines the fields:

from turbogears.widgets import *
from turbogears import *

class AddPhotoFields(WidgetsList):
    title = TextField(label=‘Title:’)
    image = FileField(label=‘Photo:’)

The form will have a text input field for the title of the photo as well as a field that enables the selection of an image file from the user’s computer. The second part of the definition is the validation schema for this form:

class AddPhotoSchema(validators.Schema):
    title = validators.String(not_empty=True, max=16)

“The touchiest issue with any Web application is its users,” I said. “Without them, there is no need to worry about bugs or invalid input.” The validation makes sure the input your Web application receives is a sound one. In many cases, further validation is needed. The above validator makes sure the title is not empty and is no longer than 16 letters. One audience member asked if the validation can be specified inside the fields definition. Yes, it is possible to specify a validator as a keyword argument to a field definition, but making the validation schema exterior to the fields definition renders it possible to define a more complex schema that involves field dependency or logical operators. A common instance where such a schema is useful is in the validation of a registration form, where you have to check whether or not the entered password field text and the “reenter password field” match.

The last part of the form definition ties the previous two parts together, with text for the submit button and a URL that will handle the form data:

add_photo_form = TableForm(fields=AddPhotoFields(),
        validator=AddPhotoSchema(),
        submit_text=‘Upload!’,
        action=‘/upload’,
        )

I’ve created a template named add.kid that will display just the form:

   <h1>Add New Photo</h1>
    ${form()}

I’ve also added a controller method to make this page accessible…

    @expose(template="turbogallery.templates.add")
    def add(self):
        return dict(form=add_photo_form)

…and I’ve linked to it from welcome.kid.

Here is a screenshot of this page:
Add photo form
As you can see, TurboGears gives us a nice form without us having to type in any HTML at all. Clicking the Upload! Button posts the data to the /upload URL. To test the form, I’ve added the corresponding method to the controller:

    @expose()
    @validate(form=add_photo_form)
    @error_handler(add)
    def upload(self, title, image):
        return "hi"

The decorators that are attached to this method make TurboGears validate its input using the add_photo_form. If a validation error occurs, the response is handled by the add() method, which just displays the form again along with the validation errors. So if, for example, we type in a too-long title, we will get the following:
TurboGears form validation errors
I would now really like to make the method save the image in a new photo object. The following code will do:

    @expose()
    @validate(form=add_photo_form)
    @error_handler(add)
    def upload(self, title, image):
        image = image.file.read()
        Photo(title=title, image=image, thumbnail=None)
        flash("Image successfully added!")
        raise redirect(‘/’)

Yes, handling a file upload in TurboGears is just a matter of reading from a file-like object. It is that simple. The next line creates a photo object with the title and the image data. The flash() method makes the given message appear on the next page, and we redirect to the main page. I filled out the form to upload an image that was downloaded from my camera. Here’s what I got:
Rotate photos in TurboGears
Damn! I hate it when my camera decides to rotate an image that has already been uploaded. So, let’s add an AJAX image-rotation tool. A click on the rotate link will rotate the image in place without requiring the entire page to be reloaded. We first add a method to rotate the image to our controller:

    @expose(‘json’)
    def rotate(self, photo_id):
        import Image
        from cStringIO import StringIO
        photo = Photo.get(photo_id)
        image = Image.open(StringIO(photo.image))
        rotated = image.rotate(90)
        photo.image = rotated.tostring(‘jpeg’, ‘RGB’)
        return dict(photo_id=photo_id, size=rotated.size)

The method uses PIL – Python Imaging Library. It loads the image from the database, rotates it, and then stores it again in the database. The method returns a dictionary containing the photo_id and the new photo dimensions. It is set to return the data in JSON format, which makes it extremely easy to use in Javascript. I directly entered http://localhost:8080/rotate/2 to show what the JSON object looks like. I then refreshed the main page to verify that the photo had been rotated. Next, I rotated it three more times until it was straight again.

I then went back to welcome.kid and added a rotate link for each photo:

<ul class="photo_list">
    <li py:for="photo in photos">
        <img src="/images/${photo.id}"
              id="image${photo.id}" width="160"/>
<br/>
        <a href="/photo_info/${photo.id}">${photo.title}</a>
        <a href="#" onclick="rotate_photo(${photo.id}); return false;">(rotate)</a>
    </li>
</ul>

Clicking on the “rotate” text located next to a photo will call a Javascript function that receives the corresponding photo ID. I’ve added the implementation of rotate_photo() to the top of the welcome.kid template. It uses MochiKit, which must be enabled in config/app.cfg (it is explained how to do so in that file).

<script type="text/javascript">
    function rotate_photo(photo_id) {
        d = loadJSONDoc(‘/rotate/’+photo_id);
        d.addCallback(update);
    }

    function update(r) {
       $(‘image’+r.photo_id).src=‘/images/’+(r.photo_id)+‘?random=’+Math.random();
    }
</script>

The function makes an asynchronous call to the rotate URL, providing it with the photo ID. Once the response arrives, update() is called and is provided with the JSON object we returned from the controller’s rotate() method. In Javascript, you can use dot notation to access the keys in that dictionary. When update() is called, the image has already been rotated at the server. Therefore it is the right time for the browser to fetch it again. To perform this re-fetching, update() sets the image src attribute to the image URL, but in order to prevent the browser from displaying the cached old image, we add a random argument to the URL. You can see a related post describing how to prevent browsers from caching. To make the image() controller method accept this argument and ignore it, its definition becomes:

def images(self, photo_id, random=None):

I then refreshed the main page and rotated the photos a few times to demonstrate how slick this functionality is (especially when you’re working on the server). You can see it in the video below:

Next, I uploaded another image I had prepared in advance, this one called questions.jpg, using the upload form. Here it is:

It says “Questions?” in Hebrew, signifying that the lecture was over and it was time to ask questions.

Download the full source code of TurboGallery.

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

15 Responses to Making a Flickr Killer With TurboGears – Part 2: A Flickr Clone in 37 Minutes Flat

  1. Pingback: Elliot Murphy 28:1 » Blog Archive » links for 2007-02-21

  2. Ryan says:

    Thanks for posting this tutorial, I had fun following it to make my first turbogears app.

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

  4. Tomek says:

    I was unable to read the images from your database – neither via TG

    wget http://localhost:8080/images/3; cp 3 3tg.jpeg

    nor directly from sqlite

    sqlite3 devdata.sqlite "select image from photo where id=3;" > 3sqlite.jpeg

    None of the files was in jpeg format. The former was binary but not jpeg, ant the latter was text.
    The complete unmodified TurboGallery gives “500 Internal error” on fresh 1.0.1 TurboGears installation :(
    What am I missing?

  5. thesamet says:

    Hi Tomek! Dumping the data from sqlite3 via the command line does not yield a valid jpeg file here is well. It looks like sqlite gives the data encoded in some 7-bit format.

    Can you send the traceback that you are getting? It might give us a clue.

  6. Tomek says:

    It turns out that python-sqlite (python interface to SQLite 2) silently takes over access to the database file. Removing the package solved the problem of getting bad jpegs via http://localhost:8080/images/3 Unfortunately the package dependencies shows no conflict with python-pysqlite2.

    Still, http://localhost:8080/ yields

    500 Internal error

    The server encountered an unexpected condition which prevented it from fulfilling the request.

    Page handler: >
    Traceback (most recent call last):
    File "/usr/lib/python2.4/site-packages/CherryPy-2.2.1-py2.4.egg/cherrypy/_cphttptools.py", line 105, in _run
    self.main()
    File "/usr/lib/python2.4/site-packages/CherryPy-2.2.1-py2.4.egg/cherrypy/_cphttptools.py", line 254, in main
    body = page_handler(*virtual_path, **self.params)
    File "", line 3, in index
    File "/usr/lib/python2.4/site-packages/TurboGears-1.0.1-py2.4.egg/turbogears/controllers.py", line 334, in expose
    output = database.run_with_transaction(
    File "", line 5, in run_with_transaction
    File "/usr/lib/python2.4/site-packages/TurboGears-1.0.1-py2.4.egg/turbogears/database.py", line 302, in so_rwt
    retval = func(*args, **kw)
    File "", line 5, in _expose
    File "/usr/lib/python2.4/site-packages/TurboGears-1.0.1-py2.4.egg/turbogears/controllers.py", line 351, in
    mapping, fragment, args, kw)))
    File "/usr/lib/python2.4/site-packages/TurboGears-1.0.1-py2.4.egg/turbogears/controllers.py", line 391, in _execute_func
    return _process_output(output, template, format, content_type, mapping, fragment)
    File "/usr/lib/python2.4/site-packages/TurboGears-1.0.1-py2.4.egg/turbogears/controllers.py", line 82, in _process_output
    fragment=fragment)
    File "/usr/lib/python2.4/site-packages/TurboGears-1.0.1-py2.4.egg/turbogears/view/base.py", line 131, in render
    return engine.render(**kw)
    File "/usr/lib/python2.4/site-packages/TurboKid-0.9.9-py2.4.egg/turbokid/kidsupport.py", line 158, in render
    tclass = self.load_template(template)
    File "/usr/lib/python2.4/site-packages/TurboKid-0.9.9-py2.4.egg/turbokid/kidsupport.py", line 128, in load_template
    mod = _compile_template(package, basename, tfile, classname)
    File "/usr/lib/python2.4/site-packages/TurboKid-0.9.9-py2.4.egg/turbokid/kidsupport.py", line 15, in _compile_template
    mod = kid.load_template(tfile, name=classname)
    File "/usr/lib/python2.4/site-packages/kid-0.9.5-py2.4.egg/kid/__init__.py", line 158, in load_template
    store=cache, ns=ns, exec_module=exec_module)
    File "/usr/lib/python2.4/site-packages/kid-0.9.5-py2.4.egg/kid/importer.py", line 144, in _create_module
    raise_template_error(module=name, filename=filename)
    File "/usr/lib/python2.4/site-packages/kid-0.9.5-py2.4.egg/kid/codewriter.py", line 184, in raise_template_error
    s.insert(0, 'Error location in template file %r' % filename)
    AttributeError: 'str' object has no attribute 'insert'

  7. thesamet says:

    Hi Tomek,

    It seems that the zip file contained pyc files which were used to work against an older version of Kid. If you remove them, it will start working.

    This is a common gotcha, if you upgrade Kid while working on a project, you have to delete the templates’ pyc files. I’ve uploaded a new zip without these.

  8. Tomek says:

    Thanks!
    This solved the problem. I needed to install PIL to make ‘rotate’ work. I’ll have to dig slightly deeper to force kid forms display correctly labels in Polish.

  9. alex says:

    hi nadav,

    loved your tutorial, thanks a lot!

    answered me a few questions in tg development.

    a small addition: on the fly jpeg translation of imported files. simple with pil (not elephant ;) ).

    keep up,

    alex

  10. jay says:

    This is great,
    Thanks so much for posting such a great walkthrough. It’s both thorough without being overwhelming and answers a lot of the corner cases I’d been wondering about (like how to upload files and ajax) !

  11. Brennan Ashton says:

    Very nice tutorial here is a simple addition that you might like that allows you to delete images as well,
    in welcome.kid


    (rotate)
    (delete)

    and in the controller


    @expose()
    def delete(self, ID):
    Photo.delete(ID)
    flash("Image successfully deleted!")
    raise redirect('/')

  12. Brennan Ashton says:

    figures it would make it would try the actual HTML
    replace the ^ with left or right tag points

    ^a href="#" onclick="rotate_photo(${photo.id}); return false;"^(rotate)^/a^
    ^a href="/delete/${photo.id}"^(delete)^/a^

    I hope that shows up correctly

  13. Manoranjan Naik says:

    Hi,
    I am turbogears learner.
    Its really nice and funny to do this application.
    But i could not find @expose(content_type=‘image/jpeg’) at turbogears documents. Needing help

    Thanks,
    Ranjan

  14. Jitesh says:

    Hi,
    I am a new beein Turbogears still I can say its a very good publication for a person like me.It has aroused more interest with in me to go deeper into Turbogears and Python.

    Thanks
    Jitesh

  15. alex says:

    hello thesamet, friends.

    as turbogallery gave me some hints in web programming with tg, and i’m about to implement the ideas here in a tg2 project, here’s a port of turbogallery to tg2:
    http://resheteva.org.il/packages/TurboGallery2.zip .

    thesamet: please feel completely free to copy my port and distribute it on your site. it’s free to all, anyway.

    best regards,
    alex

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>