Categories
Python

Getting Started with Google App Engine [Part 1]

For longest time I’ve done web development exclusively in PHP.  Lately however I’ve been looking for something a bit different to play with.  I already know Python (not well of course, but that’s changing), so I thought I’d look into web app development using that.  The most obvious way to develop web apps with Python is with a framework like Django or Pylons, but I was interested in scalability too.  Actually, I was interested in EASY scalability.  This is where Google App Engine and their WebApp framework steps in.

What is Google App Engine?

Google App Engine (GAE) is a platform for building highly scalable web applications on Google’s infrastructure.  So what does that mean for you?  It means you can use Python or Java to create web sites hosted on Google’s servers.  The main benefit of using GAE is scalability.  Google’s infrastructure is ridiculously huge, and having access to that means basically unlimited scalability.  You also get to access this all for free, and after you reach the free quota limits, you pay only for what you use.  For more information on quotas for the free service, click here.

Getting Started

Now that you’re interested, you probably want to try it out.  Before you can do that though, you need to create a GAE account and download the development environment.  Development environments are available for Windows, Mac, and Linux.  Setting up the environment is fairly straight forward, and all directions from this point forward should work for you regardless of your platform.  More information about setting up the development environment is available here.

A Simple “Hello World”

Keeping with the spirit of programmers everywhere, I’m going to start off with a simple “Hello World” program.  The first thing you need to do is create a directory called helloworld in the GAE directory.  After that, create 2 files in the new directory called helloworld.py and app.yaml.  Add the following to those files.

helloworld.py

print 'Content-Type: text/html'
print ''
print 'Hello world!'

app.yaml

application: helloworld
version: 1
runtime: python
api_version: 1
 
handlers:
-	url: /.*
	script: helloworld.py

So let’s explain things a bit.  The first file helloworld.py, is straight forward.  The first line sets the content type to HTML, prints a new line, and then prints “Hello world!”.  The second file app.yaml, is a little more complicated.  Here’s the breakdown:

  • application – The name of the folder containing the application.
  • version – The version of the app you plan to upload to GAE.  Increment this every time you are going to upload to GAE and it will keep track of your different versions.
  • runtime – This is the language you are writing the app in.  Python is what we’re using, but Java (lowercase) is also an option.
  • api_version – The GAE api version that we are using.  1 is usually a good choice here.
  • handlers – This essentially maps the url path to a python file.  Once you start the web server, by going to any URL you will be routed through the helloworld.py script.  If you wanted http://localhost/hello to go through helloworld.py, the you would change “url” to “/hello”.

Once those files are in place, running the app is easy.  Let’s assume that the helloworld script is in a directory called helloworld within my Google App Engine directory.  With that assumption, you run:

google_appengine/dev_appserver.py helloworld/

Next Time…

In the next part of this series, I’ll talk about getting started with submitting forms, storing information in the data store, and using templates.