Blueprints

A Love Story

What's a Blueprint?

Blueprints are the recommended way to implement larger or more pluggable applications in Flask 0.7 and later.

Source: Flask source code

What's a Blueprint?

A Blueprint object works similarly to a Flask application object, but it is not actually an application. Rather it is a blueprint of how to construct or extend an application.

Source: Flask docs

What's a Blueprint?

Blueprints are "subapplications" that encapsulate related logic and the only way to sanely grow an application.

Source: Me

An Analogy to Django

Flask Application ≅ Django Project

Blueprint ≅ Django Application

Why Use Blueprints?

How Flask Applications Grow

A single file containing most of your application code

How Flask Applications Grow

A few files representing your MVC structure

How Flask Applications Grow

Blueprints!
  • Models, views, and templates for each Blueprint
  • Blueprints get reused between applications

Examples

Converting Your Code

Change this:

from ..core import app

...

@app.route('/users/')
def user_list():
    ...

Converting Your Code

To this:

from flask import Blueprint

bp = Blueprint('users', __name__, template_folder='templates')

...

@bp.route('/')
def user_list():
    ...

Converting Your Code

And add this:

from .users.views import bp as user_bp

...

app.register_blueprint(user_bp, url_prefix='/users')

Considerations

Blueprints All The Way Down

FIN.

(Questions?)

SpaceForward
Left, Down, Page DownNext slide
Right, Up, Page UpPrevious slide
POpen presenter console
HToggle this help