Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Jun 25, 2021
1 parent 7a24c91 commit 64b67fe
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
48 changes: 48 additions & 0 deletions panel/tests/widgets/test_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,15 @@ def test_tabulator_frozen_rows(document, comm):
assert model.frozen_rows == [1, 3]


def test_tabulator_selectable_rows(document, comm):
df = makeMixedDataFrame()
table = Tabulator(df, selectable_rows=lambda df: list(df[df.A>2].index.values))

model = table.get_root(document, comm)

assert model.selectable_rows == [3, 4]


def test_tabulator_pagination(document, comm):
df = makeMixedDataFrame()
table = Tabulator(df, pagination='remote', page_size=2)
Expand Down Expand Up @@ -376,6 +385,23 @@ def test_tabulator_pagination_selection(document, comm):
assert model.source.selected.indices == [0, 1]


def test_tabulator_pagination_selectable_rows(document, comm):
df = makeMixedDataFrame()
table = Tabulator(
df, pagination='remote', page_size=3,
selectable_rows=lambda df: list(df.index.values[::2])
)

model = table.get_root(document, comm)

print(table._processed)
assert model.selectable_rows == [0, 2]

table.page = 2

assert model.selectable_rows == [3]


def test_tabulator_styling(document, comm):
df = makeMixedDataFrame()
table = Tabulator(df)
Expand Down Expand Up @@ -856,6 +882,28 @@ def test_tabulator_stream_dataframe_with_filter(document, comm):
np.testing.assert_array_equal(values, expected[col])


def test_tabulator_stream_dataframe_selectable_rows(document, comm):
df = makeMixedDataFrame()
table = Tabulator(df, selectable_rows=lambda df: list(range(0, len(df), 2)))

model = table.get_root(document, comm)

assert model.selectable_rows == [0, 2, 4]

stream_value = pd.DataFrame({
'A': [5, 6],
'B': [1, 0],
'C': ['foo6', 'foo7'],
'D': [dt.datetime(2009, 1, 8), dt.datetime(2009, 1, 9)]
})

table.stream(stream_value)

print(len(table._processed))

assert model.selectable_rows == [0, 2, 4, 6]


def test_tabulator_dataframe_replace_data(document, comm):
df = makeMixedDataFrame()
table = Tabulator(df)
Expand Down
15 changes: 14 additions & 1 deletion panel/widgets/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,10 @@ def _get_style_data(self):
if self.value is None:
return {}
df = self._processed
if self.pagination == 'remote':
nrows = self.page_size
start = (self.page-1)*nrows
df = df.iloc[start:(start+nrows)]
styler = df.style
styler._todo = self.style._todo
styler._compute()
Expand All @@ -855,7 +859,12 @@ def _get_style_data(self):
def _get_selectable(self):
if self.value is None or self.selectable_rows is None:
return None
return self.selectable_rows(self._processed)
df = self._processed
if self.pagination == 'remote':
nrows = self.page_size
start = (self.page-1)*nrows
df = df.iloc[start:(start+nrows)]
return self.selectable_rows(df)

def _update_style(self):
styles = self._get_style_data()
Expand All @@ -873,6 +882,7 @@ def _stream(self, stream, rollover=None, follow=True):
return
super()._stream(stream, rollover)
self._update_style()
self._update_selectable()

def stream(self, stream_value, rollover=None, reset_index=True, follow=True):
for ref, (model, _) in self._models.items():
Expand All @@ -899,6 +909,7 @@ def _patch(self, patch):
if not patch:
return
super()._patch(patch)
self._update_selectable()
if self.pagination == 'remote':
self._update_style()

Expand Down Expand Up @@ -1062,6 +1073,8 @@ def _config_columns(self, column_objs):
col_dict['formatter'] = formatter.pop('type')
col_dict['formatterParams'] = formatter
editor = self.editors.get(column.field)
if column.field in self.editors and editor is None:
col_dict['editable'] = False
if isinstance(editor, str):
col_dict['editor'] = editor
elif isinstance(editor, dict):
Expand Down

0 comments on commit 64b67fe

Please sign in to comment.