More than one static path in local Flask instance

Is that possible to add more static paths for my local dev Flask instance? I want to have default static folder for storing js/css/images files for the site and another folder, e.g. designs to keep my specific assets. I don't want to place designs folder inside static if there is a better solution exists.


I have been using following approach:

# Custom static data
@app.route('/cdn/<path:filename>')
def custom_static(filename):
    return send_from_directory(app.config['CUSTOM_STATIC_PATH'], filename)

The CUSTOM_STATIC_PATH variable is defined in my configuration.

And in templates:

{{ url_for('custom_static', filename='foo') }}

Caveat emptor - I'm not really sure whether it's secure ;)


You can use a Blueprint with its own static dir http://flask.pocoo.org/docs/blueprints/

Blueprint

blueprint = Blueprint('site', __name__, static_url_path='/static/site', static_folder='path/to/files')
app.register_blueprint(blueprint)

Template

{{ url_for('site.static', filename='filename') }}

If i have images stored in images dir like following

images/
  dir1/
    image1.jpg
  dir2/
    image2.jpg

then we can access these images with following url: http://localhost:5000/images/dir1/image1.jpg with the help of below code

from flask import Flask, send_from_directory


app = Flask(__name__, static_folder='main_static_dir')


@app.route('/images/<path:filename>')
def base_static(filename):
    return send_from_directory(app.root_path + '/images/', filename)


if __name__ == '__main__':
    app.run(debug=True)

I wanted to expand on the accepted answer above for anyone who was wondering what app.config['CUSTOM_STATIC_PATH'] was being set to.

In my case I needed a /.well-known dir and so here's what I used:

I placed a new dir in my app root called well-known.
I set a config var like so:

CUSTOM_STATIC_PATH=app.root_path + '/well-known/'

I then used that var as such:

@app.route('/.well-known/<path:filename>')
def wellKnownRoute(filename):
    return send_from_directory(app.config['CUSTOM_STATIC_PATH'], filename, conditional=True)

Setting conditional=True is smart, this will 404 any requests where the file does not exist.

And of course if you're wondering why you need to set a config value for the dir path, you don't. You can always just use the config's value in place, giving you this instead:

@app.route('/.well-known/<path:filename>')
def wellKnownRoute(filename):
    return send_from_directory(app.root_path + '/well-known/', filename, conditional=True)

My files for the /.well-known URL path were always going to come from the app root /well-known dir so no point in making it more complicated.

And to note, you would be better off handling this particular case from nginx or whatever server you're using by setting up an additional server block and serving the file from outside the app, I just needed this setup for a dev env for testing.