Getting Started with the Google App Engineby Noah Gift
It's a bird, it's a plane, no it is Guido Van Rossum and the App Engine Team. After cranking through writing an instructional application this weekend using Google App Engine, I have to say I am really impressed. In fact my exact thought are "Holy $!ck, this is awesome!
If you haven't heard about Google App Engine yet, in a nutshell, Google has opened up their infrastructure to the masses in the form of a Python API. If you are a Python developer this is obviously a very significant development as it means you can easily hit the ground running. If you come from another language such as PHP, Java, Ruby, or Perl, there is no reason why you can't get started in a day or so in writing applications in Python. Python is quite easy to learn if you already know another language, and it has a deserved reputation for being one of the easiest languages to learn period.
In this article, we are going to "crib" some of the documentation for Google App Engine, and then proceed to actually build an application using the SDK. There is a live version of the application for this article that you can view here:Greedy Coin App Engine.
First off let's take a look at some of the exposed Developer API's:
- Datastore: This is a huge deal as it lets you use Google's scalable infrastructure to perform queries in Python or GQL.
- Users API: The Users API lets you easily authenticate users via Google's authentication system.
- Email: Sending email can be done with Google's servers and there is a rich API.
- URL Fetch: Can make outbound HTTP requests.
Each application is subject to a set of resource quotas that control how much CPU, Bandwidth and storage space it can consume.
- Apps per Developer 3
- Space 500MB
- Various CPU/Memory/Disk I/O limitations
- Emails 2,000 Per Day
It can handle sudden spikes in traffic by ramping up demand. If the quota is exceeded you will get 403 errors.
You can reference more here: http://code.google.com/appengine/articles/quotas.html
Running Django on Google App Engine
You can launch a Django application using a WSGI handler, but a few modifications will need to be made. You can reference them here:
By default Google App Engine lets you share your application with anyone with a Google Account. You can restrict this to access to only Google App domain accounts though.
Python has a sweet logging module, and Google App Engine directly integrates with it by allowing you to log 5 levels of severity. These logs get funneled to the Administration Console at:
You can reference more here:
You can also view a debug log by attaching a ?debug to the end of a URL path.
Sample Applications and Code
You can download many sample applications here by doing a svn checkout:
There is a custom version of Django's form validation framework that works with the Google datastore.
This means it is easy to write forms that validate data correctly.
Flexible, schema-less object database, perhaps influenced byZODB to some degree, but apparently also in the spirit of JQuery and FBQL.
SELECT * FROM Story WHERE title = 'App Engine Launch' AND author = :current_user AND rating >= 10 ORDER BY rating, created DESC
One gotcha is that the Datastore does not support joins, as this is one of the performance bottlenecks with a Relational Databases. In my opinion this API is one of the hidden Gems of the Google App Engine API.
Building an Application: Greedy Coin Changer
Recently I wrote a blog post about a Greedy Coin Changing Algorithm. I decided to take that command line tool and convert it into a Google App Engine application for this article. This happens to be the way I tend to write a lot of small web applications, which may be weird for some people, but I happen to be a command line tool kind of guy.
Before we get started I wanted to point a couple of things out. First, this tutorial has a fully functioning application you can play with, along with the source code. If you are "uber" , you might just want to check it out of svn and use it as boiler plate for your own creation. At the bottom of this article in the references section there is a link to the application and the subversion repository where you can check out the source code.
Second, this is more condensed, pragmatic version of a tutorial than you will find at the official Google App Engine Website. You might want to refer to that link, again in the references section, if you want to refer to it for some of the items we cover, as it does a great job of covering things in great detail. This tutorial is really a "backwards first" tutorial, so if you have trouble understanding this tutorial, come back to it after you finish the official one. From here on out, any download or link mentioned will be available in the Resources section at the bottom.
There were approximately 10,000 beta accounts given out for Google App Engine, so if you didn't get lucky like me, you can still develop using the SDK offline, and then when it is opened up you will be already to go. First you will need to download the SDK, and then you only need to follow three steps to get a helloworld application working:
create a helloworld directory and create a file named helloworld.py inside of it:
print 'Content-Type: text/plain' print '' print 'Hello, world!'
inside of helloworld create a file named app.yaml that looks like this:
application: helloworld version: 1 runtime: python api_version: 1 handlers: - url: /.* script: helloworld.py
Next you just need to run the dev_appserver.py script by either giving the full path to your directory or just a "." if you are in the directory. This is how it looks for me when I run it from inside of the actual directory.
If you navigate to: http://localhost:8080/ you will see your hello world application in action. That is all there is to getting started with developing an application. As you will see in a bit, the API is incredibly powerful, so writing an application with GAE can be trivial.