Skip to content

Commit

Permalink
Add support for rendering pandas styler objects (#3152)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr authored Jan 30, 2022
1 parent 39ea2f5 commit 8503e5a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 4 additions & 1 deletion panel/pane/markup.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def applies(cls, obj):
module = getattr(obj, '__module__', '')
name = type(obj).__name__
if (any(m in module for m in ('pandas', 'dask', 'streamz')) and
name in ('DataFrame', 'Series', 'Random', 'DataFrames', 'Seriess')):
name in ('DataFrame', 'Series', 'Random', 'DataFrames', 'Seriess', 'Styler')):
return 0.3
else:
return False
Expand Down Expand Up @@ -215,6 +215,9 @@ def _get_properties(self):
if hasattr(df, 'to_html'):
if 'dask' in module:
html = df.to_html(max_rows=self.max_rows).replace('border="1"', '')
elif 'style' in module:
classes = ' '.join(self.classes)
html = df.to_html(table_attributes=f'class="{classes}"')
else:
kwargs = {p: getattr(self, p) for p in self._rerender_params
if p not in DivPaneBase.param and p != '_object'}
Expand Down
9 changes: 8 additions & 1 deletion panel/widgets/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,12 @@ class Tabulator(BaseTable):
}

def __init__(self, value=None, **params):
import pandas.io.formats.style
if isinstance(value, pandas.io.formats.style.Styler):
style = value
value = value.data
else:
style = None
configuration = params.pop('configuration', {})
self.style = None
self._computed_styler = None
Expand All @@ -924,6 +930,8 @@ def __init__(self, value=None, **params):
super().__init__(value=value, **params)
self._configuration = configuration
self.param.watch(self._update_children, self._content_params)
if style is not None:
self.style._todo = style._todo

def _validate(self, *events):
super()._validate(*events)
Expand Down Expand Up @@ -1533,4 +1541,3 @@ def on_button_click(self, callback, column=None):
if column not in self._on_click_callbacks:
self._on_click_callbacks[column] = []
self._on_click_callbacks[column].append(callback)

0 comments on commit 8503e5a

Please sign in to comment.