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

fix(python): Replace spaces with   to support showing multiple spaces in HTML repr #19783

Merged
merged 2 commits into from
Nov 15, 2024
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
4 changes: 3 additions & 1 deletion py-polars/polars/dataframe/_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ def write_body(self) -> None:
else:
series = self.df[:, c]
self.elements.append(
html.escape(series._s.get_fmt(r, str_len_limit))
html.escape(
series._s.get_fmt(r, str_len_limit)
).replace(" ", " ")
)

def write(self, inner: str) -> None:
Expand Down
29 changes: 29 additions & 0 deletions py-polars/tests/unit/dataframe/test_repr_html.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

import polars as pl


Expand Down Expand Up @@ -77,3 +79,30 @@ def test_series_repr_html_max_rows_default() -> None:

expected_rows = 10
assert html.count("<td>") - 2 == expected_rows


def test_html_representation_multiple_spaces() -> None:
df = pl.DataFrame(
{"string_col": ["multiple spaces", " trailing and leading "]}
)
html_repr = df._repr_html_()

# Regex explanation:
# Matches cell content inside <td>...</td> tags, but only within the <tbody> section
# 1. <tbody>: Ensures matching starts within the <tbody> section.
# 2. .*?: Lazily matches any content until the first <td> tag.
# 3. <td>(.*?)</td>: Captures the content inside each <td> tag (non-greedy).
# 4. .*?: Lazily matches any content between <td>...</td> and </tbody>.
# 5. </tbody>: Ensures matching ends at the closing </tbody> tag.
# The re.S flag allows the regex to work across multiple lines.
cell_pattern = re.compile(r"<tbody>.*?<td>(.*?)</td>.*?</tbody>", re.S)

cells = cell_pattern.findall(html_repr)

for cell_content in cells:
# Check that there are no regular spaces in the content
assert " " not in cell_content, f"Unexpected space in cell: {cell_content}"
# Check that the content contains &nbsp; as required
assert (
"&nbsp;" in cell_content
), f"Expected &nbsp; in cell but found: {cell_content}"