How to Deploy Your Streamlit Apps to Heroku

In this tutorial , we will be learning how to deploy our awesome streamlit apps. The simplicity of streamlit makes it easier to build ML apps, Data Science Apps and other python apps. So what do you do if you want to deploy or publish your streamlit app?

There are several options and platforms that you can deploy your app to such as GCP,AWS. But in this tutorial we will deploy our app to  Heroku using their free tier.

To see how to deploy on AWS you can check out this article by Alex T. 

The basic steps include the following:

  • Create an Account on Heroku
  • Install the Heroku CLI and setups (we will discuss it in a moment)
  • Create a Git Account on GitHub or GitLab,etc
  • Create A Repository for your app
  • Clone Your App locally
  • Have a setup.sh,Requirements.txt and ProcFile
  • Make your changes and push to your git account
  • Create A heroku app from the CLI
  • Deploy by pushing to heroku

CREDITS : gabe_maldonado for Quick Solution For Deployment Steps

Let us see the various steps in details.

Create An Account on Heroku and CLI Installation

To create an account you can go to their official website here and sign up.

The next thing is to install the heroku CLI on your system. They provide support for all the various major OS. So you can use the one based on your system. The CLI makes your work easier but of course you can also utilize their web interface when you login in into your account.

 

After installing the heroku CLI, you can then login from your terminal as below

heroku login

This will prompt you to press any key to open your browser to login. Then you are set to go.

Using Git and Cloning Repo Locally

If you have a git account either from github/gitlab you can just skip the signup step but in case you do not you can create an account. The next step is to create a repository where you will place the code for your app. It is this repo that you will be using with heroku for your deployment. You can then clone it locally and do all your work. Within this repo there are three essential files you need to allow your app to be deployed.

The Required Files

There are 3 main files which are required to deploy your apps on heroku. These include the

requirements.txt file: the file with your requirements for running the app such as your dependences,etc.

To create a requirements.txt file, it is recommended that you use a virtual environment to avoid copying/freezing all your python packages on your system to the requirements.txt file. So from your virtual env you can run the following to create the requirements.txt file

pip freeze > requirements.txt

or  with pipenv virual environment

pipenv run pip freeze > requirements.txt

Heroku uses your requirements.txt file to detect which language to use(in our case,python)

setup.sh : this file is where you write your credentials,config as well as to create a  folder for streamlit. You will then place the code below in this file. You can choose any of them as you want

Without your credentials(i.e – email)

mkdir -p ~/.streamlit/

echo "\
[server]\n\
headless = true\n\
port = $PORT\n\
enableCORS = false\n\
\n\
" > ~/.streamlit/config.toml

Or With Your Credentials(optional- not recommended since your may commit your email as part of the file)

mkdir -p ~/.streamlit/

echo "\
[general]\n\
email = \"youremail@domain.com\"\n\
" > ~/.streamlit/credentials.toml

echo "\
[server]\n\
headless = true\n\
enableCORS=false\n\
port = $PORT\n\
" > ~/.streamlit/config.toml

Procfile: this file is required by heroku for your configurations. It contains  how  and with what you want to run your app. In most flask apps, it is this file you place your gunicorn command. But for streamlit apps we will be using a different approach.

You just create a text file with the name Procfile . Then place in the following code

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

What it means is that for deployment and serving this app use bash(sh) to run the setup.sh file then after that start streamlit to run the app.

After making your changes and your commits. You can first push it to your repository on github/gitlab.

Create An App on Heroku

You can either us the web interface or the CLI, but let us use the CLI. So from your terminal, you can create an app as follows

heroku create yourappname

The yourappname is the name you want to use as your url for your app. Hence your url for your app will be yourappname.herokuapp.com In case you leave it blank like

heroku create

It will randomly generate an app name for you.

To see your app created and to show it in your browser , you can then enter

heroku open

This will show you a default page. Finally let us deploy our app

Deploying the App

To publish or deploy our app, you then push it to heroku with this command

git push heroku master

It will then setup your app with the Procfile ,and install the required packages using your requirements.txt file. After that , you can then see your app deployed and live.

Streamlit is Awesome.

In case you have issues such as

fatal: 'heroku' does not appear to be a git repository
fatal: Could not read from remote repository.

That means your directory is not seen by heroku as a git repository. To fix it you just have to add it by

heroku git:remote -a yourapp_repo_where_you_are

This fixes it. You can also check the logs via heroku –logs.  or from your web ui.

CREDITS : gabe_maldonado for Quick Solution For Deployment Steps

You can also check the video tutorials below for the entire process.

Thanks for your time

Jesus Saves

By Jesse E.Agbe(JCharis)

 

