All Projects → twtrubiks → Deploying Flask To Heroku

twtrubiks / Deploying Flask To Heroku

Deploying a Flask App To Heroku Tutorial

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Deploying Flask To Heroku

Line Bot Tutorial
line-bot-tutorial use python flask
Stars: ✭ 267 (+229.63%)
Mutual labels:  heroku, tutorial
Js Stack From Scratch
🛠️⚡ Step-by-step tutorial to build a modern JavaScript stack.
Stars: ✭ 18,814 (+23127.16%)
Mutual labels:  heroku, tutorial
Shadowsocksshare
Python爬虫/Flask网站/免费ShadowSocks账号/ssr订阅/json 订阅
Stars: ✭ 3,062 (+3680.25%)
Mutual labels:  heroku, flask
Deep Learning In Production
In this repository, I will share some useful notes and references about deploying deep learning-based models in production.
Stars: ✭ 3,104 (+3732.1%)
Mutual labels:  flask, tutorial
Flask Vuejs Template
Flask + Vue JS Template
Stars: ✭ 1,074 (+1225.93%)
Mutual labels:  heroku, flask
Kamifaka
一款基于VUE3.0的开源免费的卡密发卡系统,高效、稳定可靠。
Stars: ✭ 253 (+212.35%)
Mutual labels:  heroku, flask
Userge
Userge, Durable as a Serge
Stars: ✭ 363 (+348.15%)
Mutual labels:  heroku, tutorial
Flask Boilerplate
Simple flask boilerplate with Postgres, Docker, and Heroku/Zeit now
Stars: ✭ 251 (+209.88%)
Mutual labels:  heroku, flask
Eve Building Restful Mongodb Backed Apis Course
Course materials and handouts for EVE: Building RESTful MongoDB-backed APIs course
Stars: ✭ 53 (-34.57%)
Mutual labels:  flask, tutorial
Heroku Buildpack Python
The official Heroku buildpack for Python apps.
Stars: ✭ 849 (+948.15%)
Mutual labels:  heroku, flask
heroku
Deploy Prime CMS to Heroku in a single click
Stars: ✭ 16 (-80.25%)
Mutual labels:  heroku, deploy
Flask Heroku Sample
Flask Heroku Sample Application
Stars: ✭ 68 (-16.05%)
Mutual labels:  heroku, flask
Deploying Django To Heroku Tutorial
Deploying a Django App To Heroku Tutorial
Stars: ✭ 20 (-75.31%)
Mutual labels:  heroku, deploy
Plotlydash Flask Tutorial
📊📉Embed Plotly Dash into your Flask applications.
Stars: ✭ 265 (+227.16%)
Mutual labels:  flask, tutorial
heroku-deploy
A simple action to build, push and deploy your dockerized app to your Heroku Application
Stars: ✭ 40 (-50.62%)
Mutual labels:  heroku, deploy
Flask Tutorial
Flask 入门教程:使用 Python 和 Flask 开发你的第一个 Web 程序
Stars: ✭ 365 (+350.62%)
Mutual labels:  flask, tutorial
Linebottemplate
A simple Golang LINE bot Template and tutorial how to setup on Heroku for LINE bot API
Stars: ✭ 156 (+92.59%)
Mutual labels:  heroku, tutorial
Rocket
Automated software delivery as fast and easy as possible 🚀
Stars: ✭ 217 (+167.9%)
Mutual labels:  heroku, deploy
Phoenix Chat Example
💬 A Step-by-Step Beginners Tutorial for Building, Testing & Deploying a Chat app in Phoenix 1.5.5 🚀
Stars: ✭ 452 (+458.02%)
Mutual labels:  heroku, tutorial
Flask Tutorial
A tutorial to build your first flask application
Stars: ✭ 58 (-28.4%)
Mutual labels:  flask, tutorial

Deploying-Flask-To-Heroku

Deploying a Flask App To Heroku Tutorial 📝

今天教大家如何佈署 Flask App 到 Heroku

Heroku 免費版本

  • 可以創造 5個 app。
  • 24小時一定要休息6小時的規定。
  • 支援很多種程式語言。
  • 有SSL(https)。

更多說明請參考 Heroku

教學

步驟一

先註冊 Heroku 帳號,請到 Heroku 註冊

步驟二

請安裝 Heroku CLI (請選擇符合自己得作業系統,這裡用 Windows 介紹)

安裝過程,如果已經有安裝過 Git ,可以把勾勾拿掉,

alt tag

安裝完後,請使用你的 cmd (命令提示字元) 輸入以下指令

heroku --version

如果安裝正確,會跳出你安裝的 Heroku CLI 版本

alt tag

接著再請你使用你的 cmd (命令提示字元) 輸入你的 Heroku 帳號 和 密碼

heroku login

alt tag

步驟三

