Deploying a Flask App on ep.io

December 10, 2011 in Flask, Python, Technical

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

I wanted to deploy an instance of PyBossa to http://ep.io/ for experimenting and demo purposes.

PyBossa is a Flask app. ep.io have docs for Flask. However, the documentation is rather sparse and does not cover how to set up DB — tricky because ep.io 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 settings_epio.py (you could call this anything) as per a normal custom settings file. However we need to get DB info from ep.io 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 ep.io’s config info from bundle_config (see ep.io 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' % (
        config['postgres']['username'],
        config['postgres']['password'],
        config['postgres']['host'],
        int(config['postgres']['port']),
        config['postgres']['database']
        )
    
  • Finally, we need to have ep.io 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:

    [services]
    postgres = true
    [env]
    PYBOSSA_SETTINGS = ../settings_epio.py
    

    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: http://pybossa.ep.io/

Other tips and tricks

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

    epio run_command "./cli.py db_create"
    

Leave a reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <section align="" class="" dir="" lang="" style="" xml:lang=""> <style media="" type="" scoped="">