I want to use IPython notebook's IPython.display.display
from within a thread, as such:
def foo():
display(HTML('<div id="foobar">foobarbaz</div>'))
threading.Thread(target=foo).start()
IPython notebook spits out the following error:
Exception in thread Thread-17:
Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/lib/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
File "<ipython-input-47-921e56bac735>", line 2, in foo
display(HTML('<div id="foobar">foobarbaz</div>'))
File "/usr/local/lib/python3.5/dist-packages/IPython/core/display.py", line 171, in display
publish_display_data(data=format_dict, metadata=md_dict)
File "/usr/local/lib/python3.5/dist-packages/IPython/core/display.py", line 121, in publish_display_data
metadata=metadata,
File "/usr/local/lib/python3.5/dist-packages/ipykernel/zmqshell.py", line 111, in publish
for hook in self.thread_local.hooks:
AttributeError: '_thread._local' object has no attribute 'hooks'
I figure IPython's display is not thread safe? Is there a way to get it to point to the main thread instead of the local thread? Or perhaps a way to get the IPython.display.DisplayHandle
? Maybe the thing to do is to have a loop around a queue.Queue
on the main thread and display objects as they get added into that queue?
Thank you!
You can use an Output widget to display your HTML. This worked for me:
import ipywidgets as widgets
from IPython.display import display, HTML
import threading
out = widgets.Output()
def foo():
with out:
display(HTML('<div id="foobar">foobarbaz</div>'))
threading.Thread(target=foo).start()
out
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.