Flask: Before and After request Decorators
Tags: flask, decorators, python, webdev
Published: 2020-06-27 08:42:05

---
title: Flask: Before and After request Decorators
published: true
date: 2020-04-30 12:53:40 UTC
tags: flask,decorators,python,webdevelopment
canonical_url: https://medium.com/innovation-incubator/flask-before-and-after-request-decorators-e639b06c2128
---

![](https://cdn-images-1.medium.com/max/640/1*O0S2-UBv_Y1RRgHgRkg1yQ.png)

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/](https://flask.palletsprojects.com/en/1.1.x/)

* * *