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, 2019 in Python by Taj
• 1,080 points

1 answer to this question.

0 votes


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

They are called:



def do_something_whenever_a_request_comes_in():
    # request is available
def do_something_whenever_a_request_has_been_handled(response):
    # we have a response to manipulate, always return one return response
answered Jun 20, 2019 by Shabnam
• 930 points

Related Questions In Python

+1 vote
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, 2019 in Python by Mohammad
• 3,210 points
+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, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,461 views
0 votes
0 answers
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, 2019 in Python by Shabnam
• 930 points
0 votes
1 answer

How to get size of a list?

Hi, You can use built-in function "len" to ...READ MORE

answered Jun 20, 2019 in Python by Shabnam
• 930 points