Skip to content

Bug multiple css selectors GH44011 #44023

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

Merged
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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,8 @@ Styler
- Bug when rendering an empty DataFrame with a named index (:issue:`43305`).
- Bug when rendering a single level MultiIndex (:issue:`43383`).
- Bug when combining non-sparse rendering and :meth:`.Styler.hide_columns` or :meth:`.Styler.hide_index` (:issue:`43464`)
- Bug setting a table style when using multiple selectors in :class:`.Styler` (:issue:`44011`)
-

Other
^^^^^
Expand Down
3 changes: 2 additions & 1 deletion pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
StylerRenderer,
Subset,
Tooltips,
format_table_styles,
maybe_convert_css_to_tuples,
non_reducing_slice,
refactor_levels,
Expand Down Expand Up @@ -2047,7 +2048,7 @@ def set_table_styles(
}
for key, styles in table_styles.items()
for idx in obj.get_indexer_for([key])
for s in styles
for s in format_table_styles(styles)
]
else:
table_styles = [
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/formats/style_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def _translate(
# construct render dict
d = {
"uuid": self.uuid,
"table_styles": _format_table_styles(self.table_styles or []),
"table_styles": format_table_styles(self.table_styles or []),
"caption": self.caption,
}

Expand Down Expand Up @@ -1173,7 +1173,7 @@ def _is_visible(idx_row, idx_col, lengths) -> bool:
return (idx_col, idx_row) in lengths


def _format_table_styles(styles: CSSStyles) -> CSSStyles:
def format_table_styles(styles: CSSStyles) -> CSSStyles:
"""
looks for multiple CSS selectors and separates them:
[{'selector': 'td, th', 'props': 'a:v;'}]
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/io/formats/style/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,19 @@ def test_table_styles_multiple(self):
{"selector": "tr", "props": [("color", "green")]},
]

def test_table_styles_dict_multiple_selectors(self):
# GH 44011
result = self.df.style.set_table_styles(
[{"selector": "th,td", "props": [("border-left", "2px solid black")]}]
)._translate(True, True)["table_styles"]

expected = [
{"selector": "th", "props": [("border-left", "2px solid black")]},
{"selector": "td", "props": [("border-left", "2px solid black")]},
]

assert result == expected

def test_maybe_convert_css_to_tuples(self):
expected = [("a", "b"), ("c", "d e")]
assert maybe_convert_css_to_tuples("a:b;c:d e;") == expected
Expand Down