Flask: Before and After request Decorators
In addition to static and dynamic routes to functions/views using the @app.route()
decorator, Flask empowers us with several powerful decorators to supplement the routes we create with .route()
. In this article, we will look at some ways to run functions before and after a request in Flask using the decorators
before_request
after_request
before explaining those, let’s write a very basic flask application
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
print("Index running!")
if __name__ == "__main__":
app.run()
So whenever a request to the root (“/”) is made, we would see the output shown below.
Index running!
We make use of before_request
and after_request
If we want any specific task to get executed before and after the request respectively.
before_request
The before_request
decorator allows us to execute a function before any request. i.e, the function defined with the .before_request()
decorator will execute before every request is made.
We can do this by decorating a function with @app.before_request
:
@app.before_request
def before_request_func():
print("before_request executing!")
after adding this function, we get the following output whenever we try to make a request to the route (“/”).
before_request executing!
Index running!
we can make use of this function in cases like :
- Opening database connections.
- tracking user actions
- adding a “back button” feature by remembering the last page the user visited before loading the next
- determining user permissions, etc……
before_request
are not required to return anything, however, If a before_request
function returns a value, it will be considered as if it was the return value for the view and any further request handling is stopped.
after_request
The after_request
decorator works in the same way as before_request
decorator, except, It allows us to execute a function after each request.
let us see an example by adding this function to our application:
@app.after_request
def after_request_func(response):
print("after_request executing!")
return response
by making a request to any route on our application we would see the following output:
before_request executing!
Index running!
after_request executing!
after_request
functions must take and return an instance of the Flask response class.
this function can be used in cases like :
- close a database connection
- To alert the user with changes in the application, etc….
Any functions decorated with after_request
will NOT run if the application throws an exception.
Our application :
from flask import Flask
app = Flask(__name__)@app.before_request
def before_request_func():
print("before_request executing!")@app.after_request
def after_request_func(response):
print("after_request executing!")
return response@app.route("/")
def index():
print("Index running!")
if __name__ == "__main__":
app.run()
for more you can visit the flask documentation page here https://flask.palletsprojects.com/en/1.1.x/