Flask: How to run a method before every route in a blueprint?

user606006

I want to make my Flask Blueprint always run a method before executing any routes. Instead of decorating every route method in my blueprint with a custom decorator, I want to be able to do something like this:

def my_method():
    do_stuff

section = Blueprint('section', __name__)

# Register my_method() as a setup method that runs before all routes
section.custom_setup_method(my_method())

@section.route('/two')
def route_one():
    do_stuff

@section.route('/one')
def route_two():
    do_stuff

Then basically both /section/one and /section/two will run my_method() before executing code in route_one() or route_two().

Is there a way to do this?

Miguel

You can use the before_request decorator for blueprints. Like this:

@section.before_request
def my_method():
    do_stuff

This automatically registers the function to run before any routes that belong to the blueprint.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

how to run the code before the app.run() in flask?

From Java

Flask: Decorate every route at once?

From Python

How to make flask application run every 5 minutes?

From Java

How to schedule a function to run every hour on Flask?

From Dev

Calling method from Flask Blueprint

From Java

In Flask: How to access app Logger within Blueprint

From Java

How to run my Spring tests before every commit/push?

From

How to run a method every X seconds

From Dev

How to use the "find where" SailsJS blueprint route?

From Dev

Blueprint initialization, can I run a function before first request to blueprint

From Dev

How to pass arbitrary arguments to a flask blueprint?

From Dev

How to run script before every Rails console invocation?

From Dev

How to run method every night on JEE Cluster

From Dev

How to init restless as flask blueprint

From Dev

How to run Python code before every jupyter notebook kernel

From Dev

Overwrite route in flask blueprint

From Dev

How to run an action on every route transition in ember?

From Dev

How handle PATCH method in Flask route as API?

From Dev

how to run some method that will run on different thread every 2 seconds?

From Dev

Flask BluePrint route not working with multiple parameteres

From Dev

How to run Flask app by flask run with blueprint template

From Dev

how do I redirect to the flask blueprint parent?

From Dev

Using route decorator in a Flask blueprint's submodule

From Dev

How to use Spring AOP to run code before every call of service method in Servlet

From Dev

How can I add a route in a custom blueprint?

From Dev

Flask doesn't call blueprint's before_request function

From Dev

Python flask blueprint registration route for different arguments

From Dev

In Flask, is it there a way to run some code before every view?

From Dev

How to patch a method decorated with `flask - route` in testing?

Related Related

  1. 1

    how to run the code before the app.run() in flask?

  2. 2

    Flask: Decorate every route at once?

  3. 3

    How to make flask application run every 5 minutes?

  4. 4

    How to schedule a function to run every hour on Flask?

  5. 5

    Calling method from Flask Blueprint

  6. 6

    In Flask: How to access app Logger within Blueprint

  7. 7

    How to run my Spring tests before every commit/push?

  8. 8

    How to run a method every X seconds

  9. 9

    How to use the "find where" SailsJS blueprint route?

  10. 10

    Blueprint initialization, can I run a function before first request to blueprint

  11. 11

    How to pass arbitrary arguments to a flask blueprint?

  12. 12

    How to run script before every Rails console invocation?

  13. 13

    How to run method every night on JEE Cluster

  14. 14

    How to init restless as flask blueprint

  15. 15

    How to run Python code before every jupyter notebook kernel

  16. 16

    Overwrite route in flask blueprint

  17. 17

    How to run an action on every route transition in ember?

  18. 18

    How handle PATCH method in Flask route as API?

  19. 19

    how to run some method that will run on different thread every 2 seconds?

  20. 20

    Flask BluePrint route not working with multiple parameteres

  21. 21

    How to run Flask app by flask run with blueprint template

  22. 22

    how do I redirect to the flask blueprint parent?

  23. 23

    Using route decorator in a Flask blueprint's submodule

  24. 24

    How to use Spring AOP to run code before every call of service method in Servlet

  25. 25

    How can I add a route in a custom blueprint?

  26. 26

    Flask doesn't call blueprint's before_request function

  27. 27

    Python flask blueprint registration route for different arguments

  28. 28

    In Flask, is it there a way to run some code before every view?

  29. 29

    How to patch a method decorated with `flask - route` in testing?

HotTag

Archive