Flask - Deploy on Heroku with Gunicorn
Basic understanding
Heroku works with Git. You create your projects on a local computer, and manage the version control with Git. Then, push the project to heroku master and deploy by few setting.
Material requirement
Begin!
Prepare a flask project.
# app.py
from flask import Flask
app = Flask(__name__)@app.route(‘/’)
def hello_world():
return ‘Hello, World!’if __name__ == ‘__main__’:
app.run()
Create requirements.txt
. Remember to add gunicorn package.
# requirements.txt
Flask==1.0.2
gunicorn==20.0.4
Create a Procfile
without a file extension.
# Procfile
web gunicorn app:app
The first app
in app:app
is the filename of app.py where flask application initiated.
Use Git to manage version control.
For example, in terminal,
cd project_location/
git add .
git commit -m <message>
If your python project run on virtual environment, adding .gitignore
to exclude the directory venv/
. There is no need to use virtual environment in heroku. It was not working when I included it.
# .gitignore
venv/
Then!
In terminal, login heroku account.
heroku login
The terminal will open browser for login page.
After login, back to terminal.
cd project_location/heroku create <project-name>
heroku git:remote -a <project-name>git push heroku master # Push the project to heroku and Deploy
Done!
At this moment, the flask web server should be deployed successfully on heroku. You can open the web by using the command in terminal.
heroku open
If there is something wrong, using the command in terminal to see logs.
heroku logs — tail
As a beginner, I am trying to use the easiest way to build up a website. Thanks for reading .