ποΈ Flask Blueprints: Building Your App Like LEGO Blocks
π The Story: Your Flask App is Growing Up!
Imagine you have a toy box with ALL your toys insideβcars, dolls, blocks, action figuresβeverything mixed together. When you want to find your favorite car, you dig through EVERYTHING. Messy, right?
Now imagine you have separate boxes: one for cars π, one for dolls π, one for blocks π§±. Finding anything becomes super easy!
Flask Blueprints are like those separate boxes. They help you organize your growing Flask app into neat, tidy pieces!
π What Are Blueprints?
A Blueprint is a mini Flask app that doesnβt run by itself. Think of it like a room in a house:
- The house = Your main Flask app
- Each room = A Blueprint (kitchen, bedroom, bathroom)
- Each room has its own stuff but belongs to the house
# A Blueprint is like saying:
# "Hey, I'm the KITCHEN room!"
# "I handle all cooking stuff!"
Why Use Blueprints?
| Without Blueprints π΅ | With Blueprints π |
|---|---|
| All code in one file | Code in separate files |
| Hard to find things | Easy to find things |
| Team canβt work together | Team works on different parts |
| Messy like a toy pile | Organized like labeled boxes |
π§ Creating Blueprints
Creating a Blueprint is like naming a new room in your house!
# In auth/routes.py
from flask import Blueprint
# Create a Blueprint named 'auth'
auth_bp = Blueprint('auth', __name__)
# Now auth_bp is your "auth room"!
The Magic Words Explained:
Blueprint('auth', __name__)
# β β
# name where am I?
'auth'= The name of this Blueprint (like βkitchenβ)__name__= Tells Flask where this Blueprint lives
Real Example - User Authentication Blueprint:
# auth/routes.py
from flask import Blueprint
auth_bp = Blueprint('auth', __name__)
# This blueprint handles:
# - Login
# - Logout
# - Sign up
π Registering Blueprints
Creating a Blueprint alone does nothingβlike buying furniture but leaving it in the truck! You must register it with your main app.
# app.py (main file)
from flask import Flask
from auth.routes import auth_bp
app = Flask(__name__)
# REGISTER the blueprint!
app.register_blueprint(auth_bp)
# Now auth_bp is part of your app! π
Think of it like:
graph TD A["π Main Flask App"] --> B["π¦ Register Blueprint"] B --> C["β Blueprint Now Active!"]
π€οΈ Blueprint Routes
Routes inside a Blueprint work exactly like normal routesβbut they belong to that Blueprintβs βroomβ!
# auth/routes.py
from flask import Blueprint
auth_bp = Blueprint('auth', __name__)
@auth_bp.route('/login')
def login():
return 'Login Page!'
@auth_bp.route('/logout')
def logout():
return 'Goodbye!'
@auth_bp.route('/signup')
def signup():
return 'Create Account!'
What Just Happened?
| Route | What it does |
|---|---|
/login |
Shows login page |
/logout |
Says goodbye |
/signup |
Create new account |
All these routes belong to auth_bpβtheyβre organized together!
π¨ Blueprint Templates
Each Blueprint can have its own templates folderβlike each room having its own decorations!
Folder Structure:
my_app/
βββ app.py
βββ auth/
β βββ __init__.py
β βββ routes.py
β βββ templates/ β Auth's templates!
β βββ login.html
βββ blog/
βββ __init__.py
βββ routes.py
βββ templates/ β Blog's templates!
βββ post.html
Setting Up Template Folder:
# auth/routes.py
auth_bp = Blueprint(
'auth',
__name__,
template_folder='templates' # β Magic!
)
@auth_bp.route('/login')
def login():
return render_template('login.html')
How Flask Finds Templates:
graph TD A["render_template"] --> B{Check Blueprint folder} B -->|Found| C["β Use it!"] B -->|Not Found| D{Check main templates} D -->|Found| E["β Use it!"] D -->|Not Found| F["β Error!"]
π Blueprint Static Files
Static files are things that donβt changeβimages, CSS, JavaScript. Each Blueprint can have its own!
Folder Structure:
my_app/
βββ auth/
β βββ routes.py
β βββ static/ β Auth's static files!
β βββ style.css
β βββ logo.png
Setting Up Static Folder:
# auth/routes.py
auth_bp = Blueprint(
'auth',
__name__,
template_folder='templates',
static_folder='static' # β Add this!
)
Using Blueprint Static Files in HTML:
<!-- In auth/templates/login.html -->
<link rel="stylesheet"
href="{{ url_for('auth.static',
filename='style.css') }}">
Key Point:
Use 'auth.static' not just 'static'βthe Blueprint name comes first!
π·οΈ URL Prefixes for Blueprints
Hereβs the superpower: You can add a prefix to ALL routes in a Blueprint!
Without Prefix:
# Routes are: /login, /logout, /signup
app.register_blueprint(auth_bp)
With Prefix:
# Routes become: /auth/login, /auth/logout
app.register_blueprint(
auth_bp,
url_prefix='/auth'
)
Real-World Example:
# app.py
from flask import Flask
from auth.routes import auth_bp
from blog.routes import blog_bp
from shop.routes import shop_bp
app = Flask(__name__)
# Register with prefixes
app.register_blueprint(auth_bp, url_prefix='/auth')
app.register_blueprint(blog_bp, url_prefix='/blog')
app.register_blueprint(shop_bp, url_prefix='/shop')
Now Your URLs Are:
| Blueprint | Prefix | Example Routes |
|---|---|---|
| auth_bp | /auth |
/auth/login, /auth/signup |
| blog_bp | /blog |
/blog/post, /blog/new |
| shop_bp | /shop |
/shop/cart, /shop/buy |
Visual Map:
graph TD A["π myapp.com"] --> B["/auth"] A --> C["/blog"] A --> D["/shop"] B --> E["/auth/login"] B --> F["/auth/signup"] C --> G["/blog/posts"] C --> H["/blog/new"] D --> I["/shop/cart"] D --> J["/shop/buy"]
π― Complete Example: Putting It All Together!
Project Structure:
my_awesome_app/
βββ app.py β Main app
βββ auth/
β βββ __init__.py
β βββ routes.py β Auth Blueprint
β βββ templates/
β β βββ login.html
β βββ static/
β βββ auth.css
βββ blog/
βββ __init__.py
βββ routes.py β Blog Blueprint
βββ templates/
βββ post.html
auth/routes.py:
from flask import Blueprint
from flask import render_template
auth_bp = Blueprint(
'auth',
__name__,
template_folder='templates',
static_folder='static'
)
@auth_bp.route('/login')
def login():
return render_template('login.html')
@auth_bp.route('/logout')
def logout():
return 'Logged out!'
blog/routes.py:
from flask import Blueprint
from flask import render_template
blog_bp = Blueprint(
'blog',
__name__,
template_folder='templates'
)
@blog_bp.route('/')
def all_posts():
return 'All blog posts!'
@blog_bp.route('/new')
def new_post():
return 'Write new post!'
app.py:
from flask import Flask
from auth.routes import auth_bp
from blog.routes import blog_bp
app = Flask(__name__)
# Register blueprints with prefixes
app.register_blueprint(auth_bp, url_prefix='/auth')
app.register_blueprint(blog_bp, url_prefix='/blog')
@app.route('/')
def home():
return 'Welcome Home!'
if __name__ == '__main__':
app.run(debug=True)
π Quick Summary
| Concept | What It Does | Example |
|---|---|---|
| Create | Make a new Blueprint | Blueprint('name', __name__) |
| Register | Add Blueprint to app | app.register_blueprint(bp) |
| Routes | Define paths in Blueprint | @bp.route('/path') |
| Templates | Blueprintβs own HTML files | template_folder='templates' |
| Static | Blueprintβs own CSS/JS/images | static_folder='static' |
| Prefix | Add prefix to all routes | url_prefix='/api' |
π You Did It!
Now you know how to organize your Flask app like a pro! Blueprints keep your code clean, your team happy, and your app ready to grow!
Remember: Blueprints are rooms in your house. Each room has its own stuff, but they all belong to the same home! π
