Google云端控制台-Flask-main.py与数据包 [英] Google Cloud Console - Flask - main.py vs package
问题描述
好的,所以我已经完成了一些教程,可以将Flask应用程序安装到Google云上,这很好.
我还通过了Flask教程来建立一个flaskr博客: http://flask.pocoo.org/docs/1.0/tutorial/
在我看来,明智的做法是在Google上创建一个数据库(在mycase中为MySQL),然后修改代码以使其使用它.很好,我可以在本地计算机上使用它.
但是,现在我要部署它了,我遇到了一个问题.
google cloud教程倾向于使用在单个文件(例如main.py)中启动的flask应用程序,例如:
从烧瓶导入烧瓶中的 ,render_templateapp = Flask(__ name__)....
上面提到的烧瓶教程使用一个包,并将代码放入__init__.py文件中的create_app(),目前我无法以相同的方式启动它.(请参见示例代码).
从烧瓶导入烧瓶的 def create_app(test_config = None):#创建和配置应用app = Flask(__ name__,instance_relative_config = True)app.config.from_mapping(SECRET_KEY ='dev')
我是否需要对诸如app.yaml文件之类的内容进行一些调整,以使其将flask识别为flaskr软件包,或者我是否需要重写整个内容以便它使用main.py文件?/p>
我觉得这是我可以真正养成不良习惯的时间点之一.一般来说,在Google Cloud上编写Flask应用程序的首选方式是什么?
我正在使用google中的标准环境.
感谢您的建议.
标记
由于拥有应用程序工厂,因此您可以在任何地方创建应用程序.只需在 main.py
中创建它,因为这是App Engine期望的:
导入create_appapp = create_app()
OK, so I have been through some tutorials to get a flask app onto google cloud, which is fine.
I have also been through the flask tutorial to build a flaskr blog: http://flask.pocoo.org/docs/1.0/tutorial/
It occurred to me that a sensible thing to do would be to create a database (MySQL in mycase) on google and then modify the code so that it uses that. This is fine and I can get it to work on my local machine.
However, now that I am coming to deploying this, I have hit a problem.
The google cloud tutorials tend to use a flask app that is initiated in a single file such as main.py, eg:
from flask import Flask, render_template
app = Flask(__name__)
....
The flask tutorial mentioned above uses a package and puts the code to create_app() in the __init__.py file and at present I cannot get this to start in the same way. (see sample code).
from flask import Flask
def create_app(test_config=None):
# create and configure the app
app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping(
SECRET_KEY='dev'
)
Are there some adjustments that I need to make to something like the app.yaml file to get it to recognise flask as the flaskr package or do I need to rewrite the whole thing so that it uses a main.py file ?
I feel that this is one of the points in time where I could really pick up a bad habit. What in general is the preferred way to write flask apps on google cloud ?
I am using the standard environment in google.
Thanks for your advice.
Mark
Since you have an application factory, you can create the app anywhere. Just create it in main.py
, since this is what App Engine expects:
from my_package import create_app
app = create_app()
这篇关于Google云端控制台-Flask-main.py与数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!