Skip to content

Commit

Permalink
Replace spaces with   in HTML repr
Browse files Browse the repository at this point in the history
Ruff formatting

Move to appropriate file

Ruff
  • Loading branch information
TNieuwdorp committed Nov 14, 2024
1 parent 9af7ccd commit e0351a7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
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
19 changes: 19 additions & 0 deletions py-polars/tests/unit/dataframe/test_repr_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,22 @@ 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_()

assert (
html_repr
== """<div><style>
.dataframe > thead > tr,
.dataframe > tbody > tr {
text-align: right;
white-space: pre-wrap;
}
</style>
<small>shape: (2, 1)</small><table border="1" class="dataframe"><thead><tr><th>string_col</th></tr><tr><td>str</td></tr></thead><tbody><tr><td>&quot;multiple&nbsp;&nbsp;&nbsp;spaces&quot;</td></tr><tr><td>&quot;&nbsp;&nbsp;trailing&nbsp;and&nbsp;leading&nbsp;&nbsp;&nbsp;&quot;</td></tr></tbody></table></div>"""
)

0 comments on commit e0351a7

Please sign in to comment.