Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use _repr_mimebundle_ and require IPython 6.1 or later. #2021

Merged
merged 3 commits into from
Jan 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions ipywidgets/widgets/interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,7 @@ def __init__(self, __interact_f, __options={}, **kwargs):
else:
for widget in self.kwargs_widgets:
widget.observe(self.update, names='value')

self.on_displayed(self.update)
self.update()

# Callback function
def update(self, *args):
Expand Down
12 changes: 6 additions & 6 deletions ipywidgets/widgets/tests/test_interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,8 @@ def test_call_decorated_on_trait_change(clear_display):
def foo(a='default'):
d['a'] = a
return a
assert len(displayed) == 1
w = displayed[0].children[0]
assert len(displayed) == 2 # display the result and the interact
w = displayed[1].children[0]
check_widget(w,
cls=widgets.Text,
value='default',
Expand All @@ -487,7 +487,7 @@ def foo(a='default'):
with patch.object(interaction, 'display', record_display):
w.value = 'called'
assert d['a'] == 'called'
assert len(displayed) == 2
assert len(displayed) == 3
assert w.value == displayed[-1]

def test_call_decorated_kwargs_on_trait_change(clear_display):
Expand All @@ -498,8 +498,8 @@ def test_call_decorated_kwargs_on_trait_change(clear_display):
def foo(a='default'):
d['a'] = a
return a
assert len(displayed) == 1
w = displayed[0].children[0]
assert len(displayed) == 2 # display the result and the interact
w = displayed[1].children[0]
check_widget(w,
cls=widgets.Text,
value='kwarg',
Expand All @@ -513,7 +513,7 @@ def foo(a='default'):
with patch.object(interaction, 'display', record_display):
w.value = 'called'
assert d['a'] == 'called'
assert len(displayed) == 2
assert len(displayed) == 3
assert w.value == displayed[-1]


Expand Down
4 changes: 2 additions & 2 deletions ipywidgets/widgets/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ def close(self, *args, **kwargs):
def setup_test_comm():
_widget_attrs['_comm_default'] = getattr(Widget, '_comm_default', undefined)
Widget._comm_default = lambda self: DummyComm()
_widget_attrs['_ipython_display_'] = Widget._ipython_display_
_widget_attrs['_repr_mimebundle_'] = Widget._repr_mimebundle_
def raise_not_implemented(*args, **kwargs):
raise NotImplementedError()
Widget._ipython_display_ = raise_not_implemented
Widget._repr_mimebundle_ = raise_not_implemented

def teardown_test_comm():
for attr, value in _widget_attrs.items():
Expand Down
33 changes: 4 additions & 29 deletions ipywidgets/widgets/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from traitlets import (
HasTraits, Unicode, Dict, Instance, List, Int, Set, Bytes, observe, default, Container,
Undefined)
from IPython.display import display
from json import loads as jsonloads, dumps as jsondumps

from base64 import standard_b64encode
Expand Down Expand Up @@ -381,7 +380,6 @@ def _default_keys(self):
_property_lock = Dict()
_holding_sync = False
_states_to_send = Set()
_display_callbacks = Instance(CallbackDispatcher, ())
_msg_callbacks = Instance(CallbackDispatcher, ())

#-------------------------------------------------------------------------
Expand Down Expand Up @@ -449,7 +447,7 @@ def close(self):
Widget.widgets.pop(self.model_id, None)
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 @@ -549,21 +547,6 @@ def on_msg(self, callback, remove=False):
True if the callback should be unregistered."""
self._msg_callbacks.register_callback(callback, remove=remove)

def on_displayed(self, callback, remove=False):
"""(Un)Register a widget displayed callback.

Parameters
----------
callback: method handler
Must have a signature of::

callback(widget, **kwargs)

kwargs from display are passed through without modification.
remove: bool
True if the callback should be unregistered."""
self._display_callbacks.register_callback(callback, remove=remove)

def add_traits(self, **traits):
"""Dynamically add trait attributes to the Widget."""
super().add_traits(**traits)
Expand Down Expand Up @@ -671,10 +654,6 @@ def _handle_custom_msg(self, content, buffers):
"""Called when a custom msg is received."""
self._msg_callbacks(self, content, buffers)

def _handle_displayed(self, **kwargs):
"""Called when a view has been displayed for this widget instance"""
self._display_callbacks(self, **kwargs)

@staticmethod
def _trait_to_json(x, self):
"""Convert a trait value to json."""
Expand All @@ -685,9 +664,8 @@ 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."""
plaintext = repr(self)
if len(plaintext) > 110:
plaintext = plaintext[:110] + '…'
Expand All @@ -705,10 +683,7 @@ def _ipython_display_(self, **kwargs):
'version_minor': 0,
'model_id': self._model_id
}
display(data, raw=True)

if self._view_name is not None:
self._handle_displayed(**kwargs)
return data

def _send(self, msg, buffers=None):
"""Sends a message to the model in the front-end."""
Expand Down
6 changes: 0 additions & 6 deletions ipywidgets/widgets/widget_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,6 @@ class Box(DOMWidget, CoreWidget):
def __init__(self, children=(), **kwargs):
kwargs['children'] = children
super().__init__(**kwargs)
self.on_displayed(Box._fire_children_displayed)

def _fire_children_displayed(self):
for child in self.children:
child._handle_displayed()


@register
@doc_subst(_doc_snippets)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
setuptools_args = {}
install_requires = setuptools_args['install_requires'] = [
'ipykernel>=4.5.1',
'ipython>=6.1.0', # to use _repr_mimebundle
'traitlets>=4.3.1',
# Requiring nbformat to specify bugfix version which is not required by
# notebook.
Expand All @@ -120,7 +121,6 @@
]

extras_require = setuptools_args['extras_require'] = {
'': ['ipython>=4.0.0'],
'test': ['pytest>=3.6.0', 'pytest-cov'],
}

Expand Down