如何使用jupyter_client访问用户命名空间中的对象

2024-04-26 11:24:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我想以编程方式在IPython/Jupyer笔记本中执行单元格,并对它们进行一些内省,比如获取用户变量。在

与IPython.core.InteractiveShell这很简单:

from IPython.core.interactiveshell import InteractiveShell
shell = InteractiveShell()
shell.run_cell("foo = 1")
assert shell.ns_table['user_local']['foo'] == 1

但我不能使用InteractiveShell,因为我想捕获丰富的输出(as discussed in another question)。为了做到这一点,我必须使用jupyter_client,它使用到内核的ZeroMQ连接。在

我的问题是,如何从IPython内核的名称空间中检索对象,就像上面例子中ns_table所做的那样?在单元源代码中注入某种样板代码和/或对用户表达式进行pickle/unpickle是否不可避免(as discussed in yet another question)?在


Tags: 用户incorefooas编程ipythontable