From edcfa4333d20cbc3e5767713b2e26c16fd1c7c9e Mon Sep 17 00:00:00 2001 From: Jeremy Tuloup Date: Fri, 26 Mar 2021 17:49:09 +0100 Subject: [PATCH 1/3] Fix use of _active_widgets in Widget.close_all() --- ipywidgets/widgets/widget.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ipywidgets/widgets/widget.py b/ipywidgets/widgets/widget.py index 4b495b9c50..72310cd0dd 100644 --- a/ipywidgets/widgets/widget.py +++ b/ipywidgets/widgets/widget.py @@ -271,7 +271,7 @@ class Widget(LoggingHasTraits): @classmethod def close_all(cls): - for widget in list(cls.widgets.values()): + for widget in list(cls._active_widgets.values()): widget.close() From 8f946fa1cf749eb5ca0f60b8db4423b628edef42 Mon Sep 17 00:00:00 2001 From: Jeremy Tuloup Date: Fri, 26 Mar 2021 17:54:35 +0100 Subject: [PATCH 2/3] Add test for Widget.close_all() --- ipywidgets/widgets/tests/test_widget.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ipywidgets/widgets/tests/test_widget.py b/ipywidgets/widgets/tests/test_widget.py index c6f2f3cd66..70bcd20a92 100644 --- a/ipywidgets/widgets/tests/test_widget.py +++ b/ipywidgets/widgets/tests/test_widget.py @@ -43,3 +43,13 @@ def test_widget_view(): assert 'application/vnd.jupyter.widget-view+json' in mime_bundle, "widget should have have a view" assert cap.stdout == '', repr(cap.stdout) assert cap.stderr == '', repr(cap.stderr) + + +def test_close_all(): + # create a couple of widgets + widgets = [Button() for i in range(10)] + + # close all the widgets + Widget.close_all() + + assert len(Widget._active_widgets) == 0, "active widgets should be cleared" From 3f8ae834f3b6da61ab3cd696f070ddcde01ac615 Mon Sep 17 00:00:00 2001 From: Jeremy Tuloup Date: Fri, 26 Mar 2021 17:57:37 +0100 Subject: [PATCH 3/3] Add initial assert for the Widget.close_all test --- ipywidgets/widgets/tests/test_widget.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ipywidgets/widgets/tests/test_widget.py b/ipywidgets/widgets/tests/test_widget.py index 70bcd20a92..7a70b82c3b 100644 --- a/ipywidgets/widgets/tests/test_widget.py +++ b/ipywidgets/widgets/tests/test_widget.py @@ -49,6 +49,8 @@ def test_close_all(): # create a couple of widgets widgets = [Button() for i in range(10)] + assert len(Widget._active_widgets) > 0, "expect active widgets" + # close all the widgets Widget.close_all()