Getting Started with the Flask Microframework

Flask is a popular open-source microframework for Python. This tutorial steps through the process of creating a Python application on OpenShift and adding the Flask microframework.

If you prefer not to follow the tutorial below, you can deploy this Flask QuickStart template instead, producing a slightly different Flask application.

Step 1: Create Python Application

Create a Python 2.7 application on OpenShift with the following terminal command. You will need an OpenShift account and RHC. More details about this process are covered in the Getting Started with Python tutorial.

$ rhc app create myflaskapp python-2.7

Flask can be used with Python 3 and this works on OpenShift, but the framework authors recommend using Python 2.

Once the RHC command has completed, a Git repository will have been cloned to your local machine in a directory called myflaskapp.

Step 2: Add Flask Dependency

Dependencies can be added to your Python application via requirements.txt or setup.py.

Open myflaskapp/requirements.txt in an editor and add a line so it looks like the following:

Flask==0.10.1

Flask will be available to your application on OpenShift when this change is pushed to the platform.

Step 3: Create Basic Flask App

Create a new file flaskapp.py in the Git repository, and add the following contents of the Flask 'Hello World' app:

from flask import Flask
app = Flask(__name__)

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

if __name__ == '__main__':
    app.run()

Step 4: Update Entry Point

The default entry point for the Python application is wsgi.py. The next step is to alter this file so that it serves the new Flask application.

Open wsgi.py in an editor, and delete the entire method def application(environ, start_response) (it is quite long). Replace the method with a line to import your Flask application, as shown below:

#
# IMPORTANT: Put any additional includes below this line.  If placed above this
# line, it's possible required libraries won't be in your searchable path
#

from flaskapp import app as application

#
# Below for testing only
#

If you want to be able to test your Flask app locally, there are a couple of other changes you should make to wsgi.py. Firstly, change the line defining virtenv to the following:

virtenv = os.path.join(os.environ.get('OPENSHIFT_PYTHON_DIR','.'), 'virtenv')

This code caters for cases when the OPENSHIFT_PYTHON_DIR environment variable is not available, such as on your local machine.

Next, update the line httpd.handle_request() to instead serve forever.

httpd.serve_forever()

With these changes in place and Flask installed on your local machine, you should be able to run your app locally with the following command:

$ python wsgi.py

The application will be available at http://localhost:8051. Use Control+C or a similar command to terminate the local server.

Step 5: Deploy to OpenShift

Deploy the Flask application to OpenShift using the standard Git process:

$ git add --all
$ git commit -m "Adding Flask application"
$ git push

Once the Git push has completed, you should see the 'Hello World!' message when you navigate to your application at http://myflaskapp-<yourdomain>.rhcloud.com.

Next Steps

You now have a Flask application running on OpenShift. To learn more about Flask, dive into the documentation.

This OpenShift Flask QuickStart gives an example of serving static files and a possible application layout. The QuickStart can be used as a template for quickly and easily creating Flask apps on OpenShift.

To learn how to add a database to your application, see the OpenShift database tutorial. Flask-SQLAlchemy is a popular Flask extension for working with databases using the SQLAlchemy Object Relational Mapper.