Flask Webapps: 05

Deploying an existing Flask App on Heroku

Part of a series of tutorial articles about Flask webapps.

Deploying an existing Flask App on Heroku

If you already have a Flask app running locally by just running it in IDLE, or at the command line (e.g. python hello.py), and you want to convert it to run on Heroku, you need to do three things. Each is very simple.

Let’s try this now with the webapp you already created. For example, if you created the webapp for the unit conversions, you might try deploying that on Heroku by following these instructions.

To deploy on Heroku, we need to create two extra files.

First, We need a file called Procfile in our git repo. This file tells Heroku what to do with our github repo when we push it to github. It should contain the following:

web: gunicorn hello:app --log-file=-

The part of this line that reads hello:app assumes that the main python code for your web app is in hello.py, and that the variable app is the one that appears in the line of code app = Flask(__name__).

If that is not the case, you may need to adjust either hello or app as needed.

Now that we have that file, you will want to do these commands to commit this file to our github repo.

git add Procfile
git commit -m "Added Procfile needed by Heroku"
git push origin master

We also need a file called requirements.txt which is a list of the Python modules that are needed for our Heroku flask application.

This file will list all of the Python modules that we may have installed using pip install blah, including flask, and anything else that flask might have required.

Note that before you do the next step, you should do the following pip install command if you haven’t already. While this next line isn’t necessarily needed for running Flask applications locally, it is needed for Heroku.

pip install --user gunicorn

We can create the file requirements.txt with this command:

pip freeze > requirements.txt

But we won’t do that! Because “pip freeze” outputs the installed packages in the requirements format; however, over the course of SPIS, we have installed many packages, and the list is very very long; also heroku does not like some of the packages.

Instead, go ahead and create a file called “requirements.txt” (hint: you can do this by typing "idle requirements.txt" into the command line), and paste this into the file:


We now have a list of packages our program needs to run.

Go ahead and save that file, and now lets push that to github as well:

git add requirements.txt
git commit -m "Added list of Python modules needed by Heroku"
git push origin master

For the next step, you’ll need the Heroku Command Line Interface (CLI) installed on the machine where you are working.

Now type heroku create and notice the name of the application created.

If you are using the Heroku CLI, the next step is to type:

git push heroku master

After doing git push heroku master, you’ll probably see lots of output, showing either that your webapp is now running on Heroku, or that some error occurred.

If at the end, the output says “Deployed to Heroku”, then:

If there are errors, check them by typing heroku logs

Try entering your unique URL for you webapp on your phone or your laptop! You should be able to convert temperatures and miles to kilometers from anywhere now!

Brief recap on order of commands

We just added another step in our software development. Just as a reminder, this is the order you should follow as you make changes to your programs:

  1. git add filename
  2. git commit -m “Meaningful and informative message”
  3. git push origin master
  4. git push heroku master

A side note about that “itsdangerous” thing

When I first saw that name show up in the modules we were downloading, I was a little taken aback. If you are worried about having something called “itsdangerous” in your account, this paragraph is to reassure you that its not dangerous.

I read the documentation for the itsdangerous module and realized that that the only thing dangerous here was the name. The name refers to the fact that sometimes data has to be passed from a “trusted environment” to an “untrusted environment” or vice-versa, and when that happens, you want to “sign” the data—that is, do some cryptography with it—to ensure that it isn’t modified enroute. There isn’t anything “dangerous” about the software itself. On the contrary—not using it would be dangerous.

The next lesson

The next lesson is Web Apps Intro (part 6)

Flask webapps tutorials: table of contents

Section Code
(github repo)
Topics Covered
Flask Webapps: 01   Getting Started
Flask Webapps: 02   ftoc (from url), and intro to templates
Flask Webapps: 03   Better Navigation on your Web App with Nav Bars
Flask Webapps: 04   Webapps on Heroku
Flask Webapps: 05   Deploying an existing Flask App on Heroku
Flask Webapps: 06   Working with Sessions in Flask