Building Serverless Python Web Services with Zappa
上QQ阅读APP看书,第一时间看更新

Blueprint

Before diving into a detailed description of each file, let's have a look at the instantiation Flask mechanism. As you already know, we are creating a submodule as an independent module under the root module. Flask introduces the concept of blueprint for making the submodule components under a common pattern.

The Flask blueprint instance is very similar to a Flask instance, but it's not an application object. Instead, it has the ability to construct and extend the parent application. With the help of blueprint, you can design a modular application.

The following is a code snippet of the Blueprint instantiation in the auth/__init__.py file:

from flask import Blueprint
auth = Blueprint('auth', __name__)
from . import views

As you can see, it has very similar characteristics to the Flask class and follows a similar pattern. Now, we will be using the auth instance of the blueprint in views to register routes. To execute the application, we need to bind the blueprint object with the Flask application instance.

The following is the code snippet from the app/__init__.py file where we are going to create the Flask application instance:

from .auth import auth as auth_blueprint
from app.config import config

app = Flask(__name__)
app.config.from_object(config[environment])

app.register_blueprint(auth_blueprint, url_prefix='/auth')

With the help of the register_blueprint method, we are registering the auth module blueprint and we can add the URL prefix as well. We will have the complete description of this file after we look at the todo module explanation.