Google Cloud使用筆記(四):使用Google APP Engine 佈署Python3的Flask APP

#20190711更新Google Codelabs官方教學

1.安裝 Cloud SDK

https://dl.google.com/dl/cloudsdk/channels/rapid/GoogleCloudSDKInstaller.exe

2.開啟 Cloud SDK Shell

cd 到Flask APP的目錄當中

3.在目錄當中建立 requirements.txt文件

bokeh==1.0.2
dash==0.30.0
dash-core-components==0.38.0
dash-html-components==0.13.2
dash-renderer==0.15.0
dash-table==3.6.0
dash-table-experiments==0.6.0
Flask==1.0.2
Flask-Compress==1.4.0
Flask-Cors==3.0.7
pandas==0.23.4
numpy==1.15.4
numpydoc==0.8.0
gunicorn==19.7.1
#本txt文件可讓APP Engine佈署時,自動安裝套件
#無論是否使用Flask,佈署Python時,gunicorn一定要在requirements.txt當中

或者在CMD使用

pip freeze > requirements.txt#產生requirements.txt

4.在目錄當中建立 app.yaml文件

service: default
runtime: python
env: flex
entrypoint: gunicorn -b :$PORT main:server
runtime_config:
python_version: 3
#由於是使用Dash的關係,粗體字部分必須用server,而不是app

5.在目錄當中建立 deploy.yaml文件

steps:
- name: "gcr.io/cloud-builders/gcloud"
args: ["app", "deploy", "--project=<your-project-name>"]
timeout: "1600s"

6.檢查目錄中的檔案,應該如下樹狀圖

└── flask_example
├── main.py
├── requirements.txt
├── app.yaml
└── deploy.yaml
#python的檔案一定要是main.py(因為第4步驟$PORT main:app)
#檢查requirements.txt是否有gunicorn==19.7.1
#在本地端運行main.py,檢查是否有任何錯誤
#如果沒任何問題,進行下一步驟

6.佈署到Google APP Engine

在Shell中執行下列指令:gcloud init#進行初始化,依照螢幕上的指令操作即可gcloud app deploy#佈署到APP Engine上#過程中也許會有錯誤產生,這時候就必須針對錯誤到Google搜尋解決方案

Written by

Machine Learning / Deep Learning / Python / Flutter cakeresume.com/yanwei-liu

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store