How to share the global app object in flask?

后端 未结 6 670
被撕碎了的回忆
被撕碎了的回忆 2021-01-30 16:28

I am using flask and trying to the following.

I have defined a main.py file through which I want to run my app ie python main.py -

from flask import Fla         


        
6条回答
  •  梦毁少年i
    2021-01-30 17:08

    If you have a file AppName.py in which you define app, and then you have another file Foobar.py that needs it, you can always say in AppName.py:

    import Foobar
    Foobar.app = app
    

    Then in Foobar.py you should be able to use app in your functions. One thing you want to be careful of is that you can't have code in Foobar.py that runs immediately when the file is called the depends on the app variable which is passed in after the import.

提交回复
热议问题