Skip to content

bflaven/deploy-a-streamlit-app-with-heroku

Repository files navigation

deploy-a-streamlit-app-with-heroku

Yet another heroku deployment streamlit application directory... Some of the commands required to deploy a streamlit app on Heroku.

The streamlit app deployed from this directory is available at https://tiny-streamlit-dashapp.herokuapp.com/

How-to deploy to Heroku with Heroku-CLI

It requires few requirements before starting to deploy both on Heroku or on Streamlit:

1. Install Homebrew for Mac

# It supposed that Homebrew is already installed on your computer. 
# https://brew.sh/
# to launch the Homebrew install, you can run the following command
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# check the install
brew --version

2. Install Git Using Homebrew

To install Git, we will use Homebrew, the package management system for Mac.

Run the following brew command in the terminal:

brew install git
# Then, check the Git version to verify the installation:

# to check the git install
git --version

3. Install Heroku CLI Using Homebrew

In this step you'll install the Heroku Command Line Interface (CLI). You use the CLI to manage and scale your applications, provision add-ons, view your application logs, and run your application locally.

brew doctor 
brew update
brew install heroku/brew/heroku
heroku autocomplete

Source :: https://docs.brew.sh/Shell-Completion

Testing Heroku CLI

heroku --version

4. Create your environment with Conda

Go to the dir It is just I found easier to be in same git directory to create my environment with Conda because I know better what are the packages required by the streamlit app.

# go to your directory
cd /Users/brunoflaven/Documents/03_git/deploy-a-streamlit-app-with-heroku/

Create your dev env with conda

# listing the envs
conda info --envs

# create the env for your streamlit app
conda create --name deploy_getting_started python=3.9.7

Get into your dev env

How to create a development environment and also list and deactivate some dev env.

# go into the env
conda activate deploy_getting_started


# Let's say you create a environment with this version of python (3.8.3) if you need yo update the python version of your env
# upgrade python version in your env heroku_python_getting_started_3a
conda create --name heroku_python_getting_started_3a python=3.8.3
conda install python=3.9.7

# listing the envs
conda info --envs
conda remove --name heroku_python_getting_started_3b --all


# get from the current env
conda deactivate

Install packages in your dev env

# install the packages in the env
pip install streamlit
pip install watchdog

Save python requirements in a file name requirements.txt

# show what the requirements
pip freeze > requirements_1_heroku_python_getting_started_3.txt
pip freeze > requirements_2_heroku_python_getting_started_3.txt
pip freeze > requirements_3_heroku_python_getting_started_3.txt

# rename the last version with the correct name requirements.txt, heroku only accept the filename requirements.txt
mv requirements_1_heroku_python_getting_started_3.txt requirements.txt

Install other packages in your dev env required by your app

pip install pandas
pip install numpy
pip install matplotlib
pip install plotly-express
pip install matplotlib
pip install altair

Extra info for the app

# install the packages required to work with the streamlit app
numpy==1.18.4
pandas==1.0.3
seaborn==0.10.1
matplotlib==3.4.2
plotly-express==0.4.1
altair==4.1.0

5. Deploy to heroku (tiny-streamlit-dashapp)

The app name is tiny-streamlit-dashapp. We will perform 2 actions:

  • build your app with streamlit
  • update in github.com
  • deploy to heroku

5.1 Build your app with streamlit

It does not have to be sophisticated app as for the moment we want to learn how to deploy on platform such as Heroku or Streamlit.

An advice: grab any simple app on GitHub, create your own Streamlit app or take the one in this directory but stay simple for the moment.

5.2 Commit on GitHub with Git

# go to your dir
cd /Users/brunoflaven/Documents/03_git/deploy-a-streamlit-app-with-heroku/

# checkout status
git status
# output the changes in files

git branch
# main 

# commit changes example
git add .
git commit -am "update README.md"

# first push to git
git push origin main

