Skip to content

modify outputs to use comm #2954

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions ipywidgets/widgets/widget_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from IPython.core.interactiveshell import InteractiveShell
from IPython.display import clear_output
from IPython import get_ipython

import traceback

@register
class Output(DOMWidget):
Expand Down Expand Up @@ -108,30 +108,48 @@ def __enter__(self):
"""Called upon entering output widget context manager."""
self._flush()
ip = get_ipython()
kernel = None
if ip and getattr(ip, "kernel", None) is not None:
if hasattr(ip.kernel, "get_parent"):
parent = ip.kernel.get_parent()
elif hasattr(ip.kernel, "_parent_header"):
kernel = ip.kernel
elif self.comm is not None and self.comm.kernel is not None:
kernel = self.comm.kernel

if kernel:
parent = None
if hasattr(kernel, "get_parent"):
parent = kernel.get_parent()
elif hasattr(kernel, "_parent_header"):
# ipykernel < 6: kernel._parent_header is the parent *request*
parent = ip.kernel._parent_header
parent = kernel._parent_header

if parent and parent.get("header"):
self.msg_id = parent["header"]["msg_id"]
self.__counter += 1

def __exit__(self, etype, evalue, tb):
"""Called upon exiting output widget context manager."""
ip = get_ipython()
kernel = None
if etype is not None:
ip = get_ipython()
if ip:
kernel = ip
ip.showtraceback((etype, evalue, tb), tb_offset=0)
elif self.comm is not None and self.comm.kernel is not None:
kernel = self.comm.kernel
kernel.send_response(kernel.iopub_socket,
u'error',
{
u'traceback': ["".join(traceback.format_exception(etype, evalue, tb))],
u'evalue': repr(evalue.args),
u'ename': etype.__name__
})
self._flush()
self.__counter -= 1
if self.__counter == 0:
self.msg_id = ''
# suppress exceptions when in a kernel, since they are shown above,
# suppress exceptions when in IPython, since they are shown above,
# otherwise let someone else handle it
return True if ip else None
return True if kernel else None

def _flush(self):
"""Flush stdout and stderr buffers."""
Expand Down