Performance: Blueprint creation and registration
This affects the initial page load speed and server response time by organizing routes and handlers efficiently.
from flask import Flask, Blueprint user_bp = Blueprint('user', __name__, url_prefix='/user') @user_bp.route('/') def user_home(): return 'User page' admin_bp = Blueprint('admin', __name__, url_prefix='/admin') @admin_bp.route('/') def admin_home(): return 'Admin page' app = Flask(__name__) app.register_blueprint(user_bp) app.register_blueprint(admin_bp)
from flask import Flask app = Flask(__name__) @app.route('/user') def user(): return 'User page' @app.route('/admin') def admin(): return 'Admin page' # All routes defined in one file
| Pattern | Server Startup Time | Route Lookup Speed | Memory Usage | Verdict |
|---|---|---|---|---|
| All routes in one file | Higher due to large file parsing | Slower as route list grows | Higher due to monolithic structure | [X] Bad |
| Routes split into blueprints | Lower due to modular loading | Faster with organized routing | Lower due to modular scope | [OK] Good |
Blueprint in a Flask application?from flask import Flask, Blueprint
app = Flask(__name__)
hello_bp = Blueprint('hello', __name__)
@hello_bp.route('/hello')
def hello():
return 'Hello from Blueprint!'
app.register_blueprint(hello_bp)
if __name__ == '__main__':
app.run()from flask import Flask, Blueprint
app = Flask(__name__)
admin_bp = Blueprint('admin', __name__)
@admin_bp.route('/dashboard')
def dashboard():
return 'Admin Dashboard'
# Missing line here
if __name__ == '__main__':
app.run()app.register_blueprint(admin_bp).user_bp and admin_bp, each with a route '/profile'. How do you register them so that user_bp routes are under '/user' and admin_bp routes under '/admin' in the main app?url_prefix when registering Blueprints.user_bp with url_prefix='/user' and admin_bp with url_prefix='/admin'.Pass the test and this pattern moves unknown