請先 clone 我的簡單 flask 範例

git clone https://github.com/twtrubiks/Deploying-Flask-To-Heroku.git

在資料夾裡,有幾個比較重要的檔案,分別為

  • requirements.txt
  • Procfile
  • runtime.txt

requirements.txt

這個檔案是要告訴 Heroku 你的環境需要那些其他的套件

你可以使用 cmd (命令提示字元) 輸入以下指令查看目前電腦所安裝的套件

p.s 請安裝你需要的套件即可

pip freeze

alt tag

然後可以使用以下指令匯出文字檔 requirements.txt

pip freeze > requirements.txt

alt tag

該目錄底下會多出 requirements.txt

p.s 可以把不需要安裝的套件從 requirements.txt 裡移除

Procfile

Procfile 這個檔案是要告訴 Heroku 要如何啟動這個 web app

Heroku 裡,官方使用 Gunicorn 來啟動 web server,請參考

python-gunicorn Heroku

所以在 requirements.txt 裡,請記得要輸入 gunicorn

Procfile 檔案,基本使用方法如下

web gunicorn app_run:app

app_run 就是你的 app_run.py,請依照自己設定的名稱自行修改

runtime.txt

runtime.txt 檔案裡,只需要簡單的填入你想要指定的 python 版本

python-3.4.3

可參考 Heroku python-runtimes

如果你不想指定 python 的版本,這個檔案可以忽略。

步驟四

先創造 Heroku application

方法一 :

使用你的 cmd (命令提示字元) 輸入以下指令

heroku create

alt tag

p.s 你看到的網址會和我看到的不一樣,請輸入你看到的

方法二 :

到網頁上新增一個 Heroku application alt tag

初始化

使用你的 cmd (命令提示字元) 切換到目錄底下,先著初始化

git init

佈署

指定 remote

heroku git:remote -a tranquil-earth-29753

tranquil-earth-29753 這是我自己的,請輸入你的

這些指令你可以在 web app 裡的 deploy 看到

alt tag

基本上就是 git 的操作,如不清楚什麼是 Git

可以參考我之前寫的 Git-Tutorials

git add .
git commit -am "make it better"
git push heroku master

alt tag

alt tag

佈署完畢,網址的格式為,如上面這張圖

https://[ 你的 app 名稱 ].herokuapp.com/

例如我的網址格式為

https://tranquil-earth-29753.herokuapp.com/

commit ID 4a42e26aee2bff1b10247d7e8a75d4d86b0c83b8

畫面

如果使用我的範例佈署成功,畫面應該如下

我的網址為 https://tranquil-earth-29753.herokuapp.com/

alt tag

LOG 資訊

log 的資訊非常重要 ,因為有時候本機端可以正常運行,但佈署上去就無法運行,

所以這時候就要看 log 資訊。

可以使用以下指令查看你在 heroku上 的 web app 的 log

heroku logs

alt tag

或是可以從網頁端查看

alt tag

網址格式為

https://dashboard.heroku.com/apps/[ 你的 app 名稱 ]/logs

如何在 heroku 上使用 database

請先到下列網址建立 database heroku addons

你會看到很多 db ,這裡用 Heroku Postgres 當作範例

alt tag

接著安裝就行了,如果你還沒有登入,他會請你先登入

alt tag

選擇你的 db 是要給哪個專案用的

alt tag

接著選擇方案,這裡選擇 FREE 方案

alt tag

接下來你就會看到 DB 已經被建立了

alt tag

點擊他,就可以跳到下面的畫面

alt tag

接著按 View Credentials

alt tag

可以看到自己 db 的一些資料,包含 帳號、密碼 資訊

alt tag

現在我們來建立 DB 的 TABLE

先將 URI 這個很長的連接字串,貼到 dbModel.py 裡面

app.config[
    'SQLALCHEMY_DATABASE_URI'] = 'postgres://XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

請貼自己的連接字串!!!

之後,我們使用 Flask-Migrate-Tutorial 來建立 DB 的 TABLE,

如果不懂,請參考 Flask-Migrate-Tutorial 範例。

P.S 假如你和我一樣是使用 PostgreSQL , 需要額外安裝套件 psycopg2

pip install psycopg2

一切處理完畢之後,再進行部屬就完成了。

下圖為簡單的範例 Demo

commit ID ce4c8ee68f58c861a5a8072793912b204c186906

alt tag

database information 就是將 db 的資料全部顯示出來

alt tag

Heroku 注意事項

因為 heroku 的關係,有些人可能會遇到佈署失敗的問題,可以試著將 runtime.txt 修改為 3.6.2

執行環境

  • Windows 10

Reference

Donation

文章都是我自己研究內化後原創,如果有幫助到您,也想鼓勵我的話,歡迎請我喝一杯咖啡😆

alt tag

贊助者付款

License

MIT license

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].