Deploying Python Flask to Heroku

Posted by admin at May 30, 2020

This is a tutorial that will discuss the steps for deploying a small Flask app on the Heroku PaaS platform These series of tutorials are written, to help me and anyone else maintain a library of documentation in performing simple mundane tasks that will be used frequently.

This article will focus on the Flask app, and the necessary configurations required to ensure smooth running on Heroku. We will be using a REST approach: a basic Hello World feature on one route and a dumb bot on another.

Setting up Flask and the working directory

First we create a work folder

mkdir heroku-flask-starter
  cd heroku-flask-starter

Then a python virtual environment.

py -m venv /venv
  source venv/bin/activate

Next we install the needed modules, flask and gunicorn. Gunicorn is lightweight python server which will be needed for hosting on heroku.

pip install flask
  pip install gunicorn

Next we open our favorite editor and create some flask code and save in

from flask import Flask, request, jsonify
  app = Flask(__name__)

  # the default view
  @app.route('/', methods=['GET'])
  def home():
      return 'Hello Blink!'

  if __name__ == '__main__':
      # run the app on the defualt port 5000
      # and ensure the threaded option to allow for scaling up the app
      # by creating multiple instances of the app., port=5000)

We test on the broswer by runnning the app


This should display our new webpage with the welcome message. If everything works out, we move on to Heroku.

Heroku Account

On Heroku, we need to create an account and login ( When doing this we are prompted to select the language we want to use (Python, PHP, Ruby, Node, Java, Scala, Clojure, Go)

Next we return to our app, and export the modules used to run it into a requirements.txt file. This file will instruct heroku on the modules to install before running our app. Just enter the line below on the command line while in the working directory heroku-flask-starter.

pip freeze > requirements.txt

The requirements.txt file should now contain a list of the modules and the versions (separated by = in each line).

Next we create the Procfile, that will instruct Heroku to run our web server gunicorn before attempting to run the flask app.

web: gunicorn app:app

Back to the Heroku platform, we create a new app (enter the name of the app – sometimes this is generated for you).

Initialize Git

When the app has been created, we tie it up to Git to allow us deploy our app and subsequent changes to Heroku. ON the working directory, enter the commands below

git init
  git add .
  git commit -m "Hello blink initial commit"

Back to Heroku, now to the CLI

Now the folders contents are ready to deploy to a remote server and changes can now be monitored by git. Next we need to connect to Heroku remote server with the Heroku CLI.

heroku login -i

Enter username and password. Next connect to the new heroku app we just created.

heroku git:remote -a 

This command connects our working directory (denoted by git) to the new heroku app we created. Next we need to push the contents of the directory to the heroku server.

git push heroku master

This command takes a while to run, remember the server will have to install the contents of the requirements.txt file and run the process in the Procfile like initiating the gunicorn web server.

Next in order to ensure that our app will run, we need to create an instance of the web app using the command.

heroku ps:scale web=1

This creates one instance of our app in the form of an executable package that will run on a Heroku dyno. This instance is called whenever a user connects to our apps. The more dynos we have running, the more traffic our app can handle. However in order to scale up, you have purchase a plan. More information on scaling in Heroku can be found here and

Testing the app

To test the app we copy the domain name provided in the Heroku console and run it on a browser. This should open our welcome message.

Uploading changes

Suppose we wish to add more routes to our Flask app, we can easily do that and redeploy to Heroku using Git.

  @app.route('/ask', methods=['GET'])
  def answer():

      q = request.get('q')
      if (q is None):
          return {
              'code': 401,
              'status': 'Not found',
              'message': 'Uum, got a question for me to answer?',

      q = q.replace('?', '')

      if (q == 'Whats your name'):
          a = 'My name is Blink'
      elif (q == 'Where are you from'):
          a = 'I am running from'
      elif (q == 'What do you do'):
          a = 'Oh, I repeatedly answer about 2.5 questions from random strangers and thats it!'
          a = 'Sorry, I have no answer to this question.'

      return {
          'code': 200,
          'status': OK,
          'answer': a,

When we are done, we save and move on to Git. On command line enter the line below:

git add .
  git commit -m "Added a new route and function to handle questions from users"
  git push heroku master

These three commands are telling Git to 1) add the changes to the stage, commit the changes on the stage and then pushing the file contents to the remote heroku server.

Additional Notes – Restarting the Heroku server or scaling up

Occasionally we can run into instances where our app shuts down after a period of operation. This occurs on the free tier of the Heroku service. To permanently fix this problem consider purchasing a Heroku plan but a temporal solution that you can simply refresh the dynos as such:

heroku restart


Suggested Read