LoginSignup
0
2

More than 5 years have passed since last update.

FlaskアプリをHerokuへ(完全自分用)

Last updated at Posted at 2018-10-30

HerokuでFlaskアプリを作りたくて記事を探していたら、@Rowingさんが「Heroku初心者がFlaskを使った簡素なアプリケーションをデプロイするまで!」にて、テンプレートとともに、分かりやすく解説していました。
https://qiita.com/Rowing0914/items/de16bc2676705bd94d24

ここでは「テンプレートをクローンして、いじって再デプロイする方法」をメモ書きします。
上記の記事の手順を前提としています。


コマンドプロンプトでテンプレートのディレクトリへ移動。

C:\Users\mamapapa\Heroku_training_flask>

Herokuにログインして、上記の記事で上げたアプリをクローン。

heroku git:clone -a アプリ名

アプリ名はコマンド上でもこちらでも確認できます。
https://id.heroku.com/login

テンプレディレクトリ内にアプリファイルがクローンされているので移動

cd アプリ名

自分のエディタでテンプレディレクトリ内のクローンアプリを編集して保存
そしたら

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

これで編集した内容が表示されています。
アプリ名はHerokuにログインすると編集できます。
改めて@Rowingさんに感謝します。

自分用メモ

クローンからアプリ作成

クローンしたディレクトリをもとに新しいアプリを作りたくなったら、そのディレクトリをコピーして編集してもいい。その場合「Heroku git url」はコピー元と同じになるのでcreateしたあとに新しいURLを確認しに行って

git remote set-url (heroku createしたHeroku git url)

でurlを変更。そしたらコピーしたプロジェクトフォルダ内のgitフォルダでconfigファイルを開く。そして[remote "heroku"]のurlを新しいHeroku git urlに変更する。

もし他のライブラリを入れたら

pip freeze > requirements.txt.

その後add commit pushをする。

仮想環境activate(Windows)

envディレクトリ内で

Scripts¥activate

キャッシュオフ

ローカルでflaskアプリを起動させているとき、ページのCSSを変更しても反映されないときがある。そんな時はShift F5

最初からアプリをつくる

ディレクトリ作成
ディレクトリ内で

virtual venv

venvに移動してactivate
.gitigoreファイルをエディタで作成
アプリディレクトリを作成して移動

pip install flask

app.pyとindex.htmlを作成

from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
def hello_world():
    return render_template('index.html')

runtime.txtを作成して中にバージョンを書く

python-3.6.5(現在のバージョン知るには python --version)

入れたライブラリを記述

pip freeze > requirements.txt

herokuにgit remoteをセット

heroku git:remote -a アプリ名

そしたらadd commt push

PostgresSQLを使う方法

基本的にこの記事の通り。
自分用に補足をメモします。
https://qiita.com/croquette0212/items/9b4dc5377e7d6f292671

psycopg2がpsycopg2-binary==2.7.6.1になってるのでこっちをインストール

まずはHerokuでデータベース作成
したら下記でモデルからテーブル作成

heroku run python
>>>from app import db
>>>db.create_all()
0
2
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
0
2