Blueprints and the App Factory
The blueprint is the module: the Blueprint class (the 1, the url_prefix, the no the 1), the register_blueprint (the app, the no the the), and the factory (the create_app, the no the 1 the module). The factory is the no (the 1 the the, the config, the no the the), the blueprint is the no (the 1 the no the import, the no the the), and the extension is the no (the init_app, the 1, the no the the). This chapter covers the Blueprint (the 1, the no the 1), the register_blueprint (the no the the), the create_app (the no the 1), and the extension (the init_app, the no the the).
The Blueprint and the register
# the BLUEPRINT (the 1, the no
# the 1, the no the the):
from flask import Blueprint
bp = Blueprint("blog", __name__,
url_prefix="/blog")
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no):
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no):
@bp.route("/")
def index():
return "the blog"
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
@bp.route("/post/<int:id>")
def post(id):
return f"the post {id}"
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the REGISTER (the app, the no
# the the, the no the 1):
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no):
from flask import Flask
app = Flask(__name__)
# the no: the no (the no,
# the no, the no
# the no, the no
# the no):
app.register_blueprint(bp)
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
The Blueprint is the 1: the name (the blog, the no the 1), the import_name (the __name__, the no the the), and the url_prefix (the /blog, the no the 1) - the @bp.route is the no (the app, the no the the, the 1 the the), and the endpoint is the no (the bp, the name, the no the 1) - the register_blueprint is the no the the: the 1 argument (the bp, the no the 1), the url_prefix is the no (the override, the no the 1), and the no is the 1 (the no the 1, the no the the) - the factory is the next: the create_app the no the 1.
The create_app and the extension
# the FACTORY (the no the 1,
# the config, the no the the):
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no):
import os
from flask import Flask
def create_app(config_name=
"default"):
from .blog import bp
app = Flask(__name__)
# the no: the no (the no,
# the no, the no
# the no, the no
# the no):
app.config["SECRET_KEY"] = \
os.environ.get("SECRET_KEY")
# the no: the no (the no,
# the no, the no
# the no, the no
# the no):
app.register_blueprint(bp)
# the no: the no (the no,
# the no, the no
# the no, the no
# the no):
return app
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the EXTENSION (the init_app,
# the 1, the no the the,
# the no the 1):
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no):
db = SQLAlchemy()
# the no: the no (the no,
# the no, the no
# the no, the no
# the no):
migrate = Migrate()
# the no: the no (the no,
# the no, the no
# the no, the no
# the no):
login = LoginManager()
# the no: the no (the no,
# the no, the no
# the no, the no
# the no):
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
The create_app is the no the 1: the 1 argument (the config, the no the 1), the app.config.from_object is the no (the 1, the no the the), and the return app is the no (the 1, the no the 1) - the no is the 1 (the no the 1, the no the the), and the no is the 1 (the no the 1, the no the the). The extension is the init_app: the db = SQLAlchemy() is the no (the app, the no the 1), the db.init_app(app) is the 1 (the no the the, the no the 1), and the no is the 1 (the no the 1, the no the the) - the rule is the one: the Blueprint the 1, the register the no the the, the create_app the no the 1, and the extension the init_app - the next chapter's error is the handler the 404 the 500 the no the 1.
The Working Rules
- The Blueprint is the 1, the url_prefix the no. The name the blog (the no the 1), the __name__ the import (the no the the), and the url_prefix the /blog (the no the 1) - the @bp.route the no the app (the 1 the the), and the endpoint the bp.name (the no the 1, the url_for the the.bp.view).
- The register is the 1, the override the no. The app.register_blueprint(bp) the 1 (the no the the, the no the 1), and the url_prefix the override (the no the 1, the no the the) - the no the 2 (the same bp, the 1, the no the the), and the no the import the cycle (the no the the, the no the 1).
- The create_app is the no 1, the config the env. The 1 argument the config (the no the 1), the os.environ the SECRET_KEY (the no the commit), and the return app the 1 (the no the 1) - the no the module the app (the test the no, the 1 the no the the), and the no the global (the 1, the no the the).
- The extension is the init_app, the no the 1. The db = SQLAlchemy() the no (the app, the no the 1), the db.init_app(app) the 1 (the no the the, the no the 1), and the login the init_app (the no the the) - the no the import the app (the 1 the no the the, the no the 1), and the no the 2 (the no the the, the no the 1).
Summary
The blueprint is the module: the Blueprint the 1 (the name, the __name__, the url_prefix, the @bp.route the no the app, the endpoint the bp.name), the register_blueprint the no the the (the 1, the override, the no the import the cycle), the create_app the no the 1 (the config the env, the SECRET_KEY the no the commit, the return app, the no the global, the test the no), and the extension the init_app (the db the no the app, the db.init_app the 1, the login the no the the, the no the import the app). The rule is the one: the blueprint the module, the register the 1, the factory the no the 1, and the extension the init_app - the next chapter's error is the handler: the 404, the 500, the HTTPException, and the no the 1 (the no the the).