You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从浏览器访问Colab Notebook服务器上运行的本地服务

在Google Colab中访问localhost:8787服务的方法

使用Jupyter Server Proxy的操作步骤

  1. 安装依赖
    在Colab代码单元格中执行:
    !pip install jupyter-server-proxy

  2. 重启内核
    点击顶部菜单栏「Runtime」→「Restart runtime」,确保安装的包生效。

  3. 启动目标服务
    运行你的代码,确认服务在http://localhost:8787正常运行。

  4. 配置代理并访问
    新建代码单元格,运行以下代码:

from jupyter_server_proxy.handlers import ProxyHandler
import tornado.web
import tornado.ioloop

app = tornado.web.Application([
    (r'/proxy/8787/(.*)', ProxyHandler, {'target': 'http://localhost:8787/'}),
])

app.listen(8000)
print("访问路径:在当前Colab地址后添加 /proxy/8787/")

之后直接修改浏览器地址栏,在原Colab URL末尾加上/proxy/8787/即可访问服务。

更简便的替代方案:Colab内置端口转发

如果不想用Jupyter Server Proxy,Colab自带的端口转发功能更快捷:

  • 启动服务后,运行以下代码:
from google.colab.output import serve_kernel_port_as_window
serve_kernel_port_as_window(8787)

运行后会自动弹出新窗口直接访问服务;若想在Colab页面内嵌入,可替换为serve_kernel_port_as_iframe(8787)。

内容的提问来源于stack exchange,提问作者Dagang Wei

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 05:58:16