Blueprint static files let you organize images, CSS, and JavaScript inside parts of your Flask app. This keeps your project neat and easy to manage.
Blueprint static files in Flask
bp = Blueprint('name', __name__, static_folder='static', static_url_path='/static')
static_folder is the folder inside the blueprint where static files live.static_url_path is the URL prefix to access these static files in the browser.bp = Blueprint('blog', __name__, static_folder='static', static_url_path='/blog/static')
bp = Blueprint('admin', __name__, static_folder='assets')
This Flask app creates a blueprint named 'shop' with its own static folder. The home page returns a link tag to a CSS file inside the blueprint's static folder. The static file URL is generated using url_for with the blueprint's static endpoint.
from flask import Flask, Blueprint, url_for bp = Blueprint('shop', __name__, static_folder='static', static_url_path='/static') app = Flask(__name__) app.register_blueprint(bp, url_prefix='/shop') @app.route('/') def home(): # Generate URL for a static file inside the blueprint css_url = url_for('shop.static', filename='style.css') return f'<link rel="stylesheet" href="{css_url}">Hello from Shop!' if __name__ == '__main__': app.run(debug=True)
Static files in blueprints are served automatically by Flask when you set static_folder.
Use url_for('blueprint_name.static', filename='file.ext') to get the correct URL for static files.
Remember to register the blueprint with app.register_blueprint() for it to work.
Blueprint static files help keep your app organized by grouping static assets with their features.
Set static_folder and optionally static_url_path when creating a blueprint.
Use url_for with the blueprint's static endpoint to link to these files in templates or responses.
Practice
static_folder in a Flask blueprint?Solution
Step 1: Understand the role of
Thestatic_folderstatic_folderparameter tells Flask where to find static files like CSS or images for that blueprint.Step 2: Differentiate from other parameters
It does not affect routes or templates; it only points to the static files directory.Final Answer:
To specify the folder where the blueprint's static files are stored -> Option AQuick Check:
static_folder = static files location [OK]
- Confusing static_folder with template_folder
- Thinking static_folder sets URL prefix
- Assuming static_folder registers blueprint
assets?Solution
Step 1: Identify correct parameter for static files folder
Thestatic_folderparameter sets the folder name relative to the blueprint.Step 2: Check syntax correctness
Blueprint('bp', __name__, static_folder='assets') correctly usesstatic_folder='assets'without a leading slash, which is correct.Final Answer:
Blueprint('bp', __name__, static_folder='assets') -> Option AQuick Check:
static_folder='assets' is correct syntax [OK]
- Using static_url_path instead of static_folder for folder name
- Adding leading slash in static_folder path
- Confusing template_folder with static_folder
bp = Blueprint('bp', __name__, static_folder='static', static_url_path='/bp_static')What URL will serve the file
logo.png located inside the static folder of this blueprint?Solution
Step 1: Understand static_url_path usage
Thestatic_url_pathsets the URL prefix for serving static files of the blueprint.Step 2: Combine URL prefix with filename
Sincestatic_url_path='/bp_static', the filelogo.pngis served at/bp_static/logo.png.Final Answer:
/bp_static/logo.png -> Option BQuick Check:
static_url_path + filename = URL [OK]
- Assuming static_folder name appears in URL
- Using blueprint name as URL prefix by default
- Confusing static_url_path with static_folder
bp = Blueprint('bp', __name__, static_folder='static')
@app.route('/logo')
def logo():
return url_for('bp.static', filename='logo.png')When running the app, you get a
BuildError. What is the likely cause?Solution
Step 1: Understand BuildError in url_for
ABuildErrormeans Flask cannot find the endpoint namedbp.static.Step 2: Check blueprint registration
If the blueprintbpis not registered with the app, its static endpoint won't exist, causing the error.Final Answer:
The blueprintbpwas not registered with the Flask app -> Option DQuick Check:
Unregistered blueprint causes BuildError [OK]
- Forgetting to register blueprint with app
- Using wrong endpoint name in url_for
- Assuming file existence causes BuildError
bp1 with static files in static1 folder and URL prefix /static1, and bp2 with static files in static2 folder and URL prefix /static2. How do you correctly set up both blueprints to avoid URL conflicts and serve files properly?Solution
Step 1: Avoid URL conflicts by setting unique static_url_path
Each blueprint must have a uniquestatic_url_pathto prevent URL clashes.Step 2: Assign correct static_folder for each blueprint
Setstatic_folderto the folder containing that blueprint's static files.Step 3: Register both blueprints with the app
Only after registration will the static files be served at their URL prefixes.Final Answer:
Set static_folder='static1' and static_url_path='/static1' for bp1, and static_folder='static2' and static_url_path='/static2' for bp2, then register both blueprints. -> Option CQuick Check:
Unique static_url_path + correct static_folder + registration = correct setup [OK]
- Using same static_url_path for multiple blueprints
- Not setting static_url_path causing conflicts
- Forgetting to register blueprints
Ready to prove it?
Pass the test and this pattern moves unknown
