Skip to content

Commit

Permalink
Improvements for Tabulator filter index handling (#2831)
Browse files Browse the repository at this point in the history
* Improvements for Tabulator filter index handling

* Fix flakes
  • Loading branch information
philippjfr authored Oct 19, 2021
1 parent fd57023 commit 44a40a7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
40 changes: 40 additions & 0 deletions panel/tests/widgets/test_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,46 @@ def test_tabulator_constant_scalar_filter_client_side(document, comm):
for col, values in model.source.data.items():
np.testing.assert_array_equal(values, expected[col])

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

model = table.get_root(document, comm)

table.filters = [{'field': 'index', 'type': '=', 'value': 2}]

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]')
}
for col, values in model.source.data.items():
np.testing.assert_array_equal(values, expected[col])

def test_tabulator_constant_scalar_filter_on_multi_index_client_side(document, comm):
df = makeMixedDataFrame()
table = Tabulator(df.set_index(['A', 'C']))

model = table.get_root(document, comm)

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

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

def test_tabulator_constant_list_filter(document, comm):
df = makeMixedDataFrame()
Expand Down
21 changes: 14 additions & 7 deletions panel/widgets/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,14 @@ def _manual_update(self, events, model, doc, root, parent, comm):
else:
self._update_columns(event, model)

def _filter_dataframe(self, df, ):
def _filter_dataframe(self, df):
"""
Filter the DataFrame.
Parameters
----------
df : DataFrame
The DataFrame to filter
query : dict
A dictionary containing all the query parameters
Returns
-------
Expand Down Expand Up @@ -269,7 +267,7 @@ def _filter_dataframe(self, df, ):
if len(self.indexes) == 1:
col = df.index
else:
col = df.index.get_level_values(self.indexes.index(col))
col = df.index.get_level_values(self.indexes.index(col_name))

# Sometimes Tabulator will provide a zero/single element list
if isinstance(val, list):
Expand Down Expand Up @@ -1213,15 +1211,24 @@ def _update_model(self, events, msg, root, model, doc, comm):
)
super()._update_model(events, msg, root, model, doc, comm)


def _get_filter_spec(self, column):
fspec = {}
if not self.header_filters or (isinstance(self.header_filters, dict) and
column.field not in self.header_filters):
return fspec
elif self.header_filters == True:
if column.field == 'index':
fspec['headerFilter'] = 'number'
if column.field in self.indexes:
if len(self.indexes) == 1:
col = self.value.index
else:
col = self.value.index.get_level_values(self.indexes.index(column.field))
if col.dtype.kind in 'uif':
fspec['headerFilter'] = 'number'
elif col.dtype.kind == 'b':
fspec['headerFilter'] = 'tickCross'
fspec['headerFilterParams'] = {'tristate': True, 'indeterminateValue': None}
else:
fspec['headerFilter'] = True
else:
fspec['headerFilter'] = True
return fspec
Expand Down

0 comments on commit 44a40a7

Please sign in to comment.