如何在python中获取当前运行的Jupyter Notebook Server列表?
有一个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()
.
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/-nice!
您可以使用以下命令从命令行执行此操作:
find `jupyter --runtime-dir` -mtime -5 | grep nbserver | xargs cat
jupyter --runtime-dir
返回Jupyter存储大量关于内核和Jupyter服务器的JSON元数据文件的目录.该-mtime
参数find
使得它表明仅在最近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环境)