Skip to content

Commit c4dbc11

Browse files
committed
Use _repr_mimebundle_ in IPython 6.1 or later.
Fixes #1811
1 parent 04323bd commit c4dbc11

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

ipywidgets/widgets/widget.py

Lines changed: 21 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,21 @@ 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[0] > 6
734+
or (ipython_version_info[0] == 6 and ipython_version_info[1] >= 1)):
735+
raise NotImplementedError
736+
737+
data = self._repr_mimebundle_(**kwargs)
738+
if data:
739+
display(data, raw=True)
723740

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

0 commit comments

Comments
 (0)