diff --git a/py-polars/polars/dataframe/_html.py b/py-polars/polars/dataframe/_html.py index 6f034eab0f41..b2f153584b5b 100644 --- a/py-polars/polars/dataframe/_html.py +++ b/py-polars/polars/dataframe/_html.py @@ -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: diff --git a/py-polars/tests/unit/dataframe/test_repr_html.py b/py-polars/tests/unit/dataframe/test_repr_html.py index 8e7a62a6efc2..6ee004fabf22 100644 --- a/py-polars/tests/unit/dataframe/test_repr_html.py +++ b/py-polars/tests/unit/dataframe/test_repr_html.py @@ -77,3 +77,22 @@ def test_series_repr_html_max_rows_default() -> None: expected_rows = 10 assert html.count("") - 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_() + + assert ( + html_repr + == """
+shape: (2, 1)
string_col
str
"multiple   spaces"
"  trailing and leading   "
""" + )