LoginSignup
12
17

More than 1 year has passed since last update.

Heroku で Flask を使う

Last updated at Posted at 2017-08-07

Heroku の Getting Started のサンプルは、django です。そこで、Flask のサンプルを用意してみました。

実行例です。
ブラウザーで、https://ex02.herokuapp.com/ にアクセスした様子です。
heroku_aug0700.png

作業フォルダーに次の3つのファイルを用意します。
hello.py
Procfile
requirements.txt

hello.py
# -*- coding: utf-8 -*-
# -------------------------------------------------------------------
#
#	hello.py
#
#						Aug/07/2017
# -------------------------------------------------------------------
import os
from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
	str_out = ""
	str_out += "<h2>Hello from Python!</h2>"
	str_out += "<blockquote>"
	str_out += "<p>こんにちは</p>"
	str_out += "</blockquote>"
	str_out += "Aug/07/2017 PM 12:49<br />"
#
	return str_out
#
if __name__ == "__main__":
	port = int(os.environ.get("PORT", 5000))
	app.run(host='0.0.0.0', port=port)

# -------------------------------------------------------------------
Procfile
web: gunicorn hello:app --log-file -
requirements.txt
Flask
gunicorn

まず、ローカルでサーバーを動かします。

$ python hello.py
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

次に、heroku にアップロードします。
ex02 はアプリの名です。

go_upload.sh
heroku login
git init
git add .
git commit -m "init"
heroku create ex02
git push heroku master

heroku_aug0701.png

ソースコードを修正したら、次のループを繰り返します。

go_update.sh
git add .
git commit -m "modified"
git push heroku master

Arch Linux での heroku コマンドのインストール方法

yay -S heroku-cli

Ubuntu 22.10 での heroku コマンドのインストール方法

wget https://cli-assets.heroku.com/install-ubuntu.sh
chmod +x install-ubuntu.sh
sudo ./install-ubuntu.sh

アプリの一覧

heroku apps

アプリの削除

heroku apps:destroy -a ex02

heroku のバージョン

$ heroku --version
heroku/7.54.0 linux-x64 node-v16.1.0

コードのダウンロード

heroku git:clone --app ex02

参考ページ
The Heroku CLI

12
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
17