所以我开始
mainfolder/
main.py <--- the entire flask app was in there but it got unruly so I though abou dividing things up in to blueprints...
我这样做的新方法是“
mainfolder/
main.py <--- the entire flask app was in there but it got unruly so I though abou dividing things up in to blueprints
blueprints/
__init__.py
blueprint1/
__init__.py
blueprint1.py
我的 main.py 中有一个名为 checkIt(){} 的方法...但是我不知道如何在蓝图中引用它。
我发现蓝图有优点和缺点,并且想解决这个问题。
Flask 应用程序变得越来越大的结构很棘手(循环导入...)。
一种方法是将 Flask 应用程序创建移至
mainfolder/__init__.py
并将函数库保留在 main 中:
我刚刚测试了这个结构(源自我通常所做的事情,灵感来自https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world):
run_debug_app.py <--- just to call create_app and start a debug server
mainfolder/
__init__.py <--- new file
main.py
blueprints/
__init__.py <--- empty in example below
blueprint1/
__init__.py <--- empty in example below
blueprint1.py
与
run_debug_app.py
:
from mainfolder import create_app
if __name__ == '__main__':
app = create_app()
print("DEBUG ONLY WITH THIS SERVER")
app.run(debug=True,host=None,port=None)
mainfolder/__init__.py
:
from flask import Flask
def create_app():
app = Flask(__name__)
from mainfolder.blueprints.blueprint1.blueprint1 import mod as blueprintModule
app.register_blueprint(blueprintModule)
return app
mainfolder/main.py
:
def get_hello_world_text():
return "Hello World"
mainfolder/blueprint1/blueprint1.py
:
from flask import Blueprint
from mainfolder.main import get_hello_world_text
mod = Blueprint('blueprint1', __name__, url_prefix=None)
@mod.route("/", methods=["GET"])
def hello_world():
return get_hello_world_text()
为了继续前进,我建议您查看 https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world 了解大型应用程序的结构。