From 6fb07845d143f02165c11405ed5bec20391b09c5 Mon Sep 17 00:00:00 2001 From: "Maarten A. Breddels" Date: Tue, 23 Aug 2022 16:22:00 +0200 Subject: [PATCH] fix: support ipywidgets 8 --- react_ipywidgets/core.py | 18 +++++++++++++++++- react_ipywidgets/test_core.py | 11 ++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/react_ipywidgets/core.py b/react_ipywidgets/core.py index 42174c8..3be9b04 100644 --- a/react_ipywidgets/core.py +++ b/react_ipywidgets/core.py @@ -43,6 +43,21 @@ __version__ = _version.__version__ + +ipywidget_version_major = int(widgets.__version__.split(".")[0]) + + +class _classproperty_widget_fix(object): + def __get__(self, owner_self, owner_cls): + assert owner_self is None + return owner_cls._active_widgets + + +if ipywidget_version_major >= 8: + if not hasattr(widgets.Widget, "widgets"): + widgets.Widget.widgets = _classproperty_widget_fix() + + _last_rc = None # used for testing local = threading.local() T = TypeVar("T") @@ -1642,7 +1657,8 @@ def check_view_count(change): MIME_WIDGETS: {"version_major": 2, "version_minor": 0, "model_id": widget._model_id}, } IPython.display.display(data, raw=True) - widget._handle_displayed() + if ipywidget_version_major < 8: + widget._handle_displayed() def make(el: Element, handle_error: bool = True): diff --git a/react_ipywidgets/test_core.py b/react_ipywidgets/test_core.py index 956c5a1..d2acb98 100644 --- a/react_ipywidgets/test_core.py +++ b/react_ipywidgets/test_core.py @@ -17,6 +17,7 @@ from . import bqplot from . import ipyvuetify as v from . import ipywidgets as w +from .core import ipywidget_version_major T = TypeVar("T") @@ -26,7 +27,7 @@ def first(container: List[T]) -> T: def clear(): - widgets.Widget.widgets = {} + widgets.Widget.widgets.clear() def count(): @@ -674,8 +675,12 @@ def Buttons(checkbox): assert vbox.children[0] is not vbox.children[1] assert vbox.children[0].description == "Button 0" assert vbox.children[1].description == "Button 1" - assert vbox.children[0].tooltip == "" - assert vbox.children[1].tooltip == "" + if ipywidget_version_major >= 8: + assert vbox.children[0].tooltip is None + assert vbox.children[1].tooltip is None + else: + assert vbox.children[0].tooltip == "" + assert vbox.children[1].tooltip == "" rc.close() checkbox.style.close() checkbox.layout.close()