How to share the global app object in flask?

后端 未结 6 666
被撕碎了的回忆
被撕碎了的回忆 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条回答
  •  别那么骄傲
    2021-01-30 17:18

    One way is to create an overall package and adding a __init__.py file under that where you declare all global variables. In your case for example, you can create something like:

    myapplication/
        *        __init__.py
        *        myviews/
            *         __init__.py
            *         view.py
            *         tags.py
    

    etc

    Now you add the following code in the __init__.py file:

    app = Flask(__name__)
    

    You can now use this app variable anywhere as long as you import the package myapplication.

    import myapplication.myviews.view
    

提交回复
热议问题