python - 如何使这个烧瓶应用适合谷歌云引擎?
问题描述
我目前正在尝试使用谷歌云引擎来部署我的烧瓶网络应用程序,但我不确定我需要如何修改文件夹结构和一些文件名以符合要求。
我有一个这样的烧瓶应用程序:
.
├── app
│ ├── errors.py
│ ├── forms.py
│ ├── __init__.py
│ ├── models.py
│ ├── routes.py
│ ├── static
│ │ └── styles.css
│ └── templates
│ ├── 404.html
│ ├── 500.html
│ ├── archive.html
│ ├── base.html
│ ├── bootstrap.html
│ ├── editor.html
│ ├── index.html
│ ├── login.html
│ ├── _post.html
│ ├── post.html
│ └── tag.html
├── app.db
├── app.yaml
├── blog.py
├── config.py
└── requirements.txt
GCE 的要求类似于此页面main.py
上的文件结构,并在顶层包含一个文件。
我blog.py
在顶层定义了应用程序实例。这需要是main.py
文件吗?
我是否需要修改app.yaml
文件以表示这种不同的文件结构?
编辑:我重命名blog.py
为main.py
并将以下行添加到app/__init__.py
:
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8080, debug=True)
但运行python main.py
不会使应用程序在 localhost 上可用。
解决方案
您走在正确的轨道上,文档应该可以帮助您完成后续步骤,您可能已经或可能还没有:
- 创建
templates/
文件夹。 - 创建
static/
文件夹。 - 在文件中编写代码
main.py
。 requirements.txt
在文件中指定依赖项。app.yaml
在文件中定义您的 Web 服务设置。您需要修改处理程序 url 以根据您的项目文件结构指向正确的目录。有关详细信息,请参阅文档。
要回答您的问题,请使用以下代码:
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8080, debug=True)
仅在本地运行时使用。部署到 Google App Engine 时,Gunicorn 等网络服务器进程将为应用程序提供服务。这可以通过添加entrypoint
to来配置app.yaml
。
正如文档指出的那样, “如果您不指定入口点字段, App Engine 也会自动将 gunicorn 添加到您的文件中。”requirements.txt
最后,您将把应用程序部署到 Google App Engine。您将从app.yaml
文件所在的项目的根目录发出以下命令:
gcloud app deploy
我发现了一张很好的图片(来自本文),它描述了该过程的概述:
推荐阅读
- laravel - Web 开发堆栈 - GONE WILD --- 最佳实践架构和部署?
- reactjs - 为什么延迟加载不是 React 的默认设置?
- python - 如何使用 python 从 JSON 文件中删除空格(仅来自 JSON 密钥)
- c - 数组的统计
- vb.net - 检索对象名称
- angular - 使用 nativescript / angular 填充现有 PDF 文件中的表单字段
- scala - 如何获取加入 Spark 产生的行数
- azure-functions - Azure Function 未注册身份验证处理程序
- python - Python:如何为一个键分配多个值
- flask - HighCharts 没有从 Flask 端点获取数据