Skip to content

Commit a1d2011

Browse files
authored
ipywidgets 8 compatibility (#6912)
1 parent 58a5a3c commit a1d2011

File tree

6 files changed

+37
-13
lines changed

6 files changed

+37
-13
lines changed

continuous_integration/environment-3.10.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ dependencies:
1515
- filesystem-spec # overridden by git tip below
1616
- h5py
1717
- ipykernel
18-
- ipywidgets <8 # FIXME https://github.com/dask/distributed/issues/6909
18+
- ipywidgets
1919
- jinja2
2020
- locket >=1.0
2121
- msgpack-python

continuous_integration/environment-3.8.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ dependencies:
1616
- filesystem-spec
1717
- h5py
1818
- ipykernel
19-
- ipywidgets <8 # FIXME https://github.com/dask/distributed/issues/6909
19+
- ipywidgets
2020
- jinja2
2121
- locket >=1.0
2222
- msgpack-python

continuous_integration/environment-3.9.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ dependencies:
1616
- filesystem-spec
1717
- h5py
1818
- ipykernel
19-
- ipywidgets <8 # FIXME https://github.com/dask/distributed/issues/6909
19+
- ipywidgets
2020
- jinja2
2121
- locket >=1.0
2222
- lz4 >=0.23.1 # Only tested here

distributed/deploy/cluster.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from inspect import isawaitable
1010
from typing import Any
1111

12+
from packaging.version import parse as parse_version
1213
from tornado.ioloop import IOLoop, PeriodicCallback
1314

1415
import dask.config
@@ -493,14 +494,28 @@ def _repr_html_(self, cluster_status=None):
493494
)
494495

495496
def _ipython_display_(self, **kwargs):
497+
"""Display the cluster rich IPython repr"""
498+
# Note: it would be simpler to just implement _repr_mimebundle_,
499+
# but we cannot do that until we drop ipywidgets 7 support, as
500+
# it does not provide a public way to get the mimebundle for a
501+
# widget. So instead we fall back on the more customizable _ipython_display_
502+
# and display as a side-effect.
503+
from IPython.display import display
504+
496505
widget = self._widget()
497-
if widget is not None:
498-
return widget._ipython_display_(**kwargs)
506+
if widget:
507+
import ipywidgets
508+
509+
if parse_version(ipywidgets.__version__) >= parse_version("8.0.0"):
510+
mimebundle = widget._repr_mimebundle_(**kwargs) or {}
511+
mimebundle["text/plain"] = repr(self)
512+
mimebundle["text/html"] = self._repr_html_()
513+
display(mimebundle, raw=True)
514+
else:
515+
display(widget, **kwargs)
499516
else:
500-
from IPython.display import display
501-
502-
data = {"text/plain": repr(self), "text/html": self._repr_html_()}
503-
display(data, raw=True)
517+
mimebundle = {"text/plain": repr(self), "text/html": self._repr_html_()}
518+
display(mimebundle, raw=True)
504519

505520
def __enter__(self):
506521
return self.sync(self.__aenter__)

distributed/diagnostics/progressbar.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,9 @@ def __init__(
198198

199199
def _ipython_display_(self, **kwargs):
200200
IOLoop.current().add_callback(self.listen)
201-
return self.widget._ipython_display_(**kwargs)
201+
from IPython.display import display
202+
203+
display(self.widget, **kwargs)
202204

203205
def _draw_stop(self, remaining, status, exception=None, **kwargs):
204206
if status == "error":
@@ -379,7 +381,9 @@ def keyfunc(kv):
379381

380382
def _ipython_display_(self, **kwargs):
381383
IOLoop.current().add_callback(self.listen)
382-
return self.widget._ipython_display_(**kwargs)
384+
from IPython.display import display
385+
386+
display(self.widget, **kwargs)
383387

384388
def _draw_stop(self, remaining, status, exception=None, key=None, **kwargs):
385389
for k, v in remaining.items():

distributed/diagnostics/tests/test_widgets.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from __future__ import annotations
22

33
import pytest
4+
from packaging.version import parse as parse_version
45

5-
pytest.importorskip("ipywidgets")
6+
ipywidgets = pytest.importorskip("ipywidgets")
67

78
from ipykernel.comm import Comm
89
from ipywidgets import Widget
@@ -38,7 +39,11 @@ def close(self, *args, **kwargs):
3839
def setup():
3940
_widget_attrs["_comm_default"] = getattr(Widget, "_comm_default", undefined)
4041
Widget._comm_default = lambda self: DummyComm()
41-
_widget_attrs["_ipython_display_"] = Widget._ipython_display_
42+
if parse_version(ipywidgets.__version__) >= parse_version("8.0.0"):
43+
display_attr = "_repr_mimebundle_"
44+
else:
45+
display_attr = "_ipython_display_"
46+
_widget_attrs[display_attr] = getattr(Widget, display_attr)
4247

4348
def raise_not_implemented(*args, **kwargs):
4449
raise NotImplementedError()

0 commit comments

Comments
 (0)