Path: blob/main/py-polars/tests/unit/dataframe/test_repr_html.py
6939 views
import pytest12import polars as pl345def test_repr_html() -> None:6# check it does not panic/error, and appears to contain7# a reasonable table with suitably escaped html entities.8df = pl.DataFrame(9{10"foo": [1, 2, 3],11"<bar>": ["a", "b", "c"],12"<baz": ["a", "b", "c"],13"spam>": ["a", "b", "c"],14}15)16html = df._repr_html_()17for match in (18"<table",19'class="dataframe"',20"<th>foo</th>",21"<th><bar></th>",22"<th><baz</th>",23"<th>spam></th>",24"<td>1</td>",25"<td>2</td>",26"<td>3</td>",27):28assert match in html, f"Expected to find {match!r} in html repr"293031def test_html_tables() -> None:32df = pl.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]})3334# default: header contains names/dtypes35header = "<thead><tr><th>a</th><th>b</th><th>c</th></tr><tr><td>i64</td><td>i64</td><td>i64</td></tr></thead>"36assert header in df._repr_html_()3738# validate that relevant config options are respected39with pl.Config(tbl_hide_column_names=True):40header = "<thead><tr><td>i64</td><td>i64</td><td>i64</td></tr></thead>"41assert header in df._repr_html_()4243with pl.Config(tbl_hide_column_data_types=True):44header = "<thead><tr><th>a</th><th>b</th><th>c</th></tr></thead>"45assert header in df._repr_html_()4647with pl.Config(48tbl_hide_column_data_types=True,49tbl_hide_column_names=True,50):51header = "<thead></thead>"52assert header in df._repr_html_()535455def test_df_repr_html_max_rows_default() -> None:56df = pl.DataFrame({"a": range(50)})5758html = df._repr_html_()5960expected_rows = 1061assert html.count("<td>") - 2 == expected_rows626364def test_df_repr_html_max_rows_odd() -> None:65df = pl.DataFrame({"a": range(50)})6667with pl.Config(tbl_rows=9):68html = df._repr_html_()6970expected_rows = 971assert html.count("<td>") - 2 == expected_rows727374def test_series_repr_html_max_rows_default() -> None:75s = pl.Series("a", range(50))7677html = s._repr_html_()7879expected_rows = 1080assert html.count("<td>") - 2 == expected_rows818283@pytest.mark.parametrize(84("text", "expected"),85[86("single space", "single space"),87("multiple spaces", "multiple spaces"),88(89" trailing & leading spaces ",90" trailing & leading spaces ",91),92],93)94def test_html_representation_multiple_spaces(text: str, expected: str) -> None:95with pl.Config(fmt_str_lengths=100):96html_repr = pl.DataFrame({"s": [text]})._repr_html_()97assert f"<td>"{expected}"</td>" in html_repr9899100