hello.py
is a very simple Flask application. In this tutorial, we'll:
Without further ado, let's get our hands dirty.
hello.py
import os
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return '<h1>Hello World!</h1>'
if __name__ == '__main__':
# app.run(debug=True)
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port)
Please follow steps below:
mkdir hello-heroku
cd hello-heroku
# create and activate a virtual env
python3 -m venv venv
. venv/bin/activate
# the prompt will change here
# check the path of Python
which python
pip install flask
pip install gunicorn
gunicorn hello:app
# output on the terminal:
# * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
Now, you can access above URL in the browser. "Hello Heroku!" will be displayed if everything goes all right.
heroku login
heroku create hello-heroku-feici02
pip freeze >! requirements.txt
echo "web: gunicorn hello:app" > Procfile
git init
echo "venv\n*.pyc" > .gitignore
git add -A
git commit -m "initial commit"
git remote add origin https://github.com/feici02/hello-heroku.git
git push origin master
heroku logs --app hello-heroku-feici02
If you don't want to use GitHub, you can deploy the app created in Part 1 directly to Heroku. Here are the steps:
cd hello-heroku
git init
# steps to commit the files are omitted here
heroku apps:create hello-heroku-feici02-2
git remote -v
# a remote named heroku will be added to your repo automatically
git push heroku master
# Your app is up and running at: hello-heroku-feici02-2.herokuapp.com.