我需要在 Flask 路由开始工作之前执行调用功能。我应该在哪里放置函数以使其在服务启动时调用。我做了:
app = Flask(__name__)
def checkIfDBExists(): # it is my function
if not DBFullPath.exists():
print("Local DB do not exists")
else:
print("DB is exists")
checkIfDBExists()
@app.route("/db", methods=["POST"])
def dbrequest():
pass
如果我是你,我会把它放在创建应用程序的函数中,例如:
def checkIfDBExists(): # it is my function
if not DBFullPath.exists():
print("Local DB do not exists")
else:
print("DB is exists")
def create_app():
checkIfDBExists()
return Flask(__name__)
app = create_app()
当您发现任何设置错误时,这将允许您执行任何必要的步骤。您还可以在该功能中执行路由。我写了这样的函数来分离这个过程here https://github.com/gonczor/web-manul/blob/master/urls.py:
def register_urls(app):
app.add_url_rule('/', 'index', index)
return app
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)