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

BUG: CSS strings truncated at ":" #59720

Merged
merged 12 commits into from
Oct 4, 2024
24 changes: 12 additions & 12 deletions pandas/io/formats/style_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,9 +906,9 @@ def concatenated_visible_rows(obj):
row_body_headers = [
{
**col,
"display_value": col["display_value"]
if col["is_visible"]
else "",
"display_value": (
col["display_value"] if col["is_visible"] else ""
),
"cellstyle": self.ctx_index[r, c],
}
for c, col in enumerate(row[:index_levels])
Expand Down Expand Up @@ -2069,18 +2069,18 @@ def maybe_convert_css_to_tuples(style: CSSProperties) -> CSSList:
('border','1px solid red')]
"""
if isinstance(style, str):
s = style.split(";")
try:
return [
(x.split(":")[0].strip(), x.split(":")[1].strip())
for x in s
if x.strip() != ""
]
except IndexError as err:
if style and ":" not in style:
raise ValueError(
"Styles supplied as string must follow CSS rule formats, "
f"for example 'attr: val;'. '{style}' was given."
) from err
)
s = style.split(";")
return [
(x.split(":")[0].strip(), ":".join(x.split(":")[1:]).strip())
for x in s
if x.strip() != ""
]

return style


Expand Down
13 changes: 12 additions & 1 deletion pandas/tests/io/formats/style/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,8 +886,19 @@ def test_maybe_convert_css_to_tuples(self):
expected = []
assert maybe_convert_css_to_tuples("") == expected

# issue #59623
expected = [("a", "b"), ("c", "url('data:123')")]
assert maybe_convert_css_to_tuples("a:b;c: url('data:123');") == expected

# if no value, return attr and empty string
expected = [("a", ""), ("c", "")]
assert maybe_convert_css_to_tuples("a:;c: ") == expected

def test_maybe_convert_css_to_tuples_err(self):
msg = "Styles supplied as string must follow CSS rule formats"
msg = (
"Styles supplied as string must follow CSS rule formats, "
"for example 'attr: val;'. 'err' was given."
)
with pytest.raises(ValueError, match=msg):
maybe_convert_css_to_tuples("err")

Expand Down