Flask on Google App Engine – a getting started template/guide

I recently came across Flask, a nice tiny Python micro-web-framework.

I took the ‘hello world’ sample from the Flask website, which I wanted to host/run on Google App Engine (GAE).

Here are the necessary steps for the “Flask Hello World on App Engine”:

The python source File (pineapple.py in this example):

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

An app.yaml for the Google App Engine:

This is straightforward, but I want to use jinja2 (a requirement for Flask) lib from GAE, so  I added the libraries section


application: flaskpineapple
version: 1
runtime: python27
api_version: 1
threadsafe: true

libraries:
- name: jinja2
  version: "2.6"

handlers:
- url: /.*
  script: pineapple.app

The dependencies:

Unfortunately there are no libs for Flask available in GAE (yet?), so I had to include them in the app (which is the GAE way to do that). The same is necessary for Flask’s dependencies “werkzeug” and “itsdangerous“. This is simply done by copying the Modules/Files into the application directory

λ ls -al

drwxr-xr-x   17 Daniel   Adm    4096 Jun  3 11:01 .
drwxr-xr-x    1 Daniel   Adm       0 Jun  3 10:49 ..
-rw-r--r--    1 Daniel   Adm     194 Jun  3 10:44 app.yaml
drwxr-xr-x   38 Daniel   Adm    8192 Jun  3 10:43 flask
-rw-r--r--    1 Daniel   Adm   31840 May 30 17:19 itsdangerous.py
-rw-r--r--    1 Daniel   Adm     165 Jun  3 10:46 pineapple.py
drwxr-xr-x   39 Daniel   Adm    8192 Jun  3 10:44 werkzeug

That’s all! A simple “Hello World” with Flask and GAE.

This is the App: http://flaskpineapple.appspot.com/
And the complete source: https://github.com/danimajo/pineapple