如何获取 python 中当前的 jupyter notebook 服务器?

How to get the current jupyter notebook servers in python?

如何获取 python 中当前 运行 个 Jupyter Notebook 服务器的列表?

有一个 jupyter-notebook 命令可以列出当前的笔记本服务器

machinename:~ username$ jupyter-notebook list
 http://localhost:8888 :: /Users/username/your/notebook/path
 http://localhost:8889 :: /Users/username/your/other/notebook/path
 ...

如何在 python 中完成此操作而不转到命令行并解析输出?

正在从 python

访问 运行 个笔记本服务器列表

可以通过实际的 python notebookapp 程序调用 list_running_servers() 从 python 中访问 运行 笔记本服务器的列表。

from notebook import notebookapp
servers = list(notebookapp.list_running_servers())
print(servers)

[{u'base_url': u'/',
  u'hostname': u'localhost',
  u'notebook_dir': u'/Users/username/your/notebook/path',
  u'pid':123,
  u'port': 8888,
  u'secure': False,
  u'url': u'http://localhost:8888/'},
 ...
 {u'base_url': u'/',
  u'hostname': u'localhost',
  u'notebook_dir': u'/Users/username/your/other/notebook/path',
  u'pid': 1234,
  u'port': 8889,
  u'secure': True,
  u'url': u'http://localhost:8889/'}]

这也为您提供了比命令行界面更多的信息。 \o/-不错!

您可以使用以下命令从命令行执行此操作:

find `jupyter --runtime-dir` -mtime -5 | grep nbserver | xargs cat

jupyter --runtime-dir returns Jupyter 存储大量关于内核和 Jupyter 服务器的 JSON 元数据文件的目录。 find-mtime 参数使其只显示最近 5 天内修改的文件。

在我的 MacBook 上,我得到以下结果:

{
  "base_url": "/",
  "url": "http://localhost:8888/",
  "port": 8888,
  "pid": 50017,
  "secure": false,
  "hostname": "localhost",
  "notebook_dir": "/Users/myusername"
}{
  "base_url": "/",
  "hostname": "localhost",
  "notebook_dir": "/Users/myusername",
  "password": false,
  "pid": 63644,
  "port": 8889,
  "secure": false,
  "token": "058fc6cbd6d793c6ddda420ff6d5d3c42819be526b68602d",
  "url": "http://localhost:8889/"
}

(我有两个不同版本的 Jupyter 环境)

只需根据您的目的使用 jupyter notebook list。它列出了所有 运行 个服务器:

<frankliuao/Volumes/Ao_HardDisk/$> jupyter notebook list
Currently running servers:
http://localhost:8889/? 
token=476f392542ef41bc020cf26c2ddac0128ee42c0d3c542ac7 :: 
/Users/frankliuao/Downloads/
http://localhost:8888/? 
token=b1a33f34b80ddfa2476550671599b566131e3d875d9d4250 :: 
/Users/frankliuao/Desktop

如果您想在 Python 中获取该信息,您可以使用 popen 执行 Shell 命令。

如果你能在你的 Jupyter Notebook 平台上安装 jupyter server proxy,你就可以使用这个 notebook + httpserver

https://github.com/gbrault/jpjwidgets