Deploy Flask Application on Heroku

November 01, 2022

/

7 min read

--- views

--- comments

Last Updated November 10, 2022

Introduction

This tutorial will show you how to deploy a simple Python Flask Application on Heroku step by step.

Here is a demo of what we are going to build: https://flask-app-arlenx.herokuapp.com/.

Here is the link to the repository of the final project: https://github.com/arlenxuzj/flask-app.

This tutorial assumes that you have:

  • A GitHub Account.
  • A Heroku Account.
  • Git installed locally – See the installation guides for Git.
  • Python version 3+ (Recommend 3.10+) installed locally – See the installation guides for OS XWindows, and Linux.

Check Git and Python installed by run command in Terminal below:

# Check Git version
$ git version
git version 2.32.1 (Apple Git-133)

# Check Python version
$ python --version # or python3 --version
Python 3.10.3

# Check pip version
$ pip --version # or pip3 --version
pip 22.2.2

Get Started

Create Application Directory

# Create Directory and move into
mkdir flask-app && cd flask-app

Create Virtual Environment

Use the follow commands to create and activate the virtual environment:

# Create a Python Virtual Environment called flask-app
$ python3 -m venv venv

# Activate flask-app Virtual Environment
$ source venv/bin/activate

Then, you can list the installed python libraries by command:

$ pip list
pip        22.0.4
setuptools 58.1.0

Since it is a new virtual environment, there is no library installed yet except pip itself and setuptools.

Install Flask and Gunicorn

Within the activated environment, use the following command to install Flask and Gunicorn:

$ pip install Flask gunicorn

Finally, deactivate and reactivate the virtual environment:

$ deactivate && source venv/bin/activate

Simple Flask Application

Hello World!

First, we need to create an entry file of our Flask Application.

mkdir src && touch src/app.py && code .

The command above will create a file called app.py in the src directory and open the entire directory flask-app in VS Code.

If you see something like command not found: code in the Terminal, open VS Code normally and access its Command Palette by Shift+Command+P (Mac) / Ctrl+Shift+P (Windows/Linux). Next, type shell, choose Shell Command: Install 'code' command in PATH, and hit ENTER.

hello-world-1

Then, go back to Terminal and run command code . again.

Next, copy and paste the following code to src/app.py file.

src/app.py

Copy
from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "Hello, World!"

To run the flask application, use the flask command. The path of our application entry file need to be provided with the --app option:

$ flask --app src.app run
 * Serving Flask app 'src.app'
 * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000

Then you can copy the URL http://127.0.0.1:5000 to your favorite browser, and you can see the text Hello, World! in the browser screen.

If you find a wave line under the flask in the line 1 in VS Code,

hello-world-2

you can

  1. Click Area 1 in the footer of the VS Code.

  2. Click Python 3.x.x ('flask-app') in Area 2 to choose the python interpreter of the virtual environment.

    hello-world-3

Prepare to Deploy

At the end of this section, your folder structure should be like this:

/flask-app
  /src
    app.py
  .gitignore
  Procfile
  Requirements.txt
  1. Run the command to freeze the python libraries in order to let Heroku know which need to be installed in the build step after deploy the application:

    pip freeze > requirements.txt
    
  2. Create a new file Procfile in the project root directory (flask-app). Copy and paste following line to this file:

    web: gunicorn src.app:app
    

    Heroku will run the command gunicorn src.app:app after the application build successfully.

  3. Create a new file .gitignore in the project root directory (flask-app) and copy and paste the entire file from here.

Push Code to GitHub

Before push your code to GitHub, you should

  1. Create a repo called flask-app.
    • DO NOT select Initialize this repository with a README, it will be easy for later steps.
  2. Connecting to GitHub with SSH.

After you create a repository in GitHub, you will see some instruction in the screen. If you config the SSH setting correctly, run the following commands one by one in the Terminal (inside project directory):

git init
git add -A
git commit -m 'first commit'

If you never use Git before, you cannot commit before you config your Git name/email. Run the following commands in the Terminal:

git config --global user.name "YOUR_NAME"
git config --global user.email "YOUR_EMAIL"

Then, commit again and push your code from the local machine to the remote repository:

git commit -m 'first commit'
git branch -M master
# Find your username in GitHub
git remote add origin [email protected]:username/flask-app.git
git push -u origin master

Deploy Application on Heroku

  1. Create a new app in Heroku.

    deploy-application-on-heroku-1
  2. Go to Deploy Tab.

  3. Choose GitHub Deployment method.

  4. Type flask-app to search for a repository to connect. (You need to connect your GitHub account in the first time).

  5. Click Connect.

    deploy-application-on-heroku-2
  6. Click Enable Automatic Deploys.

  7. Click Deploy Branch.

    deploy-application-on-heroku-3
  8. Wait for the building and click Open app after build successfully. You can find the build status in the Activity Tab.

    deploy-application-on-heroku-4

You can see the text Hello, World! in the browser screen. Now, your application appears on the Internet and everyone can access this application by the URL show in the browser!

Second Router

Dynamic Router

Let us add a new router call hello with a specific URL path syntax. This is called Dynamic Router. If you go to http://127.0.0.1:5000/stranger, you will see Hello, stranger!. If you go to http://127.0.0.1:5000/arlenx, you will see Hello, arlenx!.

src/app.py

Copy
  from flask import Flask
+ from markupsafe import escape

  app = Flask(__name__)

  @app.route("/")
  def hello_world():
      return "Hello, World!"

+ @app.route("/<name>")
+ def hello(name):
+     return f"Hello, {escape(name)}!"

Deploy Again

Now, push our new code to GitHub by following command:

git add -A
git commit -m 'dynamic router'
git push origin master

In Heroku dashboard, you can find that a new deployment ran automatically. Click Open the app to check the new change of the application.

furthermore-1