Flask Snippets

Snippets are unofficial and unmaintained.

This is an archived view of user-submitted snippets. Despite being hosted on the Flask site, they are not official. No Flask maintainer has curated or checked the snippets for security, correctness, or design.

Config-based static_folder

Posted by Simon Sapin on 2012-10-05 @ 12:02 and 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):
    def static_folder(self):
        if self.config.get('STATIC_FOLDER') is not None:
            return os.path.join(self.root_path, 
    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.