You are currently viewing How To Deploy Streamlit Webapp To Heroku – Complete Guide

How To Deploy Streamlit Webapp To Heroku – Complete Guide

Developers associated with data science or machine learning probably have web development skills. So they use a framework called Streamlit as a frontend source to serve their ML or Data Science web app.

Streamlit is an open-source, accessible, and straightforward Python framework that allows us to create interactive UI for dashboards, Machine Learning, Data Visualization, and Data Science projects.

Anyone with no experience in web development can generate an app using Streamlit though it requires some basic knowledge of Python. Data Scientists and Data Engineers often use Streamlit.

Generating an app using Streamlit is achieved by adding a few streamlit function calls to the existing Python projects. Check out this article on EDA and the data visualization web app using Streamlit to learn how to build the frontend for your Python project using Streamlit.

Once you’ve created an app, you probably want your project to be on the cloud so others can utilize it.

In this tutorial, we will discuss how we can deploy the Streamlit application to Heroku, a platform as a service (PaaS) allowing us to host our applications entirely on the cloud.

Creating a Streamlit Application

To deploy an app on Heroku, we need it first. I’ve previously created a Streamlit app “Python: Web app under 100 lines of code using streamlit” and explained the process of making it in an article, but I haven’t covered the deployment process. So we’ll use that app and deploy it on Heroku.

Creating Required files

We need to add some files in the app’s root directory that allow Heroku to install the requirements and run the application.

requirements.txt file

The requirements.txt file contains all the libraries that are used in the project or need to be installed for the project to work. We can create it manually or use a Python library called pipreqs which will automatically create a requirements.txt file.

To generate a requirements.txt file.

The requirements file should look something like the following:

setup.sh and Procfile

setup.sh and Procfile is used to tell Heroku the needed commands for running the application.

In the setup.sh file, we’ll add the following code that creates a streamlit folder with a credentials.toml and a config.toml file.

setup.sh file

Now we’ll create a Procfile that specifies the commands executed by the app on the startup.

In the command below, Procfile executes the setup.sh file and then calls streamlit run to run the application.

Procfile file

Creating a GitHub repository

Now, it’s time to push the code to GitHub. If you haven’t created a GitHub repository, then log in to your GitHub account and follow the steps below:

New Repository

Type the repository name and hit the “Create repository” button.

Creating GitHub Repo

Now that you’ve created a GitHub repository, it’s time to commit and push the code to the repository. Move to the app.py directory and copy-paste the following command into your terminal.

Here, a git repo will be initialized in the current directory, and instead of adding git add README.md, we’re running git add . to stage all the files in our current directory.

Deploying to Heroku

We need a Heroku account to deploy any application on their platform. In general, Heroku is free to use but to get more features, we’ll have to pay.

Creating a Heroku account

To create an account on Heroku, click here. I’ve already created an account, so I’ll log in to my account.

There are two methods to deploy applications on Heroku:

  1. Using Heroku UI (Manually)
  2. Using Heroku CLI

Using Heroku UI (Manually)

Using Heroku UI, we can manually deploy our application by performing the below steps.

Click on the “Create new app” to create a new app.

Creating Heroku App

Then enter the name of the app and then hit “Create app” to move on to the next step.

Configuring App Details

Click on the “GitHub” option to connect to GitHub on Heroku, search for application repository, and hit “Connect“.

Uploading GitHub Repo on Heroku Server

In the last step, click “Enable Automatic Deploys“, select the branch, and hit “Deploy Branch“.

Deploying Repo

Now, wait until Heroku completes the build process of our application and deploys it on the cloud. After completing the deployment process, we can see our application live on the cloud. Here you can see the application covid19visualization.

Live App

Using Heroku CLI

To deploy our application on Heroku from the terminal, we need to download the Heroku CLI. Heroku CLI requires Git. If you haven’t installed it yet, then complete the Git installation or refer to this article on Getting Started on Heroku with Python.

Log in to Heroku

After setting up the Heroku CLI, we can log in to the Heroku account. Open your terminal, move to the app.py directory, and run the heroku login command. You will be asked to press any key, redirecting you to the Login window.

Heroku CLI Login

Deploying the app

After the log-in process, we need to create a Heroku instance for the app by using the command heroku create in the terminal.

It’s your choice to type a name for your app otherwise just run the heroku create command.

Creating Heroku App uisng CLI

Push the code using the following command to that instance.

After running the command git push heroku master, Heroku will automatically detect that it is a Python app and installs the packages from the requirements.txt file. After completing the build process, we can see the following log in the terminal.

This app is using the Heroku-20 stack which is an old stack, however, we can upgrade to Heroku-22 which is a newer stack. Here’s how to upgrade an app stack.

Checking if the app is running

Now we can check if our app is deployed successfully and running on the web using heroku ps:scale web=1.

Checking Application Status

Finally, run the command heroku open and this will open the app using your default browser, or manually enter the app’s URL in the browser to open the application.

Visit my GitHub repository for Source Code.

Conclusion

We’ve learned how to deploy a Streamlit app on Heroku using two methods.

The first method we used is Heroku UI or manually deploying our app from the Heroku website by performing some simple steps.

The second method we used is Heroku CLI, Heroku Command Line Interface helps us perform the deployment process completely from the terminal.


That’s all for now.

Keep Coding✌✌.