Skip to content

Commit b7aac7f

Browse files
committed
Use _repr_mimebundle_ in IPython 6.1 or later.
Fixes jupyter-widgets#1811
1 parent 04323bd commit b7aac7f

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

ipywidgets/widgets/tests/test_widget.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
"""Test Widget."""
55

6+
from IPython import version_info
67
from IPython.core.interactiveshell import InteractiveShell
78
from IPython.display import display
89
from IPython.utils.capture import capture_output
9-
1010
from ..widget import Widget
1111

1212

@@ -19,6 +19,10 @@ def test_no_widget_view():
1919
w = Widget()
2020
display(w)
2121

22-
assert cap.outputs == [], repr(cap.outputs)
22+
if version_info >= (6, 1):
23+
assert len(cap.outputs) == 1 and cap.outputs[0].data['text/plain'] == 'Widget()', repr(cap.outputs)
24+
else:
25+
assert cap.outputs == [], repr(cap.outputs)
26+
2327
assert cap.stdout == '', repr(cap.stdout)
2428
assert cap.stderr == '', repr(cap.stderr)

ipywidgets/widgets/widget.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import collections
1010
import sys
1111

12+
from IPython import version_info as ipython_version_info
1213
from IPython.core.getipython import get_ipython
1314
from ipykernel.comm import Comm
1415
from traitlets.utils.importstring import import_item
@@ -466,6 +467,7 @@ def close(self):
466467
self.comm.close()
467468
self.comm = None
468469
self._ipython_display_ = None
470+
self._repr_mimebundle_ = None
469471

470472
def send_state(self, key=None):
471473
"""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):
697699
"""Convert json values to objects."""
698700
return x
699701

700-
def _ipython_display_(self, **kwargs):
701-
"""Called when `IPython.display.display` is called on the widget."""
702+
def _repr_mimebundle_(self, **kwargs):
703+
"""Called when `IPython.display.display` is called."""
702704
if self._view_name is not None:
705+
# This callback now happens *before* the actual display call,
706+
# whereas before it happened *after* the display call.
707+
self._handle_displayed(**kwargs)
703708

704709
plaintext = repr(self)
705710
if len(plaintext) > 110:
@@ -717,9 +722,20 @@ def _ipython_display_(self, **kwargs):
717722
'model_id': self._model_id
718723
}
719724
}
720-
display(data, raw=True)
725+
return data
721726

722-
self._handle_displayed(**kwargs)
727+
def _ipython_display_(self, **kwargs):
728+
"""Called when `IPython.display.display` is called on a widget.
729+
730+
Note: if we are in IPython 6.1 or later, we return NotImplemented so
731+
that _repr_mimebundle_ is used directly.
732+
"""
733+
if ipython_version_info >= (6, 1):
734+
raise NotImplementedError
735+
736+
data = self._repr_mimebundle_(**kwargs)
737+
if data:
738+
display(data, raw=True)
723739

724740
def _send(self, msg, buffers=None):
725741
"""Sends a message to the model in the front-end."""

0 commit comments

Comments
 (0)