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

Add support for Tabulator.title_formatters #5421

Merged
merged 1 commit into from
Aug 21, 2023
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
13 changes: 11 additions & 2 deletions examples/reference/widgets/Tabulator.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"* **``text_align``** (``dict`` or ``str``): A mapping from column name to alignment or a fixed column alignment, which should be one of `'left'`, `'center'`, `'right'`.\n",
"* **`theme`** (``str``, `default='simple'`): The CSS theme to apply (note that changing the theme will restyle all tables on the page), which should be one of `'default'`, `'site'`, `'simple'`, `'midnight'`, `'modern'`, `'bootstrap'`, `'bootstrap4'`, `'materialize'`, `'bulma'`, `'semantic-ui'`, or `'fast'`.\n",
"* **`theme_classes`** (`list[str]`): List of extra CSS classes to apply to the Tabulator element to customize the theme.\n",
"* **``title_formatters``** (``dict``): A dictionary mapping from column name to a *Tabulator* formatter specification.\n",
"* **``titles``** (``dict``): A mapping from column name to a title to override the name with.\n",
"* **``value``** (``pd.DataFrame``): The pandas DataFrame to display and edit\n",
"* **``widths``** (``dict``): A dictionary mapping from column name to column width in the rendered table.\n",
Expand Down Expand Up @@ -176,7 +177,15 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The list of valid *Tabulator* formatters can be found in the [Tabulator documentation](https://tabulator.info/docs/5.4/format#format-builtin).\n",
"The list of valid *Tabulator* formatters can be found in the [Tabulator documentation](https://tabulator.info/docs/5.5/format#format-builtin).\n",
"\n",
"Note that the equivalent specification may also be applied for column titles using the `title_formatters` parameter (but does not support Bokeh `CellFormatter` types)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## Editors/Editing\n",
"\n",
Expand Down Expand Up @@ -204,7 +213,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Therefore it is often preferable to use one of the [*Tabulator* editors](https://tabulator.info/docs/5.4/edit#edit) directly. Setting the editor of a column to `None` makes that column non-editable. Note that in addition to the standard *Tabulator* editors the `Tabulator` widget also supports `'date'` and `'datetime'` editors:"
"Therefore it is often preferable to use one of the [*Tabulator* editors](https://tabulator.info/docs/5.5/edit#edit) directly. Setting the editor of a column to `None` makes that column non-editable. Note that in addition to the standard *Tabulator* editors the `Tabulator` widget also supports `'date'` and `'datetime'` editors:"
]
},
{
Expand Down
13 changes: 12 additions & 1 deletion panel/widgets/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,10 @@ class Tabulator(BaseTable):
List of extra CSS classes to apply to the Tabulator element
to customize the theme.""")

title_formatters = param.Dict(default={}, doc="""
Tabulator formatter specification to use for a particular column
header title.""")

_data_params: ClassVar[List[str]] = [
'value', 'page', 'page_size', 'pagination', 'sorters', 'filters'
]
Expand All @@ -1112,7 +1116,7 @@ class Tabulator(BaseTable):
_rename: ClassVar[Mapping[str, str | None]] = {
'selection': None, 'row_content': None, 'row_height': None,
'text_align': None, 'embed_content': None, 'header_align': None,
'header_filters': None, 'styles': 'cell_styles'
'header_filters': None, 'styles': 'cell_styles', 'title_formatters': None
}

# Determines the maximum size limits beyond which (local, remote)
Expand Down Expand Up @@ -1718,6 +1722,13 @@ def _config_columns(self, column_objs: List[TableColumn]) -> List[Dict[str, Any]
formatter = dict(formatter)
col_dict['formatter'] = formatter.pop('type')
col_dict['formatterParams'] = formatter
title_formatter = self.title_formatters.get(column.field)
if title_formatter:
col_dict['titleFormatter'] = title_formatter
elif isinstance(title_formatter, dict):
formatter = dict(title_formatter)
col_dict['titleFormatter'] = title_formatter.pop('type')
col_dict['titleFormatterParams'] = title_formatter
col_name = self._renamed_cols[column.field]
if column.field in self.indexes:
if len(self.indexes) == 1:
Expand Down