ONLamp.com
oreilly.comSafari Books Online.Conferences.

advertisement


Getting Started with the Google App Engine
Pages: 1, 2, 3

Datastore Backwards First

Now that we a vague understanding about authentication, lets get into the powerful stuff. The Datastore API lets you store persistent data and then retrieve it throughout your application. In order to do this you need to import the datastore, as shown in the above code, and then also define a model like this:




    class ChangeModel(db.Model):
         user = db.UserProperty()
        input = db.IntegerProperty()
         date = db.DateTimeProperty(auto_now_add=True)

With that simple class we can now create and use persistent data. Here is a class in which I use Python API to the Datastore to retrieve the last 10 changes made to the database, and then display them:

    class Recent(webapp.RequestHandler):
         """Query Last 10 Requests"""

                 def get(self):

                 #collection
                 collection = []
                 #grab last 10 records from datastore
                 query = ChangeModel.all().order('-date')
                 records = query.fetch(limit=10)

                 #formats decimal correctly
                 for change in records:
                     collection.append(decimal.Decimal(change.input)/100)

                template_values = {
                 'inputs': collection,
                 'records': records,
                 }

                 path = os.path.join(os.path.dirname(__file__), 'query.html')
                 self.response.out.write(template.render(path,template_values))

The two most important lines are:

    query = ChangeModel.all().order('-date')
    records = query.fetch(limit=10)

These pull the results out of the Datastore and then "fetch" 10 records in a query At this point, a simple thing to do for fun, would be to experiment with with this code and to try to fetch more records, or to sort them in a different way. This should give you some immediate and fun feedback.

URLS Backwards First

Believe it or not, we are about done with our reverse tutorial. The last bit of code to look at is the main function: Looking closely you might be able to guess that each of the URLs corresponds to a class we defined in our change.py file. At this point, I would recommend trying to tweak the names of URLS by changing the parts of the application that depend on a URL, this will give you a good idea of how things get routed around.

ADDITIONAL APP ENGINE INFORMATION

Python Web Frameworks, Google App Engine, and Webob

One of the more interesting aspects of Google App Engine is the inclusion of a very simple WSGI-compatible web application framework called webapp. A large and critical portion of this framework is based on a slightly modified version of Webob, a third party library created by Ian Bicking. Webob "provides objects for HTTP requests and responses", and has been under constant development by Ian over the last few years. As a side note, the Pylons web framework uses Webob as well. One slight modification that occurred with Webob is that the actual Response object was replaced with a custom Response object within the GAE webapp framework.

While the default web framework is a hybrid creation that Google engineered, it is also possible to run some of the popular Python frameworks, such as Pylons, web.py, and Django. There is also some talk of getting Zope 3 and Grok to work on GAE this summer, and this may develop at the upcoming Google I/O event. In addition, although Django templates are included, it is also possible to run a very popular fork of Django templates called, Jinja, as well as Mako and possibly a development version of Genshi. Please see the resource section for links to some of the projects, and special hacks.

Google I/O Event

If this tutorial got you interested in writing the next MySpace or Facebook with Google App Engine, then you might want to consider attending the Google I/O Event. I will be attending the Google I/O Event on May 28th and 29th in San Francisco, and am very excited to go. I was fortunate enough to score a phone interview with Tom Stocky, who is a Senior Product Manager at Google, and he had this to say about the Google I/O event.

"Google I/O is for developers interested in being part of the next

generation of web apps," said Tom Stocky, senior product manager at

Google. "It's the one time this year that we'll have the engineering

teams working on developer products all in one place, engaged with the

community."

SUMMARY

This article glossed over quite a few of the details of making a basic application, so if you feel it went too fast, then make sure you go through the complete official tutorial. Once you get done with that, come back to this code and take a look again to get some ideas, or just steal the fancy "pumpkin" CSS.

At this point in the article I get to give my opinion of the Google App Engine. I would rate it a 9.5 on a scale of 1-10. The environment just fits my brain, and I have no problem with developing complex applications with exactly the tools I was given. It might be nice to get a few more options for templating, and such, but it was a slam dunk for me. In fact I am so excited about building App Engine applications, I might need to hide my expresso machine for a bit. Feel free to contact me with questions, or hopefully consulting offers to work on a crazy App Engine project.

REFERENCES

Live Greedy Coin Tutorial Application
Source Code via SVN
Google App Engine Getting Started Guide
App Engine SDK Download
Django Templates
Webob
appengine-monkey
Do-It-Yourself
Webapp
Ian Bicking Home Page
Pylons Web Framework
Jinja templates
Mako templates

Noah Gift is the co-author of Python For Unix and Linux by O'Reilly. He is an author, speaker, consultant, and community leader, writing for publications such as IBM Developerworks, Red Hat Magazine, O'Reilly, and MacTech, and Manning.


Return to ONLamp.com.



Sponsored by: