0
0
FlaskHow-ToBeginner · 3 min read

How to Use Blueprint for Modular App in Flask

Use Blueprint in Flask to split your app into smaller modules by defining routes and views in separate files, then register these blueprints on the main app with app.register_blueprint(). This helps keep your code organized and easier to maintain.
📐

Syntax

A Blueprint is created by calling Blueprint(name, import_name). You define routes on this blueprint like a mini app. Then, in your main Flask app, you register the blueprint using app.register_blueprint(blueprint).

Parts explained:

  • Blueprint: Creates a modular component.
  • name: Unique name for the blueprint.
  • import_name: Usually __name__, tells Flask where the blueprint is defined.
  • app.register_blueprint(): Adds the blueprint routes to the main app.
python
from flask import Blueprint

mod = Blueprint('mod', __name__)

@mod.route('/')
def index():
    return 'Hello from blueprint!'

from flask import Flask
app = Flask(__name__)
app.register_blueprint(mod, url_prefix='/mod')

if __name__ == '__main__':
    app.run()
💻

Example

This example shows a simple modular Flask app with a blueprint named mod. The blueprint defines a route at / that returns a greeting. The main app registers this blueprint under the URL prefix /mod, so the route is accessible at /mod/.

python
from flask import Flask, Blueprint

# Define the blueprint in a separate module (e.g., mod.py)
mod = Blueprint('mod', __name__)

@mod.route('/')
def home():
    return 'Hello from the modular blueprint!'

# Create the main app
app = Flask(__name__)

# Register the blueprint with a URL prefix
app.register_blueprint(mod, url_prefix='/mod')

if __name__ == '__main__':
    app.run(debug=True)
Output
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) Accessing http://127.0.0.1:5000/mod/ shows: Hello from the modular blueprint!
⚠️

Common Pitfalls

Common mistakes when using blueprints:

  • Not registering the blueprint on the main app, so routes don’t work.
  • Forgetting to set url_prefix when you want to namespace routes.
  • Defining routes outside the blueprint or mixing app and blueprint routes confusingly.
  • Importing the blueprint incorrectly causing circular imports.

Always keep blueprint definitions in separate files and import them cleanly in your main app.

python
from flask import Flask, Blueprint

# Wrong: Defining route on app but forgetting to register blueprint
mod = Blueprint('mod', __name__)

@mod.route('/')
def home():
    return 'Hello!'

app = Flask(__name__)
# app.register_blueprint(mod)  # Forgot this line

if __name__ == '__main__':
    app.run()

# Fix: Add app.register_blueprint(mod) to enable blueprint routes
📊

Quick Reference

Blueprint usage tips:

  • Create a Blueprint instance per module.
  • Define routes and error handlers on the blueprint.
  • Register blueprints on the main app with optional url_prefix.
  • Keep blueprint files separate for clarity.
  • Use blueprints to organize large apps into smaller parts.

Key Takeaways

Blueprints let you split a Flask app into modular parts with their own routes.
Always register your blueprint on the main app using app.register_blueprint().
Use url_prefix to group blueprint routes under a common URL path.
Keep blueprint code in separate files to avoid circular imports and improve clarity.
Blueprints help maintain clean, organized, and scalable Flask applications.