Deploying to Heroku - MagnusBook/flasktutorial GitHub Wiki

In this tutorial we will look at how you can deploy a flask application to Heroku using the flasktutorial app as an example.

The tutorial has the following parts:

  • Setting up Heroku
  • Creating a Procfile
  • Making requirements.txt
  • Deploying the application
  • (Optional) Linking Heroku with GitHub
  • Debugging Your Application
  • Common issues

Setting up Heroku

This section will walk you through setting up a new account and project on Heroku.

  1. Create a free Heroku account at https://id.heroku.com/login

  2. Create a new application after logging in, you can name it whatever you want. This will be part of the URL of the website.

  3. Install the Heroku Command Line Interface (CLI) using the instructions for your system here.

  4. Now we need to add a separate branch to our git repository. If you do not use GitHub, you should run the command git init inside the root of the project to initialize git. To create the new branch of the repository, run the following command:

    heroku git:remote -a flask-tutorial-dat250 (Replace flask-tutorial-dat250 with the name of your application)

    This may prompt you to log into Heroku from the command line. To do so, simply press any button, and you will be directed to their website to log in.

  5. Finally, you should add your environment variables to the Heroku server. To do this, you can use the command heroku config:set FLASK_APP=flasktutorial. This sets the FLASK_APP command the same way you would use export FLASK_APP=flasktutorial locally. You should also add any other environment variables you are using.

Creating a Procfile

Next we need to set up a Procfile, which will allow us to dictate what the server should do to start our application. For this, we will need an extra python module called gunicorn, which will function as our server application. To install gunicorn, run the following command:

pip install gunicorn

Then, a new file must be created in the root directory of the project called Procfile Note the capital P and no extension.

Inside the Procfile we want to do two things - initialize the database, and starting the application. We will also specify that this will be run on a web node. The resulting file looks like this:

web: python -c "from flasktutorial import init_db; init_db()"; gunicorn flasktutorial:app

There are two commands being run here; python -c "from flasktutorial import init_db; init_db()" and gunicorn flasktutorial:app, separated by a ;.

The first command is responsible for initializing our database. It imports the init_db function defined in flasktutorial/__init__.py. After importing, we simply run the function. Note that a semicolon separates the two lines of code. You probably need to adjust this command to suit your application. You could for example create a python file for initializing the database.

The second command runs the server. Here, the variable part of the command is the input flasktutorial:app. Here, the first part (flasktutorial) refers to the name of the application, while the second refers to the name of your Flask app variable inside your code. In our case, this is app, since we define it using app = Flask(__name__) in flasktutorial/__init__.py.

Making requirements.txt

Heroku also needs to know which dependencies it needs to install to successfully run the app. There are a few ways to create a requirements.txt file, which lists the dependencies of your application. The following are a few of the ways to create this file from easiest to hardest:

  • pip freeze > requirements.txt This takes your installed packages and puts them in a file.
  • Using a python module called pipreqs you can generate a requirements.txt file using the following commands (Replace the path in the second command)
pip install pipreqs
pipreqs ./flasktutorial

Important: You need to add gunicorn to the list for Heroku to be able to run your application

  • (Not recommended) You can create the requirements.txt file manually by adding your dependencies one by one. This is very slow and error prone, so this is not recommended. Important: You need to add gunicorn to the list for Heroku to be able to run your application

Deploying the application

Now it is finally time to deploy the application to Heroku! This can be done using the following commands:

git add .
git commit -m "Some commit message here"
git push heroku master

Here we add our changes, create a commit, and finally push that commit to the Heroku server. Note that this does not push the code to your GitHub repository. To do this, you simply need to run git push

After this is done, you should see a link in the command line leading you to the website of your application. For the this project, the link to the application is https://flask-tutorial-dat250.herokuapp.com/.

(Optional) Linking Heroku with GitHub

You can also set up automatic deployment from GitHub by connecting your accounts. This can be done under the "Deployment method" on the "Deploy" page of Heroku.

Debugging Your Application

Whenever something goes wrong on the Heroku server, you can get some information about the events leading up to the crash or error. To do this, simply use the command heroku logs --tail, which will show you the last lines in the log.

Common issues

Please let me know if you experience some issues, and I will update this with the solutions we come up with!

Ephemeral File System of Heroku

Heroku has an ephemeral file system, which can potentially create some issues for a SQLite3 based database (writing a database to a file).

The best way to fix this is to make it so your application uses a more robust database system, like PostgreSQL. I have made a tutorial for converting to PostgreSQL available here.