How to deploy Flask on Heroku
This page explains how to deploy a Flask application on Heroku, the popular deployment platform.
- Basic programming knowledge in Python
- Basic Flask knowledge and WSGI concept
- Comfortable using a terminal
- Already familiar with GIT
What is Flask
Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. Classified as a microframework, Flask is written in Python and it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions.
What is Heroku
Heroku’s a fully-managed platform that helps developers to deploy apps with ease. Heroku is a cloud-based, fully-managed platform as a service (Paas) for building, running, and managing apps.
A short-list with features:
- Runtime - Heroku empowers developers to deliver products using a CLI, called Heroku Toolbelt
- PostgreSQL DBMS - a powerful database already configured to be production-ready
- Automatic scaling - Heroku scales in an instant, both vertically and horizontally.
- Github integration - trigger production builds directly from Github commits
To explain the process, we will use a simple Flask Boilerplate already enhanced for a Heroku deployment.
Sample project - Flask Boilerplate
Flask Boilerplate is a template codebase used by the AppSeed platform to generate Flask Apps enhanced with a basic set of features:
- UI-Ready, Jinja2 templating
- SQLite database, Flask-SQLAlchemy ORM
- Session-Based auth flow (login, register) via Flask-Login
- Deployment scripts: Docker, Gunicorn / Nginx, Heroku
As mentioned, the project comes pre-configured for Heroku. The relevant files are listed below:
- runtime.txt - specify the Python version used by Heroku during the build and deploy
- Procfile - configuration file that informs Heroku where to look for the WSGI interface
- requirements.txt - must contain the
Green Unicorn is a Python WSGI HTTP Server for UNIX. It’s a pre-fork worker model ported from Ruby’s Unicorn project. The Gunicorn server is broadly compatible with various web frameworks, simply implemented, light on server resources, and fairly speedy.
File - runtime.txt
To build the deploy any python-based app, Heroku uses a default Python version
python-3.6.10 or the one specified in the runtime.txt file. Supported environment, as per Heroku official documentation - Specifying a Python version:
- python-3.6.10 <– The Default Version
Heroku apps include a Procfile that specifies the commands that are executed by the app on startup. As specified in the official docs, the Procfile is always a simple text file that is named Procfile without a file extension.
web: gunicorn run:app --log-file=-
For our sample,
gunicorn is called with
How to deploy on Heroku
- Create a FREE account on Heroku platform
- Install the Heroku CLI that match your OS: Mac, Unix or Windows
- Open a terminal window and authenticate via
- Clone the sources and push the project for LIVE deployment
The full command list, executed on our sample project.
At this point, you should be able to visit the app in the browser.