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 styling is correctly applied on multi-index #7075

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 26 additions & 0 deletions panel/tests/widgets/test_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2247,6 +2247,32 @@ def test_tabulator_styling_empty_dataframe(document, comm):
}
}

def test_tabulator_style_multi_index_dataframe(document, comm):
# See https://github.com/holoviz/panel/issues/6151
arrays = [['A', 'A', 'B', 'B'], [1, 2, 1, 2]]
index = pd.MultiIndex.from_arrays(arrays, names=('Letters', 'Numbers'))
df = pd.DataFrame({
'Values': [1, 2, 3, 4],
'X': [10, 20, 30, 40],
'Y': [100, 200, 300, 400],
'Z': [1000, 2000, 3000, 4000]
}, index=index)

def color_func(vals):
return ["background-color: #ff0000;" for v in vals]

tabulator = Tabulator(df, width=500, height=300)
tabulator.style.apply(color_func, subset = ['X'])

model = tabulator.get_root(document, comm)

assert model.cell_styles['data'] == {
0: {4: [('background-color', '#ff0000')]},
1: {4: [('background-color', '#ff0000')]},
2: {4: [('background-color', '#ff0000')]},
3: {4: [('background-color', '#ff0000')]}
}


@mpl_available
def test_tabulator_style_background_gradient_with_frozen_columns(document, comm):
Expand Down
5 changes: 4 additions & 1 deletion panel/widgets/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1436,7 +1436,10 @@ def _get_style_data(self, recompute=True):
styler = self._computed_styler
if styler is None:
return {}
offset = 1 + len(self.indexes) + int(self.selectable in ('checkbox', 'checkbox-single')) + int(bool(self.row_content))

# Compute offsets (not that multi-indexes are reset so don't require an offset)
offset = 1 + int(len(self.indexes) == 1) + int(self.selectable in ('checkbox', 'checkbox-single')) + int(bool(self.row_content))

if self.pagination == 'remote':
page_size = self.page_size or self.initial_page_size
start = (self.page - 1) * page_size
Expand Down
Loading