sycamore
sycamore

Reputation: 1

Streamlit with Colab and pyngrok - Failed to complete tunnel connection - version issue?

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:

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”

Upvotes: 0

Views: 12387

Answers (3)

tous
tous

Reputation: 37

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

Upvotes: 0

Soumyadip Sarkar
Soumyadip Sarkar

Reputation: 321

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.

Upvotes: 2

sycamore
sycamore

Reputation: 1

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.

Upvotes: 0

Related Questions