Personal blog of Matthias M. Fischer


Yet Another Covid Dashboard, Part 3: Deploying it using Heroku

Posted: 3rd January 2022

Preface

Two posts ago, we have written a web app in Python using the flask library. This app loads data on the Covid pandemic from different web sources, processes them, generates plots, and serves those as a HTML dashboard to the web. In the previous post, we have optimised it using caching to prepare it for deployment to the web. Yesterday, I have finally deployed the app to the web using the cloud application platform Heroku. Since then, it can now be found here.

Heroku offers a helpful video tutorial, which is an excellent starting point; however, I had to slightly modify the work flow to get everything running. Thus, I decided to write up my process here for future reference both for myself and others.

The Workflow

1. Get a (free) Heroku Account

Sign up for a free account on this page. No personal information except for name and e-mail address are required.

2. Setting up a new App on Heroku

In your Heroku dashboard, click on "New" to set up a new app. Small hobby projects (with limited computational resources) can be set up for free and no payment information or other personal details are required for this. Assume, we have called the app heroku_appname.

3. Local Setup

On your own local machine, make a new directory for your app and switch into it. Copy the source file(s) of your app into this folder.

4. Specifying the App's Dependencies

We now need to specify the dependencies of the app in a file requirements.txt. This can be done by creating and activating a virtual environment (with arbitrary name such as myenv), e.g. by using

python -m venv myenv
. venv/bin/activate

and then installing all required libraries via pip. Here, we want to serve a flask app. While flask does contain a webserver of its own, this webserver is not suitable for production use as it doesn't scale very well. Thus, we use flask only as our web framework, but rely on another webserver to serve the app: gunicorn. Hence, both flask and gunicorn need to be installed via pip in this step. Finally, execute

pip freeze > requirements.txt

to generate the list of requirements.

5. The Procfile

A file Procfile needs to be added to the directory. It contains information that specify how exactly Heroku will run your app. Assume the Python source file of the app is called myapp.py and within that file the flask object is simply called app (as per usual convention). For the case of our flask app, one may then use the following Procfile:

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

6. Pushing to Heroku

Now, you need to log into your Heroku account on the command line, using the heroku command-line tool (which you will probably have to install beforehand) via heroku login. Then, initialise a new git repository in your app's directory and specify the remote path to push to by running

git init
heroku git:remote -a heroku_appname

Add and commit the content of your directory to the newly initialised git repository, and then push to Heroku by running

git add .
git commit -am "my first commit"
git push heroku master

Done already! After pushing, the server will automatically build your project and serve it to the web (during the build process, you may press CTRL-C to detach the process from your terminal, it will continue working in the background). If an error occurs, run heroku logs in your terminal to inspect the logs.

Conclusions and Outlook

Heroku was recommended to me by a friend of mine and has turned out to be surprisingly easy to set up and to use! I also immensely appreciate the possibility of running apps without any costs on their platform, as long as they're hobby projects and don't require too much continuous uptime. This has definitely conviced me to use their services in the future again, even for more demanding projects which might require some payment.

I also learned a great deal of details about modern web programming over the course of this little project: I have, until now, never written a web app, let alone deployed it to the internet. Naturally, I thus have also never used any "platform as a service" provider like Heroku before. It's really cool to be able to use this workflow to share some of my data science projects with a wider audience in the future.

During this project, I was made aware of a number of interesting Python libraries that allow for the generation of interactive plots. I have, however, deliberately limited the scope of my Covid dashboard to static plots for now, because I wanted to finish this project first before getting lost in learning how to use additional libraries ;-). With this project being finished now, looking into generating interactive plots seems to be a natural next step of expanding my toolbox. As soon as I have done so, I will of course talk about it here. Stay tuned and have a wonderful start into the new week!