Guide to Deploying a Flask app on Heroku
Heroku is a popular application deployment platform with a functional free tier of services, and Flask is populalar application development micro-framework in Python. The Heroku-Flask environment is one of the quickest ways to deploy a small application for testing, yet sadly, I did not find a single tutorial that covered all aspects of the deployment process without leaving room for a whole bunch of errors.
Hence, this guide.
Note that this is not a guide to Flask and Flask based development and is focused specifically on deployment of your flask application. There are excellent resources online for learning development Flask, which would get you started in a day, so refer to that if you need to get going in Flask, before learning to deploy your projects online — here.
Setup Environment and Git
Your deployment requires a root folder, we’ll let it be : flask-deployment. Create this folder, cd into it and install flask and gunicorn in a new virtual environment:
pipenv install flask gunicorn
Run the following in root (flask-deployment) to setup git:
git add .
git commit -m “Initial Commit”
You need to have heroku-cli installed on your system, and have a Heroku account configured to proceed with this step. If you don’t have these requirements fulfilled, do the following first:
Once you’re done with these installations and sign-up, run the following commands to setup Heroku in the created folder flask-deployment:
Code to login and create a new app
heroku create my-first-app
Code to push initial commit to heroku
git push heroku master
Once you’re done with this, move forward to working on your app.
Inside this folder, create an app folder which would contain all files related to your Flask application. For more information on how to create a Flask application, refer to the tutorial here.
Ensure the following specification in your flask app, for the sake of nomenclature used in this guide:
- your application is inside the app folder, i.e., it’s path is flask-deployment/app/main.py
- your app is named main.py
- your app is named app, as in app = Flask(name) — default naming.
Now that you’ve created your app and tested it locally for functionality (refer to Flask tutorial for any help), let’s move on to deployment.
Create the following files in the root folder (flask-deployment):
Create a wsgi.py file with the following content:
from app.main import appif __name__ == “__main__”:
NOTE: Ensure that the nomenclature specified earlier is followed.
Create a runtime.txt file with your Python version. For me, as of now, its 3.7.5:
By far the easiest file to mess up, and hardest to debug, your Procfile should contain the following, (given that you’ve followed the nomenclature mentioned):
web: gunicorn wsgi:app
Once you’re done with development on your app and have included all necessary modules, generate a requirements file using:
pip freeze > requirements.txt
NOTE: Ensure that this requirement file also contains gunicorn, if it doesn’t, add gunicorn to the last line of requirements.txt manually.
Deployment and Managing Dynos
Run the following code blocks to deploy this project:
Pushing code files to Heroku
git add .
git commit -m “Code files”
git push heroku master
Ensuring that a web dyno is running for deployment:
heroku ps:scale web=1
Additionally, you can check if your app is working correctly by running the following in your project’s root folder:
heroku local web
Tip on Database
Do not use an sqlite3 database (commonly used for basic Flask applications) with Heroku deployment. Heroku does not support sqlite3 due to the manner in which sqlite accesses memory. Use POSTGRES instead.
And that’s it!
Your application will be live on Heroku after this. If you face any issues or have any doubts, feel free to reach out to me at firstname.lastname@example.org.