In this post, I will show how to deploy your Flask application to Heroku. This is a very short article for step-by-step guiding and there is no theory. So if you're reading this post, I assume you already have knowledge about Flask app and you're looking for a solution to publish your app on a production environment.
Note: All command lines below are for MAC OS, which should be the same for Linux. If you are running command on Windows, the command may be a bit different (let search for the version of Windows command). The step order below is the same for MAC, Linux and Windows.
Step 1: Install Heroku CLI if you don't have it yet.
Step 2: Back to your Flask working folder and make sure your application is running in a virtual environment.
Step 3: Install gunicorn with command
pip3 install gunicorn
Step 4: Create a text file name Procfile inside working folder so Heroku can know we use gunicorn
Step 5: Add this command to the Procfile file:
web: gunicorn server:app
with server is the name of flask python file.
If your python file named ‘Hello.py’, then change the command to
web: gunicorn Hello:app
Step 6: Add all dependencies of the flask app to a text file named requirements with command
pip3 freeze > requirements.txt
Step 7: Install Git to your computer if not yet.
Step 8: Inside your flask app working folder, create a new git directory with command:
Step 9: Add everything in your app folder to git with command
git add .
Step 10: Save what we just add to git with command
git commit -am ‘initial commit’
Step 11: Login to heroku with command
heroku login, then enter your password.
Step 12: Create a new Heroku app and name the app with command
heroku create yourappname
with yourappname is whatever name you want to put
Step 13: This is final step. Push everything to your new heroku app with command
git push heroku master
We've done deploy Flask app to Heroku. If you need help or anything, feel free to leave a feedback in the comment session.