What is the main reason Flask developers use blueprints in large applications?
Think about how to keep code organized when the app grows bigger.
Blueprints help organize a large Flask app by dividing it into smaller parts. Each part can have its own routes, views, and static files, making the app easier to manage and reuse.
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?
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')
Remember how url_prefix affects blueprint routes.
The url_prefix adds a prefix to all routes in the blueprint. So the route /hello becomes /greet/hello when accessed.
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?
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?
Think about which blueprint's route overrides the other when registered later.
When two blueprints have the same route, the last registered blueprint's route overrides the previous one. So '/status' returns 'B status'.
Which of the following is the correct way to register a blueprint named admin_bp with a URL prefix /admin in a Flask app?
Check the parameter name for setting URL prefix in register_blueprint.
The correct parameter to set a URL prefix when registering a blueprint is url_prefix. The syntax is app.register_blueprint(blueprint, url_prefix='...').
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?
Think about how to avoid circular imports by changing when imports happen.
Delaying imports by moving them inside functions avoids circular import errors because the import happens only when needed, after all modules are loaded.