subDimension

Configuring Flask Externally

Or "In before Diablo III".

I finally deployed my system last week, just before Diablo III got released and gobbled up all my free time!

There are still a few features that I still want to implement, but the core functionality is there and I'm really pleased with it.

One issue I had some trouble with was configuring my app arbitrarily. The various examples pointed to in the Flask documentation all package the settings in with the application. I wanted to be able to use the same Python package to potentially run multiple sites.

My application is set up like this:

~/projects/
    pyDimension/
        pyDimension/
            __init__.py
            static/
                ...
            templates/
                ...
            access_control.py
            search.py
            system.py
            views.py
            site_settings.cfg
        startDevServer.py

~/public_html/pyDim_system/
    pyDim_system.wsgi

~/public_html/subDimension_website/
    {static HTML generated by system}

Basically, I wanted to be able to move the site_settings.cfg file into the pyDim_system folder, meaning I could create another site later using the same application code, but pointing at a different static website.

pyDimension is configured in the __init__.py file:

#!python
from flask import Flask

app = Flask(__name__)
app.config.from_pyfile('site_settings.cfg')

I'm not sure why, but app.config.from_pyfile() doesn't appear to follow any sensible environment states, it just seems to look for the file in whatever directory the application lives in, ignoring the Python path and the current working directory!

It can however be fed an absolute file path, so I removed that line from __init__.py and changed pyDim_system.wsgi:

#!python
import sys

sys.path.append('/<redacted>/pyDimension')

from pyDimension import app as application
application.config.from_pyfile('/<redacted>/public_html/subDimension_website/site_settings.cfg')

It's a little inelegant, but it means that configuration for the app can be done alongside configuration for the mod_wsgi script, and I can run multiple sites with the same app code in my projects directory.