Colab:无法使用flask-ngrok访问网页

问题描述 投票:0回答:2

我正在尝试在 Colab 上运行 Flask 应用程序,但当我进入隧道页面时,我不断看到以下内容:-

代码:-

# 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()
python flask google-colaboratory web-deployment web-development-server
2个回答
3
投票

在 Colab 上,您可以运行以下命令:

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

0
投票

在 Google Colab 中安装附加软件包

!pip install flask-ngrok
!pip install pyngrok==4.1.1

为了在代码中使用

flask-ngrok
,并方便使用ngrok,您需要从ngrok网站获取身份验证令牌。获取token后,运行以下命令:

!ngrok authtoken <ngrok-token>

现在,您可以在 Colab 单元中编写代码。这是使用 Flask 和 ngrok 的示例:

# 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()  

运行代码时,您将看到类似于以下内容的输出:

 * Serving Flask app '__main__'
 * Debug mode: off
INFO:werkzeug:WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000
INFO:werkzeug:Press CTRL+C to quit
 * Running on http://36ed-35-237-206-208.ngrok-free.app
 * Traffic stats available on http://127.0.0.1:4040
INFO:werkzeug:127.0.0.1 - - [17/Dec/2023 11:54:29] "GET / HTTP/1.1" 200 -
INFO:werkzeug:127.0.0.1 - - [17/Dec/2023 11:54:31] "GET /favicon.ico HTTP/1.1" 404 -

恭喜!您已在 Google Colab 中成功运行 Flask。

© www.soinside.com 2019 - 2024. All rights reserved.