Content-Type: RST *Warning: some Python knowledge required for understanding my mad ravings.* Google is going to take over the world. Well, more like the internet. But the internet makes the world go round? So then Google is going to take away the world's roundness... making the world flat_? .. _flat: http://en.wikipedia.org/wiki/The_World_is_Flat But enough punnings about my past school summer reading, and more about the topic: the **Google App Engine_**. First off... what the heck is it? .. _`Google App Engine`: http://code.google.com/appengine .. figure :: http://blog.opensourcenerd.com/upload/app-engine-jet `Source `_ It's a freaking jet engine with freaking snakes (Pythons) on it. Scary. Okay, that's not what really is, but it's close. Have you ever wondered what it's like to run stuff on Google's computers? They have insane computing power, awesome databases, and are one of the most reliable things on the web. Plus, running it on their servers could give you *some* access to their doubtlessly amazing tools that they use to make all their magic, right? What if people could log into your app using just their Google account? Google App Engine is a platform for developing and hosting web applications in Google-managed data centers. (source: Wikipedia) In commonspeak: the answer to the above questions is "you can". How? ---- Python! No PHP, Perl, or other scripting. Most of your app can be managed in pure Python, with only the minor inclusion of a YAML (Yet Another Markup Language) file in order to inform the app engine of how your stuff is supposed to be run. When you're writing your app, Google gives you a local development server to use. This server is completely independent of the internet, and therefore lacks some of the neater features of the app engine, such as using people's actual Google accounts, or `cloud computing `_. However, the rest of the API is completely implemented and gives a pretty accurate representation of how your stuff will actually run on the google server. Now, I'm pretty bad at explaining how stuff works by using walls of English, so I'll use walls of Python instead. Let's look at the epitome of simplicity: .. figure :: http://www.opensourcenerd.com/resources/helloworld.png The Code -------- *A slightly modified view on the tutorial Google App Engine comes with. Not a replacement for the original.* First, let's look at the app.yaml file. Only one look at this is necessary, since most newbie developers won't change it for a long time. .. sourcecode :: yaml application: helloworld version: 1 runtime: python api_version: 1 handlers: - url: /.* script: helloworld.py The top line should only be changed before you submit your app to the Google server itself, and will determine where at .appspot.com your stuff will be hosted. The version, runtime and api_version don't concern us. Then, at the bottom, it's telling that all the requests that match the "/.*" regular expression (which translates to every single request), should be processed by the helloworld.py script. Non-painful configuration - unlike my experiences with Zope 3's ZCML, which for this simple helloworld use would be about 20 lines of garbled XML by now. Now, let's take a look at the python code we need: .. sourcecode :: python print 'Content-Type: text/plain' print '' print 'Hello, world!' Next, run the development server on the directory you have your Google app in, and presto! Hello, world! At http://localhost:8080! .. figure :: http://www.opensourcenerd.com/resources/easy.png Now, to print more than plain static text, and to actually use Google's API, we need to do a little more work to achieve the same helloworld-ness. .. sourcecode :: python from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app class MainPage(webapp.RequestHandler): def get(self): self.response.headers['Content-Type'] = 'text/plain' self.response.out.write('Hello, webapp World!') application = webapp.WSGIApplication( [('/', MainPage)], debug=True) def main(): run_wsgi_app(application) if __name__ == "__main__": main() What's all this? It looks like other weird web application server stuff that I never understand! What gives, Google? - The imports: they're just imports. They don't bite. Google seems to have an odd structure for doing its packages, but it seems to work. - class Mainpage... etc. This is making a specific handler called MainPage, and defining its "get" method (use this unless you explicitly submitted a form here that used "post"). - It sets the response's headers to the MIME type of 'text/plain'. - ... And writes "Hello, webapp World!". - I'm yet to understand what WSGI is, but I just know it creates your application, while defining "/" as linking to the MainPage. Remember this Python file manages everything that starts with a "/", so the plain "/" will now give Hello World. The others, nothing. - The end stuff is just running the application. That will never need to change. So, its syntax is a little ugly for a mere hello world, but for bigger stuff it starts to be really useful. I can feel people's attention waning by now, despite my pretty pictures and my witty pythonic rhetoric, so I'll only cover one for now: user accounts. Users ----- .. sourcecode :: python from google.appengine.api import users # ... (removed for compactness) user = users.get_current_user() if user: self.response.headers['Content-Type'] = 'text/plain' self.response.out.write('Hello, ' + user.nickname()) else: self.redirect(users.create_login_url(self.request.uri)) # ... Google managed all user problems for you! It gives you a User object, or None if you ask it for the current user. The login URL it "creates" is actually not that pretty on your development server: it just asks for a sample email address and whether the user is an admin for the site or not. When uploaded to the real Google server, though, it would give the user one of the pretty, Google pre-configured login pages. You know, the kind you see when Google decides to check if you still remember what your password is, but your browser remembers it for you and autofills it, hastening your forgetting of the actual password. Anyway, that's enough from my big mouth. Next time I feel like blistering my fingers with another insanely long entry on appengine, I will talk about the weird mutant love children of Google with SQL and Django templating: GQL and Google templates. Until then, Goodbye World!