Jupyter==4.1.0,Python==2.7.10,IPython==4.2.0
我正在为我的Jupyter笔记本编写SQL UI,并希望使用多线程,以便在运行查询时可以在其他单元格中继续工作。
问题是,如果我在一个单元格中执行查询,则输出将显示在最后执行的单元格的输出提示中,而不是在执行查询的单元格的输出提示中。
我搜索了互联网并发现了这个聪明的技巧,但我认为它已过时和/或不再适用于我的Jupyter版本。当我运行它时,我只得到最后执行的单元格的输出。因此,如果我同时运行两个单元格,我只会得到最后执行的输出,而不是同时打印到不同单元格的输出。
所以我有一个上下文管理器,它设置了parent_header
:
import sys
import threading
from contextlib import contextmanager
# we need a lock so that other threads don't snatch control
# while we have set a temporary parent
stdout_lock = threading.Lock()
@contextmanager
def set_stdout_parent(parent):
"""a context manager for setting a particular parent for sys.stdout
the parent determines the destination cell of the output
"""
save_parent = sys.stdout.parent_header
with stdout_lock:
sys.stdout.parent_header = parent
try:
yield
finally:
# the flush is important, because that's when the parent_header actually has its effect
sys.stdout.flush()
sys.stdout.parent_header = save_parent
我希望能够获取单元格 In[1] 的 parent_header
,并将单元格 In[2] 的输出重定向到 In[1] 的输出。
例如:
获取 In[1] 的 parent_header
:
In[1]: t = sys.stdout.parent_header
接下来的代码将会运行,但是输出应该打印到Out[1](目前运行此代码时没有输出):
In [2]: with set_stdout_parent(t):
print 'FOO'
这应该会产生:
In[1]: t = sys.stdout.parent_header
Out[1]:'FOO'
display
和Javascript
函数将其附加到适当的DOM对象。 - tmthyjamesipykernel.iostream.OutStream
? - Michael H