Ale*_*usk 5 python zeromq jupyter
我想以编程方式执行 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
Run Code Online (Sandbox Code Playgroud)
但我无法使用 InteractiveShell,因为我想捕获丰富的输出(如另一个问题中所述)。为了做到这一点,我必须使用jupyter_client,它使用 ZeroMQ 连接到内核。
我的问题是,如何从 IPython 内核的命名空间中检索对象,就像上面示例中 ns_table 所做的那样?某种样板代码注入单元源和/或用户表达式的酸洗/取消酸洗是否不可避免(如另一个问题中所讨论的)?