Flask Study Note 3
Blueprint
A blueprint is an object that allows defining application functions without requiring an application object ahead of time. It uses the same decorators as Flask, but defers the need for an application by recording them for later registration.
# app.py
from services import users
app.register_blueprint(users.users_bpapp)
# /services/users.html
from flask import Blueprint, render_template
users_bpapp = Blueprint("users", __name__, template_folder="users_templates")
@users_bpapp.route("/add_user/", endpoint="add_user")
def add_users():
return render_template('users.html')
A blueprint is a template for generating a "section" of a web application. You can think of it as a mold. You can take the blueprint and apply it to your application in several places. Each time you apply it the blueprint will create a new version of its structure in the plaster of your application.
# An example
from flask import Blueprint
tree_mold = Blueprint("mold", __name__)
@tree_mold.route("/leaves")
def leaves():
return "This tree has leaves"
@tree_mold.route("/roots")
def roots():
return "And roots as well"
@tree_mold.route("/rings")
@tree_mold.route("/rings/<int:year>")
def rings(year=None):
return "Looking at the rings for {year}".format(year=year)
This is a simple mold for working with trees - it says that any application that deals with trees should provide access to its leaves, its roots, and its rings (by year). By itself, it is a hollow shell - it cannot route, it cannot respond, until it is impressed upon an application:
from tree_workshop import tree_mold
app.register_blueprint(tree_mold, url_prefix="/oak")
app.register_blueprint(tree_mold, url_prefix="/fir")
app.register_blueprint(tree_mold, url_prefix="/ash")
Once it is created it may be "impressed" on the application by using the register_blueprint
function - this "impresses" the mold of the blueprint on the application at the locations specified by url_prefix
.
before_request()
similar to the middleware in Django Registers a function to run before each request. For example, this can be used to open a database connection, or to load the logged in user from the session. The function will be called without any arguments. If it returns a non-None value, the value is handled as if it was the return value from the view, and further request handling is stopped.
@app.before_request
def is_login():
if request.path == "/login":
return None
if session.get("user"):
return None
else:
return redirect("/login")
after_request()
Register a function to be run after each request.Your function must take one parameter, an instance of response_class and return a new response object or the same (see process_response()).
@app.before_request
def is_login():
print("be1")
if request.path == "/login":
return None
if session.get("user"):
return None
else:
return redirect("/login")
@app.before_request
def be2():
print("be2")
return None
@app.before_request
def be3():
print("be3")
return None
@app.after_request
def af1(res):
print("af1")
return res
@app.after_request
def af2(res):
print("af2")
return res
@app.after_request
def af3(res):
print("af3")
return res
If there is no error, you'll see:
be1 - be2 - be3 - af3 - af2 - af1 # opposite way how you define the `after_request` decorators.
otherwise:
be1 - af3 - af2 - af1
errorhandler()
Register a function to handle errors by code or exception class.
A decorator that is used to register a function given an error code. Example:
@app.errorhandler(404)
def page_not_found(error):
return 'This page does not exist', 404
The error information is in error
arg.
You can also register handlers for arbitrary exceptions:
@app.errorhandler(DatabaseError)
def special_exception_handler(error):
return 'Database connection failed', 500