How to Deploy Your Streamlit App on Heroku — For Free!

Drew Hibbard
Analytics Vidhya
Published in
4 min readNov 13, 2020

This article assumes that you have already developed an app with Streamlit. If you have not, check out the documentation here. It’s a wonderful and easy tool!

Photo by Etienne Boulanger on Unsplash

So you’ve developed a web app with Streamlit and you want to share it with the world, but you don’t know how or don’t want to pay for a hosting service. Well let me introduce you to Heroku! Heroku has a pretty good free tier service, and lets you host up to 5 unverified apps at once (unverified means you haven’t given them a credit card), each with a custom domain. Alright let’s get started!

Sign Up For Heroku

First, you will need to create a Heroku account. Then you should download the Heroku Command Line Interface. Once you are signed up and have the CLI installed, you can open your terminal and run:

heroku login

This will prompt you to log into your new Heroku account in a web browser. Your terminal should print “logged in as <email>” once complete.

Initiate Your Project Repo

Next you will initiate your project repo with Git. Simply navigate to your desired directory and run the below code. Note that this project name will be your domain name, in the form of your-app-name.heroku.com. So the more generic name you give it, the higher the chances it is already taken.

heroku create your-app-name --buildpack heroku/python

Really you shouldn’t need to specify the python buildpack, but sometimes Heroku fails to recognize a python file as such, so it’s best to make sure. Your terminal should tell you that the app has been created and provide the remote repo url. Copy that repo url and run the below, again in the directory where you want your project.

git clone remote-repo-url

That will create a local copy of the repo where you can work, and later push changes back to the remote repo. (It will say that you have cloned an empty repository, which is fine).

Files Needed in the Project Directory

Now that your app has been created and you have a local copy of the repo, you can transfer your py script for the Streamlit app into the project directory.

Before you can deploy, however, you will need a few special files.

Requirements File

First you will need a requirements file that tells Heroku which Python libraries are required for your app, and which versions. The easiest way to generate this file is with the pipreqs library. You can install pipreqs with either pip or anaconda. Then simply run the below while in your project directory. (Alternatively, you can add the directory path after pipreqs to run it from elsewhere).

pipreqs

That’s it. Very easy. The library reads your py script and automatically determines which libraries your app will need, and then saves the requirements.txt file in the specified directory.

Setup Bash Script

Next you will need a bash script to configure Streamlit for use on Heroku. The file should be named “setup.sh” and should have the exact code below. You can edit the file in any text editor. Again, this needs to be in your project’s main directory.

mkdir -p ~/.streamlitecho "[server]
headless = true
port = $PORT
enableCORS = false
" > ~/.streamlit/config.toml

Procfile

Lastly, you need an odd file called a Procfile, which specifies the commands that are executed by the app on startup. It should be spelled exactly as “Procfile”, without any file extension, and should be in your project’s main directory. You can edit it with any text editor. Replace the last part of the code below with your Streamlit python script.

web: sh setup.sh && streamlit run your-script.py

Push to Remote Repo and Deploy!

Now all of the necessary files should be in your local repo:

  • your python script (for Streamlit)
  • Procfile
  • setup.sh
  • requirements.txt

Any files that are read into the python script need to be in the directory as well. All you have to do now is push the files to the remote repo!

git add .
git commit -m "message"
git push origin master

It seems the default remote name is origin and default branch name is master. You can verify that by showing hidden files in your project repo, opening the .git, and then the config file. It should tell you the remote and branch names.

This will load everything to the remote repo and your app will be live shortly! Go to https://your-app-name.herokuapp.com to see it live.

--

--