From e0351a7e7f7cbe823e2086c9a6cfe7d9346a8b10 Mon Sep 17 00:00:00 2001 From: Thijs Nieuwdorp Date: Thu, 14 Nov 2024 13:43:06 +0100 Subject: [PATCH] Replace spaces with   in HTML repr Ruff formatting Move to appropriate file Ruff --- py-polars/polars/dataframe/_html.py | 4 +++- .../tests/unit/dataframe/test_repr_html.py | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) 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   "
""" + )