Introduction
If you want to deploy an interactive dashboard or your portfolio as a web page in a cloud platform, Heroku is a great app to deploy your dashboard. In our previous post, we talk about how to build Interactive dashboards in Python using Streamlit. Here we will deploy our streamlit app in a cloud platform.
Heroku
Heroku is a cloud platform that runs our apps in virtual containers (Dynos)which executes on a runtime environment. These containers support many different languages like Ruby, Scala, Python, Java, etc. It also provides custom buildpacks with which we can deploy apps in any other language.
Setup Heroku
- Create an account in Heroku and then create a new app there.
- We can deploy our app in different ways but in this post, we will see how we can deploy our code via Github. We can connect our Github account with Heroku.
Setup files for Heroku
Now we are all set for deploying our Github code to Heroku. Now we need to add additional files in our code which will help us to run our code automatically. For this, we need two extra files in our Github folder.
- Profile – We need to create a text file with the name Procfile without any extension. It is a file that declares how and what commands to be run.
Format:
web: sh setup.sh && streamlit run <filename>.py
Example:
web: sh setup.sh && streamlit run covid_data.py
- setup.sh – We also need to create a file with the name setup.sh where we will write our config as well as to create a folder for streamlit. We just need to paste the below command in the file.
mkdir -p ~/.streamlit/ echo "\ [server]\n\ headless = true\n\ port = $PORT\n\ enableCORS = false\n\ \n\ " > ~/.streamlit/config.toml
If we don’t have a requirement.txt file in our folder. We can use pipreqs package to create it.
$ cd ~/<folder_path>\ $ pipreqs $ git add requirements.txt $ git commit -m "requirements.txt" $ git push
Deploy Heroku via Github
Now we all set to deploy our project. Connect the Github repository we want to deploy and then deploy branch. If we want to deploy automatically for every changes/commit we made, we need to click on the button “Enable Automatic Deploys”
How to run Heroku
Now our streamlit app is deployed, we need to run Heroku.
$ heroku ps:scale web=1 -a <app_name> $ heroku open -a<app_name>
Finally, we can see our streamlit app in a cloud platform
Note : The URL might not be active at the time of reading this below.
All the Code
Here is the Github repo with all the code we used for this blog.
That’s all for this post. Hope it was helpful. Stay tuned for new interesting blogs.