For development, I'd like to serve static files from the blueprint's static folder. However, I don't want to load all files via the url prefix of the blueprint. Is there a possibility to configure flask, so it looks in all blueprint's static folders and returns the first file it finds? The reason for this is that when I deploy the application, the build process will pull all the files our of the blueprints and put them into a separate folder (similar to Django's collectstatics).
In other words, I have 2 blueprints: foo
and bar
:
app
+--- foo
| +--- assets
| | +--- css
| | |
| | +--- js
| +
| |
| +--- __init__.py: foo_blueprint = Blueprint('foo', __name__, static_folder='assets')
|
+--- bar
+
|
+--- __init__.py: bar_blueprint = Blueprint('bar', __name__, static_folder='assets')
I want all js files in the particular js
subfolders to be available from the url /static/js/file.js
. Is this possible?
You can't with default
flask
mechanism.In your your example you will create 3 rules for static forlder:
When flask will match your URL to rule it take first match and return it. In this case it return
static
endpoint rule. After will dispatched function for this endpoint, e.g. one folder.PS. I don't sure that exactly
static
endpoint, better checkMap.update
method.But you always can write own request descriptor, which will look at blue prints folders:
PS. This example doesn't include the blueprint registration sequence, because it stored in dict.
But if you have two files with same name, then first file will processed. For example if you have next structure:
And include scripts to page:
You will have next result:
For js and css can concatenate files with same path, but can't do this for images.
However I prefer use unique URL prefix for each blueprint, because it simple with
url_for
.