Hosting streamlit on Heroku

I’m trying to host streamlit on Heroku but so far I haven’t been successful.

After a series of trial and error I’m launching my web dyno with (content of my Procfile):
web: sh setup.sh && streamlit run some_script.py
Where setup.sh is

mkdir -p ~/.streamlit/

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

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

This setup script ensures that the port is properly set, that I’m running a headless server and that the boot process does not get stuck waiting for a valid email.

By running heroku logs --tail I see that streamlit as started correctly.

When I access https://my-streamlit-app-on-heroku.herokuapp.com/ I get a streamlit’s empty landing page (with the burguer button on the top right, with a message next to it saying “connecting”).
Eventually I get a popup error saying “Connection failed with status 0.”

Any idea on what I’m might be doing wrong?
Thanks

3 Likes

Hi, Alexandre. I believe that this won’t work because Heroku doesn’t currently support websockets, which Streamlit requires.

However, I don’t want to jump to conclusions because I wasn’t involved in the Streamlit + Heroku effort here. So I pinged some Streamlit engineers who worked on this to provide more details!

Please understand the delay as we get back to you.

Thank you for using Streamlit!

Alexandre:

This issue is now being tracked here. Please follow for more updates.

Thanks for your question! :hugs:

@thiago this sounds like the issue we were dealing with today

Yup! We just released version 0.47.3 with a cherrypick of this bugfix.

Please give it a go and let me know if it works!

Hi,

I’m not able to make it work with version 0.47.4. All web socket request on /stream

got a 403 HTTP response and page is stuck on “connecting… please wait…”.

Was someone able to make it work on heroku or not ?

Thx!

OK I was able to make it work by disabling CORS in setup.sh :

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

Awesome! I didn’t have time to try the new version since I originally posted but that’s good to know.

@Julien_Almarcha and @Alexandre_Domingues: Please note that since 0.48 these config options can now be specified directly on the command line e.g.:

streamlit run --server.enableCORS false my_script.py

Yay! :partying_face:

1 Like

Thx, good to know!

Please guys, could you give some tips to publish a simple script in python using heroku ?
A tutorial ?

Hi @silviolima07

I found the video below via awesome-streamlit.org

2 Likes

Have a look at this gist:


all you need are 3 files:

  • Your python code
  • a Pipefile or a requirements.txt file
  • a Procfile
3 Likes

I see most of the comments are from 2019, but if anyone trying to deploy on Heroku today make sure your requirements.txt file has streamlit version code. If not specified it keeps showing “Connecting to streamlit” when viewed through the link. Maybe a bug in the latest version.

  1. requirements.txt
    streamlit==0.49.0

  2. setup.sh
    mkdir -p ~/.streamlit/
    echo "
    [server]\n
    headless = true\n
    enableCORS=false\n
    port = $PORT\n
    \n
    " > ~/.streamlit/config.toml

3.Procfile
web: sh setup.sh && streamlit run your_app.py

This worked like charm.

4 Likes

Works perfectly thankyou for this

2020-07-14T15:27:22.074324+00:00 heroku[web.1]: Starting process with command sh setup.sh && streamlit run app.py
2020-07-14T15:27:29.434867+00:00 heroku[web.1]: Process exited with status 134
2020-07-14T15:27:29.475031+00:00 heroku[web.1]: State changed from starting to crashed
2020-07-14T15:27:29.343638+00:00 app[web.1]:
2020-07-14T15:27:29.343681+00:00 app[web.1]: Telemetry: As an open source project, we collect usage statistics.
2020-07-14T15:27:29.343682+00:00 app[web.1]: We cannot see and do not store information contained in Streamlit apps.
2020-07-14T15:27:29.343682+00:00 app[web.1]:
2020-07-14T15:27:29.343682+00:00 app[web.1]: If you’d like to opt out, add the following to ~/.streamlit/config.toml,
2020-07-14T15:27:29.343687+00:00 app[web.1]: creating that file if necessary:
2020-07-14T15:27:29.343687+00:00 app[web.1]:
2020-07-14T15:27:29.343688+00:00 app[web.1]: [browser]
2020-07-14T15:27:29.343692+00:00 app[web.1]: gatherUsageStats = false
2020-07-14T15:27:29.343693+00:00 app[web.1]:
2020-07-14T15:27:29.369552+00:00 app[web.1]: libgcc_s.so.1 must be installed for pthread_cancel to work
2020-07-14T15:27:36.000000+00:00 app[api]: Build succeeded

@Nikhil_Agarwal , kindly help to me solve the above issue as you mentioned followed same steps but giving same application error.

Please follow this ( ignore the Docker portion). It will work as I’ve tested it

Thank you, Nikhil! Your post and this tutorial helped get my app ready to go :slight_smile:

Hello! I am using streamlit==0.67.1, and I keep seeing this error message '‘Connection failed with status 0.’. Even when running ‘streamlit hello’ I see this error. I’m not sure how to troubleshoot this, could someone assist? I’m using a virtual machine and I wasn’t seeing this error 3 weeks ago, but it has been happening for 2 weeks now.

@anasalu I had the same problem. Try Nikhil’s solution above with the Streamlit version set to 0.49.0 and edit the setup.sh file to exlude any new line characters (\n). Also, if you copy the code, be sure that the quotation characters an non-directional (should be the default of your keyboard, so you might just have to retype those). If this doesn’t work, try including a runtime.txt file in your directory specifying the version of Python. My Streamlit app is working with python-3.7.5 in my runtime.txt file.