5.3 Deploy on HEROKU with Heroku CLI

  • Login the Heroku CLI
# login to Heroku
heroku login
  • Create a new Git repository for the first time
# go to the dir
cd /Users/brunoflaven/Documents/03_git/deploy-a-streamlit-app-with-heroku/ 

# you have probably installed before Git on your computer
git init 

# push your app with the name given by heroku that have been created on heroku 
# My app is called: tiny-streamlit-dashapp
heroku git:remote -a tiny-streamlit-dashapp

# for any change just type this command
git add .

# add a commit with a message
git commit -am "remove runtime.txt"

# push to heroku because my branch is main
git push heroku main

Caution: one thing to remember it is the branch name that you are using to push both on github, heroku

# CAUTION depend if your branch on github is master or main


### HEROKU
# push to heroku if your branch on github is master
git push heroku master

# push to heroku if your branch on github is main
git push heroku main


### GITHUB
# push to github if your branch on github is master
git push origin master

# push to github if your branch on github is main
git push origin main

In the file runtime.txt, add the correct version of python you are using.

python-3.9.7

The example given by heroku: install heroku-cli and deploy python app with heroku

Some of the steps extracted from a tutorial given by Heroku to deploy a sample app. The sample app is not a streamlit one, it is a dash application.

(1.1) Install the Heroku CLI

  • It requires Git installed
# It supposed that Git is already installed on your computer. Check in the console with 

git --version

# If Git is not installed, please do install it whith homebrew
  • To install Git with homebrew if it is not already done!
brew install git
# To confirm, open a new terminal window/tab and type
git --version

In this step you'll install the Heroku Command Line Interface (CLI). You use the CLI to manage and scale your applications, provision add-ons, view your application logs, and run your application locally.

brew doctor 
brew update
brew install heroku/brew/heroku
heroku autocomplete

Source :: https://docs.brew.sh/Shell-Completion

(1.2) Testing Heroku CLI

heroku --version
heroku login

(1.3) Take the default app

  • clone the project given by heroku
git clone https://github.com/heroku/python-getting-started.git heroku-python-getting-started
  • go to the dir
cd python-getting-started

(1.4) create your env with conda

  • Create an Environment with version Python 3.8.3 To create a virtual environment use:
conda create --name heroku_python_getting_started_1 python=3.8.3
  • List Environments: You can list all the available environments with:
conda info --envs
  • Activate an Environment : Before you can start using the environment you need to activate it:
source activate heroku_python_getting_started_1
  • Deactivate an Environment If you are done working with the virtual environment you can deactivate it with:
conda deactivate
  • Remove an Environment If you want to remove an environment from Conda use:
conda remove --name heroku_python_getting_started_1 --all

(1.5) activate the env with conda

conda activate heroku_python_getting_started_1

(1.6) install the requirements with the requirements.txt ! be sure to be in the "heroku_python_getting_started_1" env

pip install -r requirements.txt
  • update the python in your environnement
conda install python=3.9

(1.7) create your app

  • Create an app on Heroku, which prepares Heroku to receive your source code:
heroku create

# Creating app... done, ⬢ agile-shore-37837
# https://agile-shore-37837.herokuapp.com/ | https://git.heroku.com/agile-shore-37837.git
  • Heroku generates a random name (in this case agile-shore-37837) for your app, or you can pass a parameter to specify your own app name.

  • Now deploy your code:

git push heroku main
  • The application is now deployed. Ensure that at least one instance of the app is running:
heroku ps:scale web=1
  • Now visit the app at the URL generated by its app name. As a handy shortcut, you can open the website as follows:
heroku open
  • View information about your running app using one of the logging commands:
heroku logs --tail

(1.8) Define a Procfile

Use a Procfile, a text file in the root directory of your application, to explicitly declare what command should be executed to start your app.

The Procfile in the example app you deployed looks like this:

web: gunicorn gettingstarted.wsgi

