Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More than one static path in local Flask instance

Tags:

python

html

flask

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.

like image 952
Sergei Basharov Avatar asked Mar 01 '12 08:03

Sergei Basharov


People also ask

Where do I put static files in a Flask?

To use static files in a Flask application, create a folder called static in your package or next to your module and it will be available at /static on the application. app.py is a basic example of Flask with template rendering.

What should be in a static folder Flask?

Folder structure for a Flask app That folder contains two folders, specifically named static and templates. The static folder contains assets used by the templates, including CSS files, JavaScript files, and images.

What is a static folder?

Static files are files that don't change when your application is running. These files do a lot to improve your application, but they aren't dynamically generated by your Python web server. In a typical web application, your most common static files will be the following types: Cascading Style Sheets, CSS.


1 Answers

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 ;)

like image 122
plaes Avatar answered Sep 18 '22 16:09

plaes