Flask-Bower provides a method to manage and serve bower installed packages. This simplifies javascript dependency management a lot.
To provide this, there is a flask blueprint to serve content from your bower_components directory and use url_for() for serving the files same as serving files form flask static folder.
pip install flask-bowerfrom flask_bower import Bower [...] Bower(app)
/bower url route.bower_components directory has to be inside the app directory (app/bower_components - like your static and templates directories). Another directory can be specified using BOWER_COMPONENTS_ROOTjquery with bower: bower install -S jqueryNow it should look like:
$ ls -1 app/bower_components/jquery MIT-LICENSE.txt bower.json dist src
To include and use this, you can use url_for():
<script src="{{ url_for('bower.static', filename='jquery/dist/jquery.js') }}"></script>
There are several configuration options to customize the behavior:
BOWER_COMPONENTS_ROOTdefault:
bower_componentsDirectory name containing your installed bower packages
BOWER_KEEP_DEPRECATEDdefault:
TrueKeep deprecated functions available
Note: deprecated functions will be removed in future versions
affected functions:
bower_url_for- please migrate tourl_for('bower.static', filename='component/path')
BOWER_QUERYSTRING_REVVINGdefault:
TrueAppend ?version= parameter to url (useful for cache busting by updates). It tries to detect the version in the following order:
- bower.json
- package.json (if available)
- file modification timestamp
BOWER_REPLACE_URL_FORdefault:
FalseReplace flasks
url_for()function in templates.This is useful - but not recommended - to build an "overlay" for the static folder.
Warning: Replacing
url_for()causes conflicts with other flask extensions likeflask-cdn, since only one extension can replaceurl_for()at a time and the last registered extension wins.BOWER_SUBDOMAINdefault:
NoneSubdomain to serve the content like
static(see flask blueprint documentation for subdomains)BOWER_TRY_MINIFIEDdefault:
TrueCheck if a minified version is available and serve this instead (check if a file with
<filename>.min.<ext>likejquery/dist/jquery.min.jsexists)BOWER_URL_PREFIXdefault:
/bowerCustomize the url prefix
This is now deprecated since it is a break of the development workflow due to the use of a different function thanurl_for(), which is the default for url handling in flask.Since v1.1.0 it is possible to use the defaulturl_for()function also for flask assets::url_for('bower.static', filename='component/path')Use of this new approach is recommended to all developers and to simplify the migration the
bower_url_for()function will stay available for a while; though it can be disabled to help migrating (seeBOWER_KEEP_DEPRECATED)