From b7aac7ffc32110726667aeb3fb877db119a402b6 Mon Sep 17 00:00:00 2001 From: Jason Grout Date: Tue, 27 Mar 2018 14:10:28 -0700 Subject: [PATCH] Use _repr_mimebundle_ in IPython 6.1 or later. Fixes #1811 --- ipywidgets/widgets/tests/test_widget.py | 8 ++++++-- ipywidgets/widgets/widget.py | 24 ++++++++++++++++++++---- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/ipywidgets/widgets/tests/test_widget.py b/ipywidgets/widgets/tests/test_widget.py index b19c79a0d25..9193c7d46e9 100644 --- a/ipywidgets/widgets/tests/test_widget.py +++ b/ipywidgets/widgets/tests/test_widget.py @@ -3,10 +3,10 @@ """Test Widget.""" +from IPython import version_info from IPython.core.interactiveshell import InteractiveShell from IPython.display import display from IPython.utils.capture import capture_output - from ..widget import Widget @@ -19,6 +19,10 @@ def test_no_widget_view(): w = Widget() display(w) - assert cap.outputs == [], repr(cap.outputs) + if version_info >= (6, 1): + assert len(cap.outputs) == 1 and cap.outputs[0].data['text/plain'] == 'Widget()', repr(cap.outputs) + else: + assert cap.outputs == [], repr(cap.outputs) + assert cap.stdout == '', repr(cap.stdout) assert cap.stderr == '', repr(cap.stderr) diff --git a/ipywidgets/widgets/widget.py b/ipywidgets/widgets/widget.py index b504793bb32..feb1de4166f 100644 --- a/ipywidgets/widgets/widget.py +++ b/ipywidgets/widgets/widget.py @@ -9,6 +9,7 @@ import collections import sys +from IPython import version_info as ipython_version_info from IPython.core.getipython import get_ipython from ipykernel.comm import Comm from traitlets.utils.importstring import import_item @@ -466,6 +467,7 @@ def close(self): self.comm.close() self.comm = None self._ipython_display_ = None + self._repr_mimebundle_ = None def send_state(self, key=None): """Sends the widget state, or a piece of it, to the front-end, if it exists. @@ -697,9 +699,12 @@ def _trait_from_json(x, self): """Convert json values to objects.""" return x - def _ipython_display_(self, **kwargs): - """Called when `IPython.display.display` is called on the widget.""" + def _repr_mimebundle_(self, **kwargs): + """Called when `IPython.display.display` is called.""" if self._view_name is not None: + # This callback now happens *before* the actual display call, + # whereas before it happened *after* the display call. + self._handle_displayed(**kwargs) plaintext = repr(self) if len(plaintext) > 110: @@ -717,9 +722,20 @@ def _ipython_display_(self, **kwargs): 'model_id': self._model_id } } - display(data, raw=True) + return data - self._handle_displayed(**kwargs) + def _ipython_display_(self, **kwargs): + """Called when `IPython.display.display` is called on a widget. + + Note: if we are in IPython 6.1 or later, we return NotImplemented so + that _repr_mimebundle_ is used directly. + """ + if ipython_version_info >= (6, 1): + raise NotImplementedError + + data = self._repr_mimebundle_(**kwargs) + if data: + display(data, raw=True) def _send(self, msg, buffers=None): """Sends a message to the model in the front-end."""