Skip to content

Commit

Permalink
modify outputs to use comm
Browse files Browse the repository at this point in the history
This modifies the outputs widget to use comm rather than calling
ipython directly.  This fixes jupyter-widgets#2953 and Calysto/metakernel#217
  • Loading branch information
Joseph C Wang committed Aug 28, 2020
1 parent 6be18d9 commit 40c73a8
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 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 @@ -107,24 +107,35 @@ def inner(*args, **kwargs):
def __enter__(self):
"""Called upon entering output widget context manager."""
self._flush()
ip = get_ipython()
if ip and hasattr(ip, 'kernel') and hasattr(ip.kernel, '_parent_header'):
self.msg_id = ip.kernel._parent_header['header']['msg_id']
if self.comm is not None and self.comm.kernel is not None and \
hasattr(self.comm.kernel, '_parent_header'):
self.msg_id = self.comm.kernel._parent_header['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

0 comments on commit 40c73a8

Please sign in to comment.