Skip to content

Commit

Permalink
Implement keywords filter for Tabulator (#3298)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Apr 1, 2022
1 parent 070b0c4 commit d7a6b66
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
41 changes: 41 additions & 0 deletions panel/tests/widgets/test_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,47 @@ def test_tabulator_constant_list_filter_client_side(document, comm):
np.testing.assert_array_equal(values, expected[col])


def test_tabulator_keywords_filter_client_side(document, comm):
df = makeMixedDataFrame()
table = Tabulator(df)

model = table.get_root(document, comm)

table.filters = [{'field': 'C', 'type': 'keywords', 'value': 'foo3 foo5'}]

expected = {
'index': np.array([2, 4]),
'A': np.array([2, 4]),
'B': np.array([0, 0]),
'C': np.array(['foo3', 'foo5']),
'D': np.array(['2009-01-05T00:00:00.000000000',
'2009-01-07T00:00:00.000000000'],
dtype='datetime64[ns]').astype(np.int64) / 10e5
}
for col, values in model.source.data.items():
np.testing.assert_array_equal(values, expected[col])


def test_tabulator_keywords_match_all_filter_client_side(document, comm):
df = makeMixedDataFrame()
table = Tabulator(df, header_filters={'C': {'type': 'input', 'func': 'keywords', 'matchAll': True}})

model = table.get_root(document, comm)

table.filters = [{'field': 'C', 'type': 'keywords', 'value': 'f oo 3'}]

expected = {
'index': np.array([2]),
'A': np.array([2]),
'B': np.array([0]),
'C': np.array(['foo3']),
'D': np.array(['2009-01-05T00:00:00.000000000'],
dtype='datetime64[ns]').astype(np.int64) / 10e5
}
for col, values in model.source.data.items():
np.testing.assert_array_equal(values, expected[col])


def test_tabulator_widget_scalar_filter(document, comm):
df = makeMixedDataFrame()
table = Tabulator(df)
Expand Down
13 changes: 13 additions & 0 deletions panel/widgets/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ def _filter_dataframe(self, df):
col_name = filt['field']
op = filt['type']
val = filt['value']
filt_def = getattr(self, 'header_filters', {}) or {}
if col_name in df.columns:
col = df[col_name]
elif col_name in self.indexes:
Expand Down Expand Up @@ -345,6 +346,18 @@ def _filter_dataframe(self, df):
filters.append(col.str.startsWith(val))
elif op == 'ends':
filters.append(col.str.endsWith(val))
elif op == 'keywords':
match_all = filt_def.get(col_name, {}).get('matchAll', False)
sep = filt_def.get(col_name, {}).get('separator', ' ')
matches = val.lower().split(sep)
if match_all:
for match in matches:
filters.append(col.str.lower().str.contains(match))
else:
filt = col.str.lower().str.contains(matches[0])
for match in matches[1:]:
filt |= col.str.lower().str.contains(match)
filters.append(filt)
elif op == 'regex':
raise ValueError("Regex filtering not supported.")
else:
Expand Down

0 comments on commit d7a6b66

Please sign in to comment.