Skip to content

Commit

Permalink
Fix widget.delete() by deleting all views on the widget manager
Browse files Browse the repository at this point in the history
  • Loading branch information
sc1f committed Nov 24, 2020
1 parent 9d84a4f commit e0da24f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
6 changes: 3 additions & 3 deletions python/perspective/perspective/table/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,9 @@ def delete(self):
"""Delete the :class:`~perspective.View` and clean up all associated
callbacks.
This method must be called to clean up resources used by the
:class:`~perspective.View`, as it will last for the lifetime of the
underlying :class:`~perspective.Table` otherwise.
This method must be called to clean up callbacks used by the
:class:`~perspective.View`, as well as allow for deletion of the
underlying :class:`~perspective.Table`.
Examples:
>>> table = perspective.Table(data)
Expand Down
25 changes: 22 additions & 3 deletions python/perspective/perspective/tests/widget/test_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# This file is part of the Perspective library, distributed under the terms of
# the Apache License 2.0. The full license can be found in the LICENSE file.
#

import six
import numpy as np
from datetime import date, datetime
from functools import partial
Expand All @@ -15,12 +15,11 @@


def mock_post(self, msg, msg_id=None, assert_msg=None):
'''Mock the widget's `post()` method so we can introspect the contents.'''
"""Mock the widget's `post()` method so we can introspect the contents."""
assert msg == assert_msg


class TestWidget:

def test_widget(self):
data = {"a": np.arange(0, 50)}
widget = PerspectiveWidget(data, plugin="x_bar")
Expand Down Expand Up @@ -278,3 +277,23 @@ def test_widget_delete(self):
widget.post = MethodType(mocked_post, widget)
widget.delete()
assert widget.table is None

def test_widget_delete_with_view(self):
data = {"a": np.arange(0, 50)}
widget = PerspectiveWidget(data)

# create a view on the manager
table_name, table = list(widget.manager._tables.items())[0]
make_view_message = {"id": 1, "table_name": table_name, "view_name": "view1", "cmd": "view", "config": {"row_pivots": ["a"]}}
widget.manager._process(make_view_message, lambda x: True)

assert len(widget.manager._views) == 1

mocked_post = partial(mock_post, assert_msg={
"cmd": "delete"
})

widget.post = MethodType(mocked_post, widget)
widget.delete()

assert widget.table is None
8 changes: 8 additions & 0 deletions python/perspective/perspective/viewer/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,14 @@ def delete(self, delete_table=True):
deleted. Defaults to True.
"""
if delete_table:
# Delete all created views on the widget's manager instance
for view in six.itervalues(self.manager._views):
view.delete()

# Reset view cache
self.manager._views = {}

# Delete table
self.table.delete()
self.manager._tables.pop(self.table_name)
self.table_name = None
Expand Down

0 comments on commit e0da24f

Please sign in to comment.