22 thoughts on “How to Deploy Your Streamlit Apps to Heroku”

  1. Hi,

    I can run: streamlit run íris_eda_app.py and open in a browse, ok.

    But I get a error message when do it:

    C:\Users\silvi\Documents\GitHub\Streamlit>pipenv run streamlit run iris_eda_app.py
    C:\Users\silvi\.virtualenvs\Streamlit-XLCeM1P1\Scripts\python.exe: No module named streamlit.__main__; ‘streamlit’ is a package and cannot be directly executed

    I´m running on win 10 and python 3.7.2

    Could you help me please ?

    1. Hello Silvio,you can fix it by installing streamlit on your system itself as well as the pipenv.
      For your system
      pip install streamlit

      For the virual environment(This is to be able to get streamlit on the requirements.txt)
      pipenv install streamlit

      Then when running the app you can either use
      streamlit run your_app.py

      or
      pipenv run streamlit run your_app.py

  2. Hi,

    I did the deploy and anything is fine but when try to launch my app on the browser. I am getting the error below

    2020-04-05T22:41:29.081355+00:00 heroku[web.1]: State changed from crashed to starting
    2020-04-05T22:41:45.791622+00:00 heroku[web.1]: State changed from starting to crashed
    2020-04-05T22:41:45.733389+00:00 app[web.1]: bash: streamlit: command not found
    2020-04-05T22:42:03.856688+00:00 heroku[router]: at=error code=H10 desc=”App crashed” method=GET path=”/” host=wineclassifierapp.herokuapp.com request_id=ae31221d-5594-439b-b402-520d826a1d81 fwd=”187.65.211.112″ dyno= connect= service= status=503 bytes= protocol=https

    i am running in a ubuntu 18.04

    Could you help me?

    1. Hello Bruno, it looks like you do not have streamlit in your path . You can either install streamlit again
      using pip eg pip install streamlit
      And add it to your path( bashrc or bashprofile)
      or try using python -m streamlit run your_app.
      Hope it helps

      1. Thanks Jesse_Jcharis for your response. I can be wrong but i think its not a PATH issue because i can run it locally.
        I am having problems to serve it in a browser. Receiving the message below.

        Application error

        An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details. You can do this from the Heroku CLI with the command
        heroku logs –tail

        What i posted before was the heroku logs –tail of the error.

        Thank you Jesse.

        1. I have the same issue.
          Jesse, please help. all files are in https://github.com/KhaledAbouSamak/streamlit
          it raised the following error:

          2020-05-09T01:33:14.384647+00:00 heroku[web.1]: State changed from starting to crashed
          2020-05-09T01:33:19.745916+00:00 heroku[router]: at=error code=H10 desc=”App crashed” method=GET path=”/” host=bullzaye-streamlit-api.herokuapp.com request_id=c948d34c-caff-460d-a03c-f41c0f706bdb fwd=”31.215.135.80″ dyno= connect= service= status=503 bytes= protocol=https
          2020-05-09T01:33:20.054546+00:00 heroku[router]: at=error code=H10 desc=”App crashed” method=GET path=”/favicon.ico” host=bullzaye-streamlit-api.herokuapp.com request_id=7ce068e1-dc72-4d31-905a-ee94094177f9 fwd=”31.215.135.80″ dyno= connect= service= status=503 bytes= protocol=https

          1. jesse_jcharis

            Hello Khaled, please I wanted to ask if it works locally on your system.
            The H10 error is an app crashing error. Can you redeploy the app again?

          2. Hi Jesse. Thanks for your reply. It is working on my local machine However, I tried to deploy it gain but it is still not working. Do you think the requirement.txt file is correct?

            You can check it in here https://github.com/KhaledAbouSamak/streamlit

            I really appreciate your help.
            Best regards,
            Khaled

  3. Hi there! I know this is kinda off topic but I’d figured I’d ask. Would you be interested in exchanging links or maybe guest authoring a blog article or vice-versa? My site discusses a lot of the same subjects as yours and I feel we could greatly benefit from each other. If you might be interested feel free to send me an e-mail. I look forward to hearing from you! Superb blog by the way!

  4. I tried creating a spacy App to detect NER and dependencies. when I run my app it shows that
    nlp = spacy.load(“en_core_web_md”) \\ model doesn’t exist.

    I want to ask whether that, do we need to write a command to download model in app.py file or requrement.txt

    1. jesse_jcharis

      Hi Pallavit, you will need to add it to your requirements.txt file eg

      https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-2.2.0/en_core_web_sm-2.2.0.tar.gz#egg=en_core_web_sm

      Hope it helps

    1. jesse_jcharis

      Hello Avi, you will need to get more space on your dynos on heroku. I think for more space you will need the
      paid version instead of the free.
      Hope it helps

  5. All the image files are there in my directory . Whenever I am running it on heroku server it’s showing “no such file directory” . But when I am running it on streamlit server using “streamlit run app_name” command it’s not raising that error . I have also kept an empty “.keep” file in directory . How can I fix it?

    1. Hi,please did you push the image to github repo? If so you can load it directly from github to the app and check it an see if it will work. Hope it helps

  6. Hi Jessie!
    I always enjoy your videos. Your explanations are always good.
    But this I cannot deploy the app 🙁
    It’s all good with Streamlit and GitHub but I am getting the below log when pushing heroku to master…
    —–> Python app detected
    —–> Installing python-3.6.12
    —–> Installing pip 20.1.1, setuptools 47.1.1 and wheel 0.34.2
    —–> Installing SQLite3
    —–> Installing requirements with pip
    ERROR: Invalid requirement: ‘Package Version’ (from line 1 of /tmp/build_36ec69b1/requirements.txt)
    ! Push rejected, failed to compile Python app.
    ! Push failed

    It seems to be related to the “requirements” file ? Does it matter that my Python version (3.8.6) is different from the one on Heroku as displayed above (version 3.6.12)? Same with “pip” and “setuptools”? “wheel” is not in my requirements.txt.
    Also, is it because my github repo is set to “private”? The link is:
    https://github.com/loic-nazaries/exam_piscine_heroku

    I have been working on this app for several months now, so frustrating to be a click away to delivering my first data science project with Python!

    Many thanks in advance.

    1. Hello Loic, thanks for the remark.
      About the error, for the commands used in the post, your repo needs to be public.
      Except you change the setting on your heroku dashboard to work with your private repo.
      I think this will fix it.
      Let me know the outcome

Leave a Reply to Hello Cancel Reply

Your email address will not be published. Required fields are marked *