0

For educational use, I’m trying to run Streamlit apps within a Google Colab Jupyter notebook.*

You can run my setup here. This worked consistently a few weeks ago.

Now, the app only loads correctly about 5% of the time. The other 95% I’m getting an error like this (similar to this question):

Failed to complete tunnel connection

The connection to 67567754f01f.ngrok.io was successfully tunneled to your ngrok client, but the client failed to establish a connection to the local address localhost:80 .

Make sure that a web service is running on localhost:80 and that it is a valid address.

The error encountered was: dial tcp 127.0.0.1:80: connect: connection refused

Any suggestions for fixes?

*Motivation: I’m using Colab so that I can share files and have students view and edit apps entirely in the browser, with no installation required. Frame challenges welcome if there’s a better way to achieve this!

Versions:

  • python 3.6.9
  • streamlit 0.71.0 (also tried a couple previous versions)
  • pyngrok 5.0.0 (also tried 4.2.2. This is a new major version - relevant?)
  • Firefox 82.0.3

Complete code and output:

Code:

!pip -q install streamlit
!pip -q install pyngrok

Output:

Building wheel for watchdog (setup.py) … done
Building wheel for blinker (setup.py) … done
Building wheel for pathtools (setup.py) … done
ERROR: requests 2.23.0 has requirement urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1, but you’ll have urllib3 1.26.2 which is incompatible.
ERROR: google-colab 1.0.0 has requirement ipykernel~=4.10, but you’ll have ipykernel 5.3.4 which is incompatible.
ERROR: datascience 0.10.6 has requirement folium==0.2.1, but you’ll have folium 0.8.3 which is incompatible.
Building wheel for pyngrok (setup.py) … done

Code:

%%writefile app.py
import streamlit as st
st.title(“hi”)

Output:

Writing app.py

Code:

from pyngrok import ngrok
public_url = ngrok.connect(port=‘80’)
print (public_url)
!streamlit run app.py >/dev/null

Output:

NgrokTunnel: “http://67567754f01f.ngrok.io” -> “localhost:80”
/usr/local/lib/python3.6/dist-packages/requests/init.py:91: RequestsDependencyWarning: urllib3 (1.26.2) or chardet (3.0.4) doesn’t match a supported version!
RequestsDependencyWarning)
t=2020-11-19T18:42:24+0000 lvl=warn msg=“failed to open private leg” id=61fef1ba5d31 privaddr=localhost:80 err=“dial tcp 127.0.0.1:80: connect: connection refused”
t=2020-11-19T18:42:24+0000 lvl=warn msg=“failed to open private leg” id=8da0b078aaa7 privaddr=localhost:80 err=“dial tcp 127.0.0.1:80: connect: connection refused”
sycamore
  • 1
  • 1
  • 4
  • What server are you starting on port 80 that would receive the connection? I'm not familiar with Streamlit, but it that supposed to be doing that? Connection refused would seem to imply nothing is running to receive the tunneled connection on that port. – alexdlaird Nov 19 '20 at 19:49
  • `pyngrok` has HTTP examples for Colab in its own documentation: https://pyngrok.readthedocs.io/en/latest/integrations.html#colab-http-example – alexdlaird Nov 19 '20 at 19:53
  • @alexdlaird "Internally, Streamlit uses Tornado to serve HTTP and WebSocket data to its frontend. That is, it’s already its own web server" (https://discuss.streamlit.io/t/serve-streamlit-within-flask/493) – sycamore Nov 19 '20 at 20:10
  • I edited the "streamlit run" call to specify port 80 and it's now working correctly maybe 20% of the time. I believe the default port is 8501, have tried that as well: https://docs.streamlit.io/en/stable/streamlit_configuration.html . Thanks for the note! – sycamore Nov 19 '20 at 20:11

3 Answers3

2

I was having the same problem, It was showing Failed to complete tunnel connection. and st the bottom it was showing The error encountered was: dial tcp 127.0.0.1:80: connect: connection refused.

As you said version issue?, I guess it is. Because I tried with pyngrok -> 4.1.1. And its working fine. Just do !pip install pyngrok==4.1.1.

Except that there is another problem in your code, I think in this part

from pyngrok import ngrok
public_url = ngrok.connect(port=‘80’)
print (public_url)
!streamlit run app.py >/dev/null

you should 1st run !streamlit run app.py >/dev/null, then go for

from pyngrok import ngrok
public_url = ngrok.connect(port=‘80’)

by doing that you are running the streamlit app in the background 1st and then you are creating a tunnel using ngrok for exposing. source .If I am wrong here, please correct me.

I got to know about it from this youtube video comment, which is at the bottom.

If you still face the problem re-start the notebook and run again.

0

Following the lead from alexdlaird in comments, I needed to specify that Streamlit should start a server on port 80:

!streamlit run --server.port 80 app.py >/dev/null

I should also have been able to do this by editing the Streamlit config file (explainer here), but that didn't work for me.

I've found that I also need to wait a couple of seconds between running Streamlit and opening up the web app to avoid the error.

sycamore
  • 1
  • 1
  • 4
0

check this: https://darekdari.com/streamlit-in-google-colab/

you can use localtunnel instead of pyngrok

it worked for me and may work for you

tous
  • 37
  • 8