Flask for large apps: using blueprints


One of the most complex features of Flask, in my opinion, is the use of Blueprints, very neccesary for large apps, or whenever you need your code to be well organized.

The first detail is that the code organization changes a lot. Let’s see the distribution of files of the previous example:

Organización inicial de proyecto Flask

Organización inicial de proyecto Flask

As you see, for using Flask, we need a templates and static directories.

Before to go on, I will give you two recommendations to go further with Flask. You can see several videos and tutorials on realpython, and of course, you can read Miguel Grinberg website (if you have more time, you can see his talk on PyCon 2014 on Youtube)

Config file

Thr truth is that every Flask project should have a config file to save all neccesary data for the app, such as secret key, where are the database, debug mode, ….. This is the config.py:

import os

class Config:
    SECRET_KEY = os.environ.get('SECRET_KEY')

class Development(Config):
    DEBUG = True
    SECRET_KEY = os.environ.get('SECRET_KEY') or 'clave'
class Testing(Config):
    TESTING = True

class Production(Config):

config = {
    'development': Development,
    'testing': Testing,
    'production': Production,
    'default': Development

The use of this file config.py is very interesting, becuase once the code is on the production server, with one change, every thing changes (of course, for security reasons).


In order to launch the app, you need to create run.py, with a very basic code: you define the config file for the app, and you’re done!

from app import create_app

app = create_app('default')

The app directory

Next step, you need to create the app dir, and here is where the static and templates dirs will be included. You already know what to store on these directories. In the first one, all the css and javascript, images, … and on the second, HTML templates.

Getting access to the content of this dir by Python can only obtained only by creating the __init__.py, where you define the functions used on run.py for creating the app object and the configuration:

from flask import Flask
from config import config

def create_app(config_name):
    app = Flask(__name__)

    # Configuracion de los BluePrints
    from .miblueprint import miblue as miblueprint

    return app

Also, on this file is where Blueprints are registered.

What are Blueprints

Blueprints are modules used to create Flask apps. Blueprints objects are similar to a Flask object, but they are different because an app only can have one Flask object while having several Blueprints. The advantage of this is when creating large apps, you can redistribute the code on several files, well organized, instead of having all the code on a single file.

So, let’s create a blueprint creating a directory called miblueprint, and inside this, you create the templates dir (static is not needed in all blueprints). As before, you need to create the init file __init__.py, where you define the Blueprint object, ist name, and you define the template directory:

from flask import Blueprint
miblue = Blueprint('miblue', __name__, template_folder='templates')
from . import routes

Last line is very important, becuase you requires routes.py, a file where you define the routes dealing with this Blueprint.

from flask import Flask, render_template, url_for
from . import miblue

def index():
return render_template('index2.html')

def saludo():
return render_template('saludo.html')

def user(username):
return render_template('user.html', username=username)

And that’s all. You have a new organization using Blueprint, with this new structure:

flask con blueprints

flask with blueprints

A base template

Almost all of the code you have seen here can be reuse for future projects, so, in my GitHub account you have a template you can use, and of course, if you have any recommendation, just email me.

And … that’s all by now. Happy coding!