Deploying a Flask App on

December 10, 2011 in Flask, Python, Technical

Documenting my experience deploying a non-trivial Flask app to (in particular, one that needed custom per instance config and DB access).

I wanted to deploy an instance of PyBossa to for experimenting and demo purposes.

PyBossa is a Flask app. have docs for Flask. However, the documentation is rather sparse and does not cover how to set up DB — tricky because provide DB connection info in a local python module — and do not cover, more generally, how to allow your Flask app access to standard Flask custom config.

The Process

Here’s how I did it.

  • Ensure your Flask App allows itself (using app.config.from_envvar) to get path to a custom config from an environment variable. E.g. in PyBossa have:

    app.config.from_envvar('PYBOSSA_SETTINGS', silent=True)
  • Create a (you could call this anything) as per a normal custom settings file. However we need to get DB info from bundle_config system.

    I’m using SQLAlchemy and have its config stored in SQLALCHEMY_DATABASE_URI variable but this is easy to change if you have a different set up. So what we need to do is grab’s config info from bundle_config (see postgres service docs and use it to set our applications DB info:

    from bundle_config import config
    SQLALCHEMY_DATABASE_URI = 'postgresql://%s:%s@%s:%s/%s' % (
  • Finally, we need to have set up a database and set and environment variable so our app will pick up our config file. To do this edit your epio.ini file and add:

    postgres = true

    Note that:

    • You will need to change PYBOSSA_SETTINGS to whatever environment variable you used in your app.config.from_pyvar (see abbove)
    • We have to use a relative path (you can work out the absolute path to your app by doing something like epio run_command pwd. However, this directory seems to change from moment to moment!)
  • Finally: let’s upload our work (automatically reloading the server):

    epio upload

And you can see it all working at:

Other tips and tricks

  • You can run commands via run_command. For example, to initialize the PyBossa database we need to use the script (also uploaded to the server):

    epio run_command "./ db_create"

Leave a reply

Your email address will not be published. Required fields are marked *