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 21, 2019 in Python by Taj
• 1,080 points
815 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 21, 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,230 points
1,205 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,441 views
0 votes
1 answer
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
5,432 views
0 votes
1 answer

How to get size of a list?

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

answered Jun 21, 2019 in Python by Shabnam
• 930 points
731 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP