Photo by Joshua Aragon on Unsplash

How to Deploy your Streamlit app using Heroku

Posted by

What’s the point of building a fancy web app if you can’t show off your dashboard/model to the world? 🌎 In this tutorial, we will deploy a Streamlit app using Heroku

Things you will need

  • Heroku Account
  • Github Account
  • A Streamlit app you want to deploy, I will be deploying this app. If you are want to learn how to build a streamlit app to scrape Github, check out my previous article

Files needed

Requirements.txt

pip freeze > requirements.txt

The above command lists down all the libraries your app uses in a text file. Make sure your virtual environment is activated before you type the above command.

Procfile

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

Create a file and name it ‘Procfile’. Make sure it has no extension and its name is set to ‘Procfile’. Inside the file, paste the above content. If your app.py is inside a folder app, it should be ‘ streamlit run app/app.py’

setup.sh

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

Create a file and name it ‘setup.sh’ Copy the above content and paste it in the file. Make sure you replace your email id.

Once you have created all the files, upload your app along with the files to Github

Your files should look similar. I have scrape.py since I am using the web app I build previously, you might not have it

Heroku

Screenshot 1

Click on New > Create new app

In the next screen chose a name for your app and chose the region

Screenshot 2

Click on create app once you’re done.

On the next screen, select your deploy method as Github. Search for your GitHub repo and connect to it.

Screenshot 3

After you successfully connect your repo, you should be able to scroll down and see an option called “Manual Deploy”

Screenshot 4

You can choose Automatic Deploy as well, the master branch will be deployed automatically whenever changes are made. For this tutorial, we will use Manual Deploy.

Click on Deploy Branch and wait for the process to complete.

Once the build is complete, you should see a success message.

Screenshot 5

Click on view to see your app live

Congratulations 👏 👏

If there is a build error due to a version of a library not being found, manually update the version in requirements.txt with the latest version shown in the build error message. This may cause some functionalities of your app to not work but it’s worth a try.