Photo by Florian Olivo on Unsplash

The workflow of Heroku Deployment

The beginner level for people who focus on data science and looking for deploying their Streamlit on Heroku.

Hugo Shih
Published in
6 min readSep 8, 2020

--

This is my very first Medium article and I hope this place can become the collection of my experience and experiment. To make sure I can still explain those steps, let’s get started!

Create the project folder. The path can be varied, depends on your preference. Once it’s created, change the directory to the root level of the folder.

# mkdir ~/code/example/my_project
# cd ~/code/example/my_project

And then, create an empty Git repository in the folder. For more details, you can check the online document here.

# my_project git init
Initialized empty Git repository in /Users/hugoshih/code/example/my_project/.git/

Once the repository is created. We can build a Github repo by using the following code.

# my_project hub create

If you don’t have hub, you will need to install through brew. Here is for the method for different OS system. For Windows user, you will need to install a package manager, check the link for more information here.

# Mac: brew install hub
# Ubuntu: sudo apt install hub
# Windows: choco install hub

You must follow the order from the above steps. If you don’t have an empty Git repository in the folder, you won’t be able to make Github repo. The error message below:

# my_project hub create
'create' must be run from inside a git repository

If this is the first time you execute hub create, it will ask your Github username and password. And then it will give you an Github URL link. Meaning you can check your project on your Github repository page.

# my_project git:(master) hub create
Updating origin
https://github.com/username/my_project

The next step is also very important. We need to set up a virtual environment for each project. The reason is because each project comes with specific version of packages and libraries. If we mix all together, it will be hard to manage when we come to the deployment process, also it would be possible to break the app.

1 project == 1 virtualenv

Here comes the easy steps to build a virtual environment.

I use pyenv-virtualenv, which is pyenv’s plugin, to manage my virtual environment. If you don’t have it, you can check the link for pyenv here or pyenv-virtualenv here. Here comes the example.

pyenv virtualenv my_project# create a new virtualenv for our project
pyenv virtualenvs # list all virtualenvs
pyenv activate my_project # enable our new virtualenv
pip install --upgrade pip # install and upgrade pip
pip list # list all installed packages

Reminder:
When you open a new terminal, make sure you are at right virtual environment before installing any new packages or libraries.

After finishing the above steps, you can start building your brand new project.

After finishing the Streamlit project and now you want to deploy your python app to the Heroku. Here are the basic steps for you as reference.

First, you need to have 5 important files in your project folder and make sure it locate at the root level of your folder. Here are the file list:

setup.sh
Procfile
requirements.txt
runtime.txt
.gitignore

For setup.sh, here is the code. You just need to replace the email to yours.

mkdir -p ~/.streamlit/
echo "[general]email = \"hugo@postcolour.com\"
" > ~/.streamlit/credentials.toml
echo "[server]
headless = true
port = $PORT
enableCORS = false
" > ~/.streamlit/config.toml

For Procfile, this is the one line code.

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

For requirements.txt file, here comes the best practice for the beginner.
The reason we need to have this file is becasue when we deploy our app, it will need to install all required packages and libraries. And pip install can use text document to automatically intall the necessary libraries. However, when we build our app, most of time I forget what I installed. Therefore, we need a tool called pipreqs. Here is the link for more information here.

pip install pipreqs

Once you installed, you just need to make sure you are at the root level of your project folder and run the code below.

# my_project pipreqs .

It automatically create a requirement.txt file in your folder with all libraries and the specific version you used. Here is what it looks like.

pandas==1.1.1
requests==2.24.0
awesome_streamlit==20200728.1
streamlit==0.66.0

For .gitignore, this will help you to avoid the file you don’t want to upload to your Github, like API and Raw data. There is a proper practice which I will share in the later article.

For runtime.txt, this is the most important thing motivate me to write this article. This file will help us to trouble shoot the Heroku deployment. The key point is that you need to make sure what python version you use to build the app, so that when you deploy on Heroku and system can install all required libraries successfully.

# my_project python --version
# my_project touch runtime.txt
# my_project echo python-3.7.7 > runtime.txt

Once you have all files in the folder, then you can push to the Github.

# git status
# git add .
# git commit -m "Your message here"
# git push origin master

Once you push all files, the final is almost there.

Since I already addressed all possible issue and solution on the top, you won’t have too much issue at this stage I guess.

Here are the code for deploying on Heroku.
* If you don’t have Heroku CLI, then you might need to install first. Information here.

# my_project heroku login
# my_project heroku create

When you create the heroku app, it will give you 2 URLs.

https://random-name-12345.herokuapp.com/ | https://git.heroku.com/random-name-12345.git

Heroku automatically create 2 random names for your app which I guess it’s for the security purpose. This link will also automatically added into your Git remote whcih you can use the below link to check.

# my_project git remote -v
heroku https://git.heroku.com/random-name-12345.git (fetch)
heroku https://git.heroku.com/random-name-12345.git (push)
origin git@github.com:username/app_name.git (fetch)
origin git@github.com:username/app_name.git (push)

If you see these 4 git remote in your list, then you can push to heroku master.

# my_project git push heroku master

Like I said before if you have a runtime.txt file to address your python version. The install process should be okay. Otherwise, you would see something like the picture below:

My python version is 3.7.7, but I don’t know why the system installed python-3.6.12 which has the compatible issue with the latest pandas and awesome-streamlit. If you need more official documents, please check here.

When the app is successfully deployed, you should see something like “done”.

remote: -----> Discovering process types
remote: Procfile declares types -> web
remote:
remote: -----> Compressing...
remote: Done: 140.4M
remote: -----> Launching...
remote: Released v3
remote: https://random-name-12345.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/random-name-12345.git
* [new branch] master -> master

And then run the last 2 codes to finish the deployment.

# my_project heroku ps:scale web=1
# my_project heroku open

The last code will automatically open the browser and you can start sharing your app online!

Note: You can change your app on Heroku. However, you would also need to update your git remote link. Here are the code for solving this issue.

# git remote rm heroku
# heroku git:remote -a newname
# git remote -v

The last line is for you to check if everything looks right to you.

--

--

Hugo Shih

Tech enthusiast, data analyst, film colorist. Love challenges and new adventures.