How do I run a set of code for all the requests in Flask?

0 votes
I want to run a set of code for every request that occurs in Flask, without adding extra lines of code for each view. Is there a way to write this code in a catch that can be applied before and after each view?
Jun 20 in Python by Taj
• 1,040 points
27 views

1 answer to this question.

0 votes

Hi,

You can use dedicated hooks(decorators) called before and after requests.

They are called:

Flask.before_request

Flask.after_request

@app.before_request
def do_something_whenever_a_request_comes_in():
    # request is available
@app.after_request
def do_something_whenever_a_request_has_been_handled(response):
    # we have a response to manipulate, always return one return response
answered Jun 20 by Shabnam
• 920 points

Related Questions In Python

0 votes
1 answer
0 votes
1 answer

how do I check the length of an array in a python program?

lets say we have a list mylist = ...READ MORE

answered Mar 12 in Python by Mohammad
• 3,060 points
57 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 340 views
0 votes
1 answer

How to add a new Python interpreter in PyCharm?

Refer to the below screenshots: Then set a ...READ MORE

answered May 30 in Python by Shabnam
• 920 points
678 views
0 votes
1 answer