Flask Snippets

Config-based static_folder

By Simon Sapin filed in Application Structure

A response to a question on the mailing-list: how replace the static_folder parameter by a config value?

import flask
class MyFlask(flask.Flask):
    @property
    def static_folder(self):
        if self.config.get('STATIC_FOLDER') is not None:
            return os.path.join(self.root_path, 
                self.config.get('STATIC_FOLDER'))
    @static_folder.setter
    def static_folder(self, value):
        self.config.get('STATIC_FOLDER') = value

# Now these are equivalent:
app = Flask(__name__, static_folder='foo')

app = MyFlask(__name__)
app.config['STATIC_FOLDER'] = 'foo'

However since the URL rule is still created in __init__ this only work for setting a different path, not for disabling it completely with None.

# Still no static URL rule, /static/foo.png gives HTTP 404
app = MyFlask(__name__, static_folder=None)
app.config['STATIC_FOLDER'] = 'foo'

# /static/foo.png gives HTTP 500 rather than 404:
app = Flask(__name__)
app.config['STATIC_FOLDER'] = None

This snippet by Simon Sapin can be used freely for anything you like. Consider it public domain.