Deploying your Flask app to Heroku

Evan Calzolaio
3 min readNov 8, 2019

--

This article serves as a guide for my students to deploy their Flask apps to Heroku. I will assume that you are using Terminal on Mac/Linux or Git Bash on Windows. There is an accompanying GitHub repo for this article.

Getting Started

  1. Install the Heroku CLI
  2. In terminal, run heroku login -i and login

app.py

We will start off with a new project directory (folder) called my_app. In this folder, create a new file, app.py which will house our Flask app. The code for the Flask app should look something like this:

from flask import Flask, render_template
import gunicorn
app = Flask(__name__)@app.route('/')
def index():
return render_template('index.html')

index.html

Your index.html file will go in the templates/ folder in your project directory. Let’s create a basic Hello, World! page for our website.

<!DOCTYPE html>
<head>
</head>
<body>
<h1>Hello, World!</h1>
</body>

The Procfile

To tell Heroku how to run our Flask app (app.py), a Procfile is required. In the case of a Flask app, we will need to use Gunicorn (green unicorn) to create a server for our Flask app.

  1. Navigate to your project directory
  2. Create a new file in the project directory and name it ProcfileNote: the Procfile does not have a file extension
  3. open the Procfile with a text editor (ex: Visual Studio Code)
  4. Type web: gunicorn app:app and save the file

Alternatively, if you are feeling savvy, we can do this with just one command: echo "web: gunicorn app:app" > Procfile

This will tell Heroku that we would like to use gunicorn to run a function called app inside of the file app.py.

requirements.txt

There are many ways to create a requirements.txt file. Since many students are often using Anaconda and are avoiding virtual environments, I recommend using the pipreqs module.

To use this module, run the following commands in your Terminal:

pip install pipreqspipreqs .

pipreqs will automatically find out which libraries you have imported and creates a requirements.txt file in your current directory.

File Structure

The file structure of your project directory, my_app, should now look like this:

my_app/
|--- app.py
|--- requirements.txt
|--- Procfile
|--- templates/
|--- index.html

Deploying to Heroku

We’re going to deploy to Heroku from the command line. Deploying is very similar to pushing to GitHub, if you are comfortable with that process, this will be a breeze! Follow along and type the following in your Terminal:

  1. initialize your repository
git init

2. Now that we’ve initialized this working directory as a git repository, we can create a new Heroku project. Run the following:

heroku create

Heroku will initialize the project with a random name, if you would like to use a custom name, like my-app, you can write heroku create my-app, but names must not be taken by another user already.

3. Finally, add your files to git, commit your changes and push to the Heroku repository

git add .git commit -m "ready for Heroku"git push heroku master

If this was successful you should see something similar to the following:

remote: -----> Discovering process types
remote: Procfile declares types -> web
remote:
remote: -----> Compressing...
remote: Done: 45M
remote: -----> Launching...
remote: Released v3
remote: https://fierce-reaches-15276.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/fierce-reaches-15276.git

You can now visit your web page and verify that it says Hello, World! Make sure to use the url provided on the command line, similar to https://fierce-reaches-15276.herokuapp.com/ in this case.

If your app is not working, check the logs. Run the following in Terminal:

heroku logs --tail

Summary

We’ve deployed a Flask app to Heroku and learned the basic deployment requirements along the way. We must make sure we have a Procfile and requirements.txt file for a successful deployment. We can then use the Heroku CLI to create a new project with heroku create and push our work to a Heroku repository, a process similar to a GitHub push.

Please feel free to visit the GitHub repository accompanying this article if you have any questions, or feel free to leave them below. Most importantly, have fun deploying your apps!

--

--