0
0
Flaskframework~20 mins

Blueprint best practices in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Blueprint Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
Blueprint URL Prefix Behavior
Consider a Flask Blueprint registered with a URL prefix. What will be the full URL path for a route defined as @bp.route('/dashboard') if the blueprint is registered with url_prefix='/admin'?
Flask
from flask import Blueprint
bp = Blueprint('admin', __name__, url_prefix='/admin')

@bp.route('/dashboard')
def dashboard():
    return 'Admin Dashboard'
A/admin/dashboard
B/dashboard
C/admin
D/dashboard/admin
Attempts:
2 left
💡 Hint
Think about how the url_prefix combines with route paths.
📝 Syntax
intermediate
1:30remaining
Correct Blueprint Registration Syntax
Which option correctly registers a blueprint named auth in a Flask app instance app?
Flask
from flask import Flask, Blueprint
app = Flask(__name__)
auth = Blueprint('auth', __name__)

# Registration code here
Aapp.register_blueprint(auth)
Bapp.register_blueprint('auth')
Capp.register_blueprint(auth, url_prefix='/auth')
Dapp.register_blueprint('auth', url_prefix='/auth')
Attempts:
2 left
💡 Hint
The first argument must be the blueprint object, not a string.
🔧 Debug
advanced
2:00remaining
Blueprint Import Error Debugging
You have a Flask app with multiple blueprints in separate files. When running the app, you get an ImportError related to circular imports. What is the best practice to avoid this issue?
Flask
File structure:
- app.py
- auth.py (defines auth blueprint)
- dashboard.py (defines dashboard blueprint)

Both auth.py and dashboard.py import app.py and each other.
AImport blueprints inside functions or after app creation to avoid circular imports.
BRemove all imports between blueprint files and put all routes in app.py.
CUse global variables to share app instance across files.
DRename blueprint files to avoid import conflicts.
Attempts:
2 left
💡 Hint
Think about when imports happen and how to delay them.
state_output
advanced
2:00remaining
Blueprint Template Folder Resolution
If a blueprint is created with Blueprint('shop', __name__, template_folder='templates') and the app renders a template with render_template('index.html') inside a blueprint route, where does Flask look for index.html?
Flask
shop = Blueprint('shop', __name__, template_folder='templates')

@shop.route('/')
def home():
    return render_template('index.html')
AIn the app's main templates folder only.
BIn both the app's templates folder and blueprint's templates folder, app folder takes priority.
CFlask raises a TemplateNotFound error because path is ambiguous.
DIn the blueprint's 'templates' folder relative to the blueprint file.
Attempts:
2 left
💡 Hint
Blueprints can have their own templates folder.
🧠 Conceptual
expert
2:30remaining
Blueprints and Application Factory Pattern
Why is it a best practice to register blueprints inside the application factory function rather than at the global level in a Flask app?
AIt prevents blueprints from being used outside the app context.
BIt allows creating multiple app instances with different configurations and avoids circular imports.
CIt automatically registers blueprints with URL prefixes without extra code.
DIt improves the app's runtime speed by preloading blueprints.
Attempts:
2 left
💡 Hint
Think about app creation and flexibility.