栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何获取当前的IPython / Jupyter Notebook名称

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何获取当前的IPython / Jupyter Notebook名称

如前所述,您可能真的不应该能够执行此操作,但是我确实找到了一种方法。尽管这是一个令人发指的骇客,所以请不要完全依赖于此:

import jsonimport osimport urllib2import IPythonfrom IPython.lib import kernelconnection_file_path = kernel.get_connection_file()connection_file = os.path.basename(connection_file_path)kernel_id = connection_file.split('-', 1)[1].split('.')[0]# Updated answer with semi-solutions for both IPython 2.x and IPython < 2.xif IPython.version_info[0] < 2:    ## Not sure if it's even possible to get the port for the    ## notebook app; so just using the default...    notebooks = json.load(urllib2.urlopen('http://127.0.0.1:8888/notebooks'))    for nb in notebooks:        if nb['kernel_id'] == kernel_id: print nb['name'] breakelse:    sessions = json.load(urllib2.urlopen('http://127.0.0.1:8888/api/sessions'))    for sess in sessions:        if sess['kernel']['id'] == kernel_id: print sess['notebook']['name'] break

我更新了答案,以包含至少在一个简单的测试中即可在IPython 2.0中“运行”的解决方案。如果有多个笔记本连接到同一内核等,则可能无法保证给出正确的答案。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/662081.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号