Python: Flask

WSGI

Gunicorn - a WSGI Server

# -w = number of workers (rule of thumb - #cpu*2)
gunicorn -w 4 'hello:app'       # equivalent to 'from hello import app' - 4 workers

Minimal Application

# hello_world.py

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"
$ export FLASK_APP="$PWD/hello_world.py"
$ flask run &
  * Running on http://127.0.0.1:5000
$ curl 127.0.0.1:5000

...

#
# flask_hello_world.py
#

import Flask

# Flask needs to 
app = Flask(__name__)

# "/" should trigger hello_world()
@app.route("/")
def index()
    return <"p>index</p>"

@app.route("/hello_world")
def hello_world()
    return "<p>Hello, World!</p>"

[Suggested] Directory Structure

application
|-flaskr/               # application code boes here
|   __init__.py

Acknowledgements