DEV Community

NIBRAS NAZAR for Innovation Incubator

Posted on • Originally published at Medium on

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_requestIf 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/


Top comments (0)