Performance: Blueprint routes and templates
This affects server response time and client rendering speed by organizing routes and templates efficiently.
from flask import Flask from myapp.main import main_bp app = Flask(__name__) app.register_blueprint(main_bp) # myapp/main.py from flask import Blueprint, render_template main_bp = Blueprint('main', __name__, template_folder='templates') @main_bp.route('/') def home(): return render_template('home.html') @main_bp.route('/about') def about(): return render_template('about.html')
from flask import Flask, render_template app = Flask(__name__) @app.route('/') def home(): return render_template('home.html') @app.route('/about') def about(): return render_template('about.html')
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Single app file with all routes | N/A (server-side) | N/A | N/A | [X] Bad |
| Blueprints with modular routes | N/A (server-side) | N/A | N/A | [OK] Good |
Blueprint in a Flask application?blog with a Flask app app using the URL prefix /blog?register_blueprint(blueprint, url_prefix='...').from flask import Blueprint, render_template
blog = Blueprint('blog', __name__, template_folder='templates')
@blog.route('/post')
def show_post():
return render_template('post.html')from flask import Flask app = Flask(__name__) app.register_blueprint(blog, url_prefix='/blog')
post.html template rendered?from flask import Flask app = Flask(__name__) app.register_blueprint(blog, prefix='/blog')
url_prefix, not prefix.admin and user, both with a route /dashboard. How can you register them in your Flask app to avoid URL conflicts and keep URLs clear?/dashboard, which causes conflicts if no prefix is used.url_prefix values like /admin and /user keeps URLs unique and organized.Pass the test and this pattern moves unknown