This declares a single process type, web, and the command needed to run it. The name web is important here. It declares that this process type will be attached to the HTTP routing stack of Heroku, and receive web traffic when deployed.

Procfiles can contain additional process types. For example, you might declare one for a background worker process that processes items off of a queue.

(1.9) Scale the app

You can check how many dynos are running using the ps command:

  • excercice to scale up and down Scaling an application on Heroku is equivalent to changing the number of dynos that are running. Scale the number of web dynos to zero:
heroku ps:scale web=0
heroku ps:scale web=1

(1.10) Declare app dependencies (locally)

pip install -r requirements.txt
pip list

(1.11) Run the app locally

The app is almost ready to start locally. Django uses local assets, so first, you’ll need to run collectstatic: python manage.py collectstatic

heroku local web
# Open http://localhost:5000

(1.12) Make an change in the application

In python-getting-started/hello/templates/index.html change the H1

git add .
git commit -m "Change the H1 with a custom text"
git push heroku main
heroku open
  • to see change locally
heroku local web

(1.13) Provision add-ons

heroku addons:create papertrail
heroku addons
heroku addons:open papertrail

(1.14) Start a console

  • do not forget to type exit
heroku run python manage.py shell
  • To get a real feel for how dynos work, you can create another one-off dyno and run the bash command, which opens up a shell on that dyno.
heroku run bash
# type "$ exit" to exit from the terminal

(1.15) More command for Heroku CLI Commands

https://devcenter.heroku.com/articles/heroku-cli-commands

  • command to view all apps
heroku apps --all 
  • command to destroy an app
heroku apps:destroy -a agile-shore-37837 --confirm agile-shore-37837

Some extra GIT commands

  • how to remove a directory or a subdirectory from a git directory
# This deletes from filesystem
git rm -r one-of-the-directories
# Make the commit
git commit . -m "Remove duplicated directory"
# Git push (typically 'master', but not always)
git push origin <your-git-branch> 
  • simple example to delete a directory
rm -r deploy-a-streamlit-app-with-Heroku
git add .
git commit -m "add deploy-a-streamlit-app-with-Heroku"
git commit -m "add files"
git push
  • to add, commit and push
git add .
# Adds the file to your local repository and stages it for commit. To unstage a file, use 'git reset HEAD YOUR-FILE'.
git commit -m "Add existing file"
# Commits the tracked changes and prepares them to be pushed to a remote repository. To remove this commit and modify the file, use 'git reset --soft HEAD~1' and commit and add the file again.
git push origin main
# Pushes the changes in your local repository up to the remote repository you specified as the origin
# git push  <REMOTENAME> <BRANCHNAME> 
# git push origin main
  • to check the remote branch
git branch
# output
# * main

4. Installing the Heroku Command Line Interface (CLI)

In my case this is done.... so let's start using Heroku Command Line Interface

  • (1.2) Testing Heroku CLI
heroku --version
heroku login
# you should be login
  • do not forget to be in your directory
cd deploy-a-streamlit-app-with-heroku/
  • launch the creation
heroku create
  • Create a new Git repository Initialize a git repository in a new or existing directory
cd deploy-a-streamlit-app-with-heroku/
# no need to do that
# git init
git status
heroku git:remote -a nameless-woodland-72201
  • Specify a Buildpack Version
heroku buildpacks:set heroku/python
  • Deploy your application Commit your code to the repository and deploy it to Heroku using Git.
# push your app
git add .
git commit -am "made it or not with heroku and git 4"
git push heroku master
#git push heroku develop:master
#git push heroku main
#git push heroku develop:master
git commit -am "remove from main dir"
git push
  • Existing Git repository For existing repositories, just simply add the heroku remote
heroku git:remote -a nameless-woodland-72201
heroku login
heroku git:remote -a <heroku-project-name>
git push heroku master

VIDEOS

RESSOURCES

About

yet another heroku deployment streamlit application directory

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published