A quick tutorial on how to deploy your Streamlit app to Heroku. | by Navid Mashinchi | Towards Data Science

Welcome to the step-by-step tutorial on how to deploy your Streamlit app to Heroku. For the past two weeks, I have been working with Streamlit, and I was very impressed with its capabilities. It’s ideal for sharing your data science project with the world since it’s very straightforward. In this tutorial, I assume you have already completed your app and you are at the final stage of deploying your project. Based on my experiences from the past two weeks, you would think you can immediately deploy your project using Streamlit Sharing. However, you need to apply for the functionality, and it could potentially take a couple of days for them to send you the invite. Worst come worse, I even heard of stories where people waited a week to get the invitation to take advantage of Streamlit Sharing. If waiting isn’t an option for you, you can always deploy your app to Heroku or some other host. In this guide, I will go only over how to deploy your app to Heroku and note that the steps below won’t work if you would like to host it on AWS or somewhere else.
The first step is to make sure you have a python file. If you are working in Jupyter Notebook, make sure to download your file as a .py file. This is necessary if you want to run your code locally with Streamlit and push it later to Heroku. I have created a sample Streamlit app so we can follow along here. The name of my python script is app.py. To run your code locally, go to the directory where your script is saved using the command-line interface (CLI). Once you are in the directory, run the following command.
streamlit run app.py
A window will open automatically in your browser, as you can see below.
Voila, you have your Streamlit app running on your local machine.
The next step is to create a GitHub repo for your app. Log in into your GitHub account and follow the steps below:
Type in your repository name and click “Create repository.”
Now that you have created the repo, it’s time to make your first commit and push to the repo. In your terminal, move to the directory where your app.py is in and copy-paste the following code.
echo "# streamlit-to-heroku-tutorial" >> README.mdgit initgit add README.mdgit commit -m "first commit"git branch -M mastergit remote add origin https://github.com/your_username/your_repo_name.gitgit push -u origin master
I am initializing my directory in the above code, adding a README.md file, a remote origin with the URL you see above, and conducting my first push.
The next step is crucial, and you want to make sure you don’t have any typos or make any mistakes. Otherwise, Heroku will run an error when you deploy your app. Before you move on, make sure you have the app.py and the README.md file in your directory.
requirements.txt: Create a requirements.txt file (No capital letters!)and add all the libraries to that file you are using in your python script. In my app.py example, I just imported the following library:
import streamlit as st
As a result, I add “streamlit” to my requirements.txt file. This file is necessary for Heroku to know which libraries it needs to run your application.
setup.sh: The next file is the setup.sh file. This is a shell file, and you need to add the following shell command inside the file. Copy-paste the code below into the file:
mkdir -p ~/.streamlit/echo "\[server]\n\headless = true\n\port = $PORT\n\enableCORS = false\n\\n\" > ~/.streamlit/config.toml
If you don’t add this file, you will get an error message when you deploy your app to Heroku. So make sure you create this app and name it as I wrote it above. No capital letters!
Procfile: Inside your directory, create a text file and call it “Procfile.” Inside the file, copy-paste the following code:
web: sh setup.sh && streamlit run app.py
The “web” means that it’s a web app. The Procfile pretty much specifies the commands once you run the app on Heroku. We specify the shell file that we created above and then call Streamlit to run app.py.
Once you have created the files above, review the steps and make sure you named the files as I named them above. It’s without saying that if you have more libraries for your app that you need to list them in your requirements.txt file. Any library that you are importing needs to be in that file.
Once you have created all the required files, it’s now time to set up your app so it can interact with Heroku.
The first thing you will do is make sure you have installed Heroku on your machine and created a free account. See the link below to create an account.
www.heroku.com
According to the documents, Heroku CLI requires Git. Hence, if you don’t have Heroku, click on the link below, and you can install it.
devcenter.heroku.com
When you installed Heroku on your machine and created an account, it’s time to log in to your Heroku account from the terminal. Run the following command in your terminal:
heroku login
The terminal returns the following line:
‘’heroku: Press any key to open up the browser to login or q to exit:’’
Just click any key, and your browser will automatically open up. You should see the following:
Next, you click Log In and enter your credentials. After that, you will see the following:
As the picture above says, you can close the window and return to the CLI. The next step is to create the Heroku app. Type in the following command:
heroku create sample_app22a
Here I am creating an app called “sample_app22a”. If you don’t specify a name, it will create an app with random numbers. Hence I suggest picking a name for your app. The last step is to push our files to Heroku. Since Heroku uses git, the command line will be easy to understand. Type in the following:
git push heroku master
Once you executed the command above, it will take a couple of seconds for Heroku to install all the libraries and set up your app. You will see a URL in the output, and that will be the URL of your app. Please see below for reference.
We didn’t receive an error message, and it’s now time to check out the app. Click on the link below, and you can see our Streamlit app on Heroku.
sample-app22a.herokuapp.com
There you have it, folks. Now that your app is working, you want to push your changes to your repo as well.
git add -Agit commit -m "Type in a message"git push 
I suggest waiting until you get the Streamlit Sharing invitation, so you can take advantage of that feature instead of deploying it to Heroku. I created an app myself, which I will write about in the coming days. The rendering time took a bit longer for a few plots when using Heroku. However, if you don’t have the time and need to deploy as soon as possible, Heroku will get the job done too.
Hopefully, this article gives you a better understanding of how to deploy your Streamlit app to Heroku. Let me know if you have any questions on this topic or have some feedback. If you enjoyed this article, I’d be very grateful if you would share it on any social media platforms. I will introduce you to the Covid19-Dashboard web app that I created with Streamlit and its Sharing feature in my next article. Stay tuned for that! Until next time️ ✌️