0
0
Flaskframework~20 mins

Why blueprints organize large applications in Flask - Challenge Your Understanding

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!
🧠 Conceptual
intermediate
1:30remaining
Why use blueprints in Flask?

What is the main reason Flask developers use blueprints in large applications?

ATo store user data securely in the database
BTo speed up the app by compiling code into machine language
CTo automatically generate HTML templates without coding
DTo split the app into smaller, reusable parts with their own routes and views
Attempts:
2 left
💡 Hint

Think about how to keep code organized when the app grows bigger.

component_behavior
intermediate
1:30remaining
Blueprint route behavior in Flask

Given a Flask blueprint with a route defined as @bp.route('/hello'), what URL path will respond when the blueprint is registered with a prefix /greet?

Flask
from flask import Blueprint
bp = Blueprint('bp', __name__)

@bp.route('/hello')
def hello():
    return 'Hello!'

# In main app:
# app.register_blueprint(bp, url_prefix='/greet')
A/greet/hello
B/greet
C/bp/hello
D/hello
Attempts:
2 left
💡 Hint

Remember how url_prefix affects blueprint routes.

lifecycle
advanced
2:00remaining
Order of blueprint registration effects

In a Flask app, two blueprints define the same route /status. Blueprint A returns 'A status', Blueprint B returns 'B status'. If Blueprint A is registered before Blueprint B, what will /status return?

Flask
from flask import Flask, Blueprint

app = Flask(__name__)

bp_a = Blueprint('a', __name__)
@bp_a.route('/status')
def status_a():
    return 'A status'

bp_b = Blueprint('b', __name__)
@bp_b.route('/status')
def status_b():
    return 'B status'

app.register_blueprint(bp_a)
app.register_blueprint(bp_b)

# What does app.route('/status') return?
AB status
BA status
C404 Not Found
DRuntimeError due to route conflict
Attempts:
2 left
💡 Hint

Think about which blueprint's route overrides the other when registered later.

📝 Syntax
advanced
1:30remaining
Correct blueprint registration syntax

Which of the following is the correct way to register a blueprint named admin_bp with a URL prefix /admin in a Flask app?

Aapp.register_blueprint(admin_bp, prefix='/admin')
Bapp.register_blueprint('/admin', admin_bp)
Capp.register_blueprint(admin_bp, url_prefix='/admin')
Dapp.register_blueprint(admin_bp, url='/admin')
Attempts:
2 left
💡 Hint

Check the parameter name for setting URL prefix in register_blueprint.

🔧 Debug
expert
2:30remaining
Debugging blueprint import error

You have a Flask app with two blueprints in separate files. When running the app, you get an ImportError related to circular imports. What is the best way to fix this issue?

ARemove one blueprint to avoid circular import
BMove blueprint imports inside the function that registers them to delay import
CRename one blueprint to avoid name conflict
DUse global variables to share data between blueprints
Attempts:
2 left
💡 Hint

Think about how to avoid circular imports by changing when imports happen.