krypticmouse
krypticmouse

Reputation: 81

Colab: Unable to access webpage using flask-ngrok

I'm trying to run flask application on colab but I keep seeing the following when I go to the tunneled page:-

enter image description here

Code:-

# flask_ngrok_example.py
from flask import Flask
from flask_ngrok import run_with_ngrok

app = Flask(__name__)
run_with_ngrok(app)  # Start ngrok when app is run

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == '__main__':
    app.run()

Upvotes: 1

Views: 4464

Answers (3)

I am using Google Colab for solving the above issue. First I would like you to visit the official page of ngrok. Then, login with your own google account and after you have created an account go the Your Authtoken section and copy your authtoken from there.

Now that you have copied the authtoken, go and create a Google Colab file and write the following lines of code:

!pip install flask_ngrok pyngrok
!ngrok authtoken your_own_authtoken

Then after running the above code cell, go on and write the below code snippet:

from flask import Flask
from flask_ngrok import run_with_ngrok
from pyngrok import ngrok

app = Flask(__name__)
run_with_ngrok(app)
@app.route("/")
def home():
    return "<h1>Hello World</h1>"
ngrok_tunnel = ngrok.connect(5000)
app.run()

This will give you a ngrok-free.app link in the cell output which on clicking will lead you to a webpage where you can click on the Visit Site option to see your output message which in this case is Hello World.

Spent a lot of time on this and after lot of hits and trials, finally found this solution which worked for me. Hope it helps. Happy learning!

Upvotes: 0

Real Uniquee
Real Uniquee

Reputation: 531

To not render to ngrok-signup page when using flask-ngrok you should authenticate the ngrok from your local-machine using your authentication token from ngrok website.

First install dependencies.

pip install flask-ngrok
pip install pyngrok==4.1.1

Authenticate with ngrok with your token

!ngrok authtoken <ngrok-token>

Now you can write your code as sample below:

# flask_ngrok_example.py
from flask import Flask
from flask_ngrok import run_with_ngrok

app = Flask(__name__)
run_with_ngrok(app)  # Start ngrok when app is run

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == '__main__':
    app.run()  

Upvotes: 0

sir5338 chung
sir5338 chung

Reputation: 39

On Colab, you can run these commands:

!pip install pyngrok==4.1.1
!ngrok authtoken 'your authtoken'

Upvotes: 3

Related Questions