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 an 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.Below image shows 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:
Using the selectboxes and checkboxes in the sidebar you can switch between the different pages and KPIs.
Last one :
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.
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 what libraries are used or automatically using something like pipreqs.
If it does not work for you also you can try to type following in cmd :
pipreqs ./ --encoding utf-8
The requirements file should look something like the following:
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 file we will create a streamlit folder with a credentials.toml and a config.toml file.
mkdir -p ~/.streamlit/ echo "\
email = \"firstname.lastname@example.org\"\n\
" > ~/.streamlit/credentials.toml echo "\
headless = true\n\
port = $PORT\n\
" > ~/.streamlit/config.toml
To create Procfile without extension type the following in cmd :
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 an Heroku account here. In general, using Heroku is free but in order 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 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 an 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: Verifying deploy... done.
* [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.
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 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.
This is the end of article. In this article , I try to explain how to deploy your streamlit app with heroku and some errors that might be occured in the deployment process. If you want to look at the Streamlit app that mentioned above you can visit this repository and open the live project from there or click this link for quickly overview.Hope to see you in my next article…