How to Deploy Your Streamlit App with Heroku?

Ugur Selim Ozen
5 min readOct 29, 2021
Figure 1: Example App Page-1

Hi everyone, In this article, I want to show you how to deploy an example Streamlit application with Heroku, a platform as a service (PaaS) allowing you to run applications written in multiple different programming languages — including Python and Ruby — on the cloud.

Streamlit is an open-source Python framework that allows you to create beautiful interactive websites for Machine Learning and Data Science projects without needing to have any web development skills.

It achieves this by allowing you to create a website by only adding a few Streamlit function calls to existing python projects/experiments. For more information check out Streamlit documentation.

Building an Example Streamlit Application

To show how to deploy a Streamlit application we first need to create one. We have built a dashboard with multiple pages. The below image shows a little piece of application code. For the full code, you can visit this page.

After running the file by executing streamlit run app.py you will see the following pages:

Figure 2: Example App Page-2

Using the select boxes and checkboxes in the sidebar you can switch between the different pages and KPIs.

Figure 3: Example App Page-3

Last one :

Figure 4: Example App Page-4

Heroku Deployment

Now that we have our application we are ready to start deploying the application to Heroku.

1. Needed Files

First, you need to add some files that allow Heroku to install the needed requirements and run the application.

requirements.txt

The requirements.txt file contains all the libraries that need to be installed for the project to work. This file can be created manually by going through all files and looking at what libraries are used or automatically using something like pipreqs.

pipreqs <directory-path>

If it does not work for you also you can try to type the following in cmd :

pipreqs ./ --encoding utf-8

The requirements file should look something like the following:

numpy==1.21.2
plotly==5.2.1
geopandas==0.9.0
streamlit==0.86.0
pandas<1.2.0
matplotlib==3.4.3
requests==2.26.0
protobuf==3.17.3
google.cloud < 0.34.0
google.cloud.bigquery < 2.24.0
google.cloud.core < 1.7.2
google.auth < 1.34.0
google.resumable.media < 1.3.3
google.api.core < 1.31.1

setup.sh and Procfile

Using the setup. sh and Procfile files you can tell Heroku the needed commands for starting the application.

In the setup.sh, the file we will create a streamlit folder with a credentials.toml and a config.toml file.

mkdir -p ~/.streamlit/  echo "\ 
[general]\n\
email = \"your-email@domain.com\"\n\
" > ~/.streamlit/credentials.toml
echo "\
[server]\n\
headless = true\n\
enableCORS=false\n\
port = $PORT\n\
" > ~/.streamlit/config.toml

To create Procfile without extension type the following in cmd :

notepad Procfile.

The Procfile is used to first execute the setup.sh and then call streamlit run to run the application.

web: sh setup.sh && streamlit run app.py

For more information on how to write a Procfile check out the official documentation.

2. Creating A Git repository (if you haven’t yet)

Heroku allows deployment using many different technologies including Git and Docker. In this article, we will use Git because it’s simple.

A Git repository can be created with git init.

(ml) C:\Users\Ugur\Desktop\Streamlit>git init 
Initialized empty Git repository in C:/Users/Ugur/Desktop/Streamlit/.git/

3. Create A Heroku Account

Next, you’ll need to create a Heroku account here. In general, using Heroku is free but to get more features like application metrics or free SSL, you’ll have to pay.

You can find a detailed overview of all the different tiers on their pricing page.

4. Installing the Heroku Command Line Interface (CLI)

Now that you have an account you will have to download Heroku’s Command Line Interface.

5. Login to Heroku

After installing the CLI you can login into the CLI using your Heroku account. For this open a terminal, move it into the application folder, and then execute Heroku login. You’ll be asked to press a button and after you have done so you’ll be redirected to a login screen in your default browser.

(ml) C:\Users\Ugur\Desktop\Streamlit>heroku login 
heroku: Press any key to open up the browser to login or q to exit: Opening browser to https://cli-auth.heroku.com/auth/browser/51ce4b4c-bd75-4fec-b67e-2ea39e00f36d Logging in... done
Logged in as <xyz>

6. Deploy The Application

Finally, you can deploy your application to Heroku by running Heroku create to create a Heroku instance.

(ml) C:\Users\Ugur\Desktop\Streamlit>heroku create your_app_name
Creating app... done, merchandisestoredashboard
https://merchandisestoredashboard.herokuapp.com/ | https://git.heroku.com/merchandisestoredashboard.git

Lastly, push the code to that instance using the following commands.

git add . 
git commit -m "some message"
git push heroku master

When running git push Heroku master you should notice that it should automatically detect that you have a Python app and it should install all packages inside the requirements.txt. After the command has finished you should see something similar to the following:

remote: -----> Launching... 
remote: Released v3
remote: https://merchandisestoredashboard.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/merchandisestoredashboard.git
* [new branch] master -> master

7. Check If It Is Running

You can check if the application was deployed successfully using heroku ps:scale web=1.

(ml) C:\Users\Ugur\Desktop\Streamlit>heroku ps:scale web=1
Scaling dynos... done, now running web at 1:Free

Finally, the application can be opened with heroku open. This will open the app using your default browser.

Figure 5: Streamlit App Deployment

8. Heroku Monthly Free Dynos

Heroku gives to every user 500 free dynos for every month and if you want your app all time awake [if it is not, it will take a minute to run the app from the Heroku server] you can use this website to ping your Heroku app every 30 minutes so it will never sleep but this time, monthly free dynos provided by Heroku will run out rapidly.

9. Conclusion

This is the end of the article. In this article, I try to explain how to deploy your streamlit app with Heroku and some errors that might be occurred in the deployment process. If you want to look at the Streamlit app mentioned above you can visit this repository and open the live project from there or click this link for a quick overview. Hope to see you in my next article…

--

--

Ugur Selim Ozen

Data Engineer who is developing himself in Big Data Processing, Data Analytics, Machine Learning and Cloud Technologies. https://ugurselimozen.github.io/