Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/py-polars/tests/unit/dataframe/test_repr_html.py
6939 views
1
import pytest
2
3
import polars as pl
4
5
6
def test_repr_html() -> None:
7
# check it does not panic/error, and appears to contain
8
# a reasonable table with suitably escaped html entities.
9
df = pl.DataFrame(
10
{
11
"foo": [1, 2, 3],
12
"<bar>": ["a", "b", "c"],
13
"<baz": ["a", "b", "c"],
14
"spam>": ["a", "b", "c"],
15
}
16
)
17
html = df._repr_html_()
18
for match in (
19
"<table",
20
'class="dataframe"',
21
"<th>foo</th>",
22
"<th>&lt;bar&gt;</th>",
23
"<th>&lt;baz</th>",
24
"<th>spam&gt;</th>",
25
"<td>1</td>",
26
"<td>2</td>",
27
"<td>3</td>",
28
):
29
assert match in html, f"Expected to find {match!r} in html repr"
30
31
32
def test_html_tables() -> None:
33
df = pl.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]})
34
35
# default: header contains names/dtypes
36
header = "<thead><tr><th>a</th><th>b</th><th>c</th></tr><tr><td>i64</td><td>i64</td><td>i64</td></tr></thead>"
37
assert header in df._repr_html_()
38
39
# validate that relevant config options are respected
40
with pl.Config(tbl_hide_column_names=True):
41
header = "<thead><tr><td>i64</td><td>i64</td><td>i64</td></tr></thead>"
42
assert header in df._repr_html_()
43
44
with pl.Config(tbl_hide_column_data_types=True):
45
header = "<thead><tr><th>a</th><th>b</th><th>c</th></tr></thead>"
46
assert header in df._repr_html_()
47
48
with pl.Config(
49
tbl_hide_column_data_types=True,
50
tbl_hide_column_names=True,
51
):
52
header = "<thead></thead>"
53
assert header in df._repr_html_()
54
55
56
def test_df_repr_html_max_rows_default() -> None:
57
df = pl.DataFrame({"a": range(50)})
58
59
html = df._repr_html_()
60
61
expected_rows = 10
62
assert html.count("<td>") - 2 == expected_rows
63
64
65
def test_df_repr_html_max_rows_odd() -> None:
66
df = pl.DataFrame({"a": range(50)})
67
68
with pl.Config(tbl_rows=9):
69
html = df._repr_html_()
70
71
expected_rows = 9
72
assert html.count("<td>") - 2 == expected_rows
73
74
75
def test_series_repr_html_max_rows_default() -> None:
76
s = pl.Series("a", range(50))
77
78
html = s._repr_html_()
79
80
expected_rows = 10
81
assert html.count("<td>") - 2 == expected_rows
82
83
84
@pytest.mark.parametrize(
85
("text", "expected"),
86
[
87
("single space", "single space"),
88
("multiple spaces", "multiple&nbsp;&nbsp;&nbsp;spaces"),
89
(
90
" trailing & leading spaces ",
91
"&nbsp;&nbsp;trailing &amp; leading spaces&nbsp;&nbsp;",
92
),
93
],
94
)
95
def test_html_representation_multiple_spaces(text: str, expected: str) -> None:
96
with pl.Config(fmt_str_lengths=100):
97
html_repr = pl.DataFrame({"s": [text]})._repr_html_()
98
assert f"<td>&quot;{expected}&quot;</td>" in html_repr
99
100