9
9
import collections
10
10
import sys
11
11
12
+ from IPython import version_info as ipython_version_info
12
13
from IPython .core .getipython import get_ipython
13
14
from ipykernel .comm import Comm
14
15
from traitlets .utils .importstring import import_item
@@ -466,6 +467,7 @@ def close(self):
466
467
self .comm .close ()
467
468
self .comm = None
468
469
self ._ipython_display_ = None
470
+ self ._repr_mimebundle_ = None
469
471
470
472
def send_state (self , key = None ):
471
473
"""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):
697
699
"""Convert json values to objects."""
698
700
return x
699
701
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."""
702
704
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 )
703
708
704
709
plaintext = repr (self )
705
710
if len (plaintext ) > 110 :
@@ -717,9 +722,20 @@ def _ipython_display_(self, **kwargs):
717
722
'model_id' : self ._model_id
718
723
}
719
724
}
720
- display ( data , raw = True )
725
+ return data
721
726
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 )
723
739
724
740
def _send (self , msg , buffers = None ):
725
741
"""Sends a message to the model in the front-end."""
0 commit comments