Bird
Raised Fist0
Flaskframework~5 mins

Blueprint static files in Flask

Choose your learning style10 modes available
Introduction

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.

You want to keep static files related to a feature separate from the main app files.
You are building a large Flask app with multiple sections or modules.
You want to reuse a blueprint with its own static files in different projects.
You want to avoid mixing all static files in one folder to reduce confusion.
Syntax
Flask
bp = Blueprint('name', __name__, static_folder='static', static_url_path='/static')
The static_folder is the folder inside the blueprint where static files live.
The static_url_path is the URL prefix to access these static files in the browser.
Examples
This creates a blueprint named 'blog' with its own static files served under '/blog/static'.
Flask
bp = Blueprint('blog', __name__, static_folder='static', static_url_path='/blog/static')
This blueprint uses 'assets' folder for static files and defaults the URL path to '/admin/static'.
Flask
bp = Blueprint('admin', __name__, static_folder='assets')
Sample Program

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.

Flask
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)
OutputSuccess
Important Notes

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.

Summary

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

(1/5)
1. What is the main purpose of setting static_folder in a Flask blueprint?
easy
A. To specify the folder where the blueprint's static files are stored
B. To define the URL prefix for all routes in the blueprint
C. To register the blueprint with the Flask app
D. To set the template folder for the blueprint

Solution

  1. Step 1: Understand the role of static_folder

    The static_folder parameter tells Flask where to find static files like CSS or images for that blueprint.
  2. Step 2: Differentiate from other parameters

    It does not affect routes or templates; it only points to the static files directory.
  3. Final Answer:

    To specify the folder where the blueprint's static files are stored -> Option A
  4. Quick Check:

    static_folder = static files location [OK]
Hint: static_folder always points to static files folder [OK]
Common Mistakes:
  • Confusing static_folder with template_folder
  • Thinking static_folder sets URL prefix
  • Assuming static_folder registers blueprint
2. Which of the following is the correct way to create a Flask blueprint with static files located in a folder named assets?
easy
A. Blueprint('bp', __name__, static_folder='assets')
B. Blueprint('bp', __name__, static_url_path='assets')
C. Blueprint('bp', __name__, template_folder='assets')
D. Blueprint('bp', __name__, static_folder='/assets')

Solution

  1. Step 1: Identify correct parameter for static files folder

    The static_folder parameter sets the folder name relative to the blueprint.
  2. Step 2: Check syntax correctness

    Blueprint('bp', __name__, static_folder='assets') correctly uses static_folder='assets' without a leading slash, which is correct.
  3. Final Answer:

    Blueprint('bp', __name__, static_folder='assets') -> Option A
  4. Quick Check:

    static_folder='assets' is correct syntax [OK]
Hint: Use static_folder='foldername' without leading slash [OK]
Common Mistakes:
  • Using static_url_path instead of static_folder for folder name
  • Adding leading slash in static_folder path
  • Confusing template_folder with static_folder
3. Given this blueprint setup:
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?
medium
A. /static/logo.png
B. /bp_static/logo.png
C. /bp/logo.png
D. /bp_static/static/logo.png

Solution

  1. Step 1: Understand static_url_path usage

    The static_url_path sets the URL prefix for serving static files of the blueprint.
  2. Step 2: Combine URL prefix with filename

    Since static_url_path='/bp_static', the file logo.png is served at /bp_static/logo.png.
  3. Final Answer:

    /bp_static/logo.png -> Option B
  4. Quick Check:

    static_url_path + filename = URL [OK]
Hint: static_url_path sets URL prefix for blueprint static files [OK]
Common Mistakes:
  • Assuming static_folder name appears in URL
  • Using blueprint name as URL prefix by default
  • Confusing static_url_path with static_folder
4. You have this blueprint code:
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?
medium
A. The filename logo.png does not exist
B. The static_folder path is incorrect
C. The url_for call should use 'static' instead of 'bp.static'
D. The blueprint bp was not registered with the Flask app

Solution

  1. Step 1: Understand BuildError in url_for

    A BuildError means Flask cannot find the endpoint named bp.static.
  2. Step 2: Check blueprint registration

    If the blueprint bp is not registered with the app, its static endpoint won't exist, causing the error.
  3. Final Answer:

    The blueprint bp was not registered with the Flask app -> Option D
  4. Quick Check:

    Unregistered blueprint causes BuildError [OK]
Hint: Always register blueprint before using its static endpoint [OK]
Common Mistakes:
  • Forgetting to register blueprint with app
  • Using wrong endpoint name in url_for
  • Assuming file existence causes BuildError
5. You want to serve static files from two blueprints: 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?
hard
A. Set static_folder only for bp1 and leave bp2 without static files.
B. Use the same static_url_path for both blueprints but different static_folder names.
C. 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.
D. Register both blueprints without setting static_url_path to use default static URL.

Solution

  1. Step 1: Avoid URL conflicts by setting unique static_url_path

    Each blueprint must have a unique static_url_path to prevent URL clashes.
  2. Step 2: Assign correct static_folder for each blueprint

    Set static_folder to the folder containing that blueprint's static files.
  3. Step 3: Register both blueprints with the app

    Only after registration will the static files be served at their URL prefixes.
  4. 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 C
  5. Quick Check:

    Unique static_url_path + correct static_folder + registration = correct setup [OK]
Hint: Use unique static_url_path for each blueprint's static files [OK]
Common Mistakes:
  • Using same static_url_path for multiple blueprints
  • Not setting static_url_path causing conflicts
  • Forgetting to register blueprints
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard