Skip to content
Advertisement

How to add a function to app context in flask

I am trying to use flask blueprints to organize the flask projects I build. And I found out a that context can be used to export variables from main.py to blueprints.

main.py(or _init_.py)

JavaScript

my_data can be retrieved from a blueprint using current_app

bp1.py

JavaScript

Now I created a function rand_string in main.py and want to add it to the context.

main.py

JavaScript

How can I do it and how can I retrieve the rand_string function from the bp_1 using current_app?

Advertisement

Answer

You can add rand_string to a class and register this class to config. After that you can call it.

JavaScript

and call it:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement