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 controlling text alignment in DataFrame and Tabulator #2331

Merged
merged 2 commits into from
May 20, 2021
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
1 change: 1 addition & 0 deletions examples/reference/widgets/DataFrame.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"* **``selection``** (``list``) The currently selected rows.\n",
"* **``show_index``** (``boolean``): Whether to show the index column.\n",
"* **``sortable``** (``sortable``): Allows to sort table's contents. By default natural order is preserved. To sort a column, click on it's header. Clicking one more time changes sort direction. Use Ctrl + click to return to natural order. Use Shift + click to sort multiple columns simultaneously\n",
"* **``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",
"* **`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
1 change: 1 addition & 0 deletions examples/reference/widgets/Tabulator.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"* **``selection``** (``list``): The currently selected rows.\n",
"* **``selectable``** (`boolean` or `str`): Whether to allow selection of rows. Can be `True`, `False`, `'checkbox'` or `'toggle'`. \n",
"* **``show_index``** (``boolean``): Whether to show the index column.\n",
"* **``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``): The CSS theme to apply (note that changing the theme will restyle all tables on the page).\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",
Expand Down
14 changes: 14 additions & 0 deletions panel/widgets/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class BaseTable(ReactiveData, Widget):
show_index = param.Boolean(default=True, doc="""
Whether to show the index column.""")

text_align = param.ClassSelector(default={}, class_=(dict, str), doc="""
A mapping from column name to alignment or a fixed column
alignment, which should be one of 'left', 'center', 'right'.""")

titles = param.Dict(default={}, doc="""
A mapping from column name to a title to override the name with.""")

Expand Down Expand Up @@ -129,6 +133,11 @@ def _get_column_definitions(self, col_names, df):
formatter = StringFormatter()
editor = StringEditor()

if isinstance(self.text_align, str):
formatter.text_align = self.text_align
elif col in self.text_align:
formatter.text_align = self.text_align[col]

if col in self.editors and not isinstance(self.editors[col], (dict, str)):
editor = self.editors[col]

Expand Down Expand Up @@ -1004,6 +1013,11 @@ def _config_columns(self, column_objs):
if column.field in group_cols
]
col_dict = {'field': column.field}

if isinstance(self.text_align, str):
col_dict['hozAlign'] = self.text_align
elif column.field in self.text_align:
col_dict['hozAlign'] = self.text_align[column.field]
formatter = self.formatters.get(column.field)
if isinstance(formatter, str):
col_dict['formatter'] = formatter
Expand Down