Skip to content

Commit

Permalink
Use _repr_mimebundle_ in IPython 6.1 or later.
Browse files Browse the repository at this point in the history
  • Loading branch information
jasongrout committed Mar 27, 2018
1 parent 04323bd commit fc0c914
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions ipywidgets/widgets/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,6 +20,7 @@
from IPython.display import display
from json import loads as jsonloads, dumps as jsondumps


from base64 import standard_b64decode, standard_b64encode

from .._version import __protocol_version__, __jupyter_widgets_base_version__
Expand Down Expand Up @@ -466,6 +468,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.
Expand Down Expand Up @@ -697,9 +700,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:
Expand All @@ -717,9 +723,21 @@ 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[0] > 6
or (ipython_version_info[0] == 6 and ipython_version_info[1] >= 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."""
Expand Down

0 comments on commit fc0c914

Please sign in to comment.