0
0
Flaskframework~20 mins

Blueprint creation and registration 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
2:00remaining
What is the output when accessing the registered blueprint route?
Given the Flask app with a blueprint registered under '/shop', what will be the response when visiting '/shop/items'?
Flask
from flask import Flask, Blueprint

shop_bp = Blueprint('shop', __name__)

@shop_bp.route('/items')
def items():
    return 'Shop Items List'

app = Flask(__name__)
app.register_blueprint(shop_bp, url_prefix='/shop')

# What is the output of a GET request to '/shop/items'?
AShop Items List
B404 Not Found
CInternal Server Error
DMethod Not Allowed
Attempts:
2 left
💡 Hint
Think about how url_prefix affects blueprint routes.
📝 Syntax
intermediate
2:00remaining
Which option correctly creates and registers a blueprint?
Select the code snippet that correctly creates a Flask blueprint named 'admin' and registers it with the app under '/admin'.
A
admin_bp = Blueprint('admin')
app = Flask(__name__)
app.register_blueprint(admin_bp, url_prefix='/admin')
B
admin_bp = Blueprint('admin', __name__)
app = Flask(__name__)
app.register_blueprint(admin_bp, url_prefix='/admin')
C
admin_bp = Blueprint('admin', __name__)
app = Flask(__name__)
app.register_blueprint(admin_bp, prefix='/admin')
D
admin_bp = Blueprint('admin', __name__)
app = Flask(__name__)
app.register_blueprint(admin_bp, url='/admin')
Attempts:
2 left
💡 Hint
Check the Blueprint constructor and register_blueprint parameters.
🔧 Debug
advanced
2:00remaining
Why does this blueprint route cause a 404 error?
Consider this code snippet: from flask import Flask, Blueprint api = Blueprint('api', __name__) @api.route('/data') def data(): return 'API Data' app = Flask(__name__) # Missing blueprint registration # What happens when you visit '/data' and why?
A200 OK with response 'API Data' because blueprint routes are auto-registered
B500 Internal Server Error due to missing route handler
C404 Not Found because the blueprint was never registered with the app
D405 Method Not Allowed because GET is not allowed
Attempts:
2 left
💡 Hint
Think about what happens if you define a blueprint but don't register it.
state_output
advanced
2:00remaining
What is the output of this blueprint route using url variables?
Given the blueprint and route below, what is the output when visiting '/blog/post/42'?
Flask
from flask import Flask, Blueprint

blog_bp = Blueprint('blog', __name__)

@blog_bp.route('/post/<int:post_id>')
def show_post(post_id):
    return f'Post ID: {post_id}'

app = Flask(__name__)
app.register_blueprint(blog_bp, url_prefix='/blog')
APost ID: post/42
BPost ID: <int:post_id>
C404 Not Found
DPost ID: 42
Attempts:
2 left
💡 Hint
Look at how url variables are passed to the route function.
🧠 Conceptual
expert
3:00remaining
What happens if two blueprints have overlapping routes without url_prefix?
If you create two blueprints, both defining a route for '/' and register both without url_prefix, what will happen when you visit '/' in the Flask app?
AThe route from the blueprint registered last will override the first, so only one response is served
BBoth routes will be merged and served together in the response
CFlask will raise a runtime error due to route conflict
DBoth routes will be served alternately on each request
Attempts:
2 left
💡 Hint
Think about how Flask handles route conflicts in registration order.