ipython notebook是什么_如何获取当前的IPython Notebook名称

正如已经提到的,你可能不是真的应该这样做,但我确实找到了办法.这是一个火红的黑客,所以不要依赖于此:

import json

import os

import urllib2

import IPython

from IPython.lib import kernel

connection_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.x

if 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']

break

else:

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中“工作”的解决方案,至少通过一个简单的测试.如果有多个笔记本连接到同一内核,可能无法保证给出正确答案.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值