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

Ensure Tabulator selection is recalculated after filtering #7712

Merged
merged 2 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
43 changes: 43 additions & 0 deletions panel/tests/widgets/test_tables.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import asyncio
import datetime as dt
import random
import string

from zoneinfo import ZoneInfo

Expand Down Expand Up @@ -2174,6 +2176,47 @@ def filter_c(df, value):
for col, values in model.source.data.items():
np.testing.assert_array_equal(values, expected[col])

def test_tabulator_function_filter_selection(document, comm):
# issue https://github.com/holoviz/panel/issues/7695
def generate_random_string(min_length=5, max_length=20):
length = random.randint(min_length, max_length)
return ''.join(random.choices(string.ascii_letters + string.digits, k=length))

def df_strings():
num_strings = 12
randomized_descr = [generate_random_string() for _ in range(num_strings)]
code = [f'{i:02d}' for i in range(num_strings)]
return pd.DataFrame(dict(code=code, descr=randomized_descr))

df = df_strings()
tbl = Tabulator(df)

descr_filter = TextInput(name='descr', value='')

def contains_filter(df, pattern=None):
if not pattern:
return df
return df[df.descr.str.contains(pattern, case=False)]

filter_fn = param.bind(contains_filter, pattern=descr_filter)

tbl.add_filter(filter_fn)

model = tbl.get_root()

tbl.selection = [0, 1, 2]

assert model.source.selected.indices == [0, 1, 2]

descr_filter.value = df.iloc[5, -1]

assert model.source.selected.indices == []

descr_filter.value = ""

assert model.source.selected.indices == [0, 1, 2]


def test_tabulator_function_mask_filter(document, comm):
df = makeMixedDataFrame()
table = Tabulator(df)
Expand Down
22 changes: 13 additions & 9 deletions panel/widgets/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1711,7 +1711,7 @@ def _update_cds(self, *events):
super()._update_cds(*events)
if self.pagination:
self._update_max_page()
self._update_selected()
self._update_selected()
self._update_style(recompute)
self._update_selectable()

Expand All @@ -1735,24 +1735,28 @@ def _clear_selection_remote_pagination(self, event):

def _update_selected(self, *events: param.parameterized.Event, indices=None):
kwargs = {}
if self.pagination == 'remote' and self.value is not None:
if self.value is not None:
# Compute integer indexes of the selected rows
# on the displayed page
index = self.value.iloc[self.selection].index
indices = []
for ind in index.values:
try:
iloc = self._processed.index.get_loc(ind)
self._validate_iloc(ind ,iloc)
self._validate_iloc(ind, iloc)
indices.append((ind, iloc))
except KeyError:
continue
nrows = self.page_size or self.initial_page_size
start = (self.page - 1) * nrows
end = start+nrows
p_range = self._processed.index[start:end]
kwargs['indices'] = [iloc - start for ind, iloc in indices
if ind in p_range]
if self.pagination == 'remote':
nrows = self.page_size or self.initial_page_size
start = (self.page - 1) * nrows
end = start+nrows
p_range = self._processed.index[start:end]
indices = [iloc - start for ind, iloc in indices
if ind in p_range]
else:
indices = [iloc for _, iloc in indices]
kwargs['indices'] = indices
super()._update_selected(*events, **kwargs)

def _update_column(self, column: str, array: TDataColumn) -> None:
Expand Down
Loading