|
7 | 7 | from pandas.io.formats.style import Styler
|
8 | 8 |
|
9 | 9 |
|
10 |
| -class TestStylerTooltip: |
11 |
| - @pytest.mark.parametrize( |
12 |
| - "ttips", |
13 |
| - [ |
14 |
| - DataFrame( |
15 |
| - data=[["Min", "Max"], [np.nan, ""]], |
16 |
| - columns=["A", "B"], |
17 |
| - index=["a", "b"], |
18 |
| - ), |
19 |
| - DataFrame(data=[["Max", "Min"]], columns=["B", "A"], index=["a"]), |
20 |
| - DataFrame( |
21 |
| - data=[["Min", "Max", None]], columns=["A", "B", "C"], index=["a"] |
22 |
| - ), |
23 |
| - ], |
| 10 | +@pytest.fixture |
| 11 | +def df(): |
| 12 | + return DataFrame( |
| 13 | + data=[[0, 1, 2], [3, 4, 5], [6, 7, 8]], |
| 14 | + columns=["A", "B", "C"], |
| 15 | + index=["x", "y", "z"], |
24 | 16 | )
|
25 |
| - def test_tooltip_render(self, ttips): |
26 |
| - # GH 21266 |
27 |
| - df = DataFrame(data=[[0, 3], [1, 2]], columns=["A", "B"], index=["a", "b"]) |
28 |
| - s = Styler(df, uuid_len=0).set_tooltips(ttips).render() |
29 |
| - |
30 |
| - # test tooltip table level class |
31 |
| - assert "#T__ .pd-t {\n visibility: hidden;\n" in s |
32 |
| - |
33 |
| - # test 'Min' tooltip added |
34 |
| - assert ( |
35 |
| - "#T__ #T__row0_col0:hover .pd-t {\n visibility: visible;\n}\n" |
36 |
| - + '#T__ #T__row0_col0 .pd-t::after {\n content: "Min";\n}' |
37 |
| - in s |
38 |
| - ) |
39 |
| - assert ( |
40 |
| - '<td id="T__row0_col0" class="data row0 col0" >0<span class="pd-t">' |
41 |
| - + "</span></td>" |
42 |
| - in s |
43 |
| - ) |
44 |
| - |
45 |
| - # test 'Max' tooltip added |
46 |
| - assert ( |
47 |
| - "#T__ #T__row0_col1:hover .pd-t {\n visibility: visible;\n}\n" |
48 |
| - + '#T__ #T__row0_col1 .pd-t::after {\n content: "Max";\n}' |
49 |
| - in s |
50 |
| - ) |
51 |
| - assert ( |
52 |
| - '<td id="T__row0_col1" class="data row0 col1" >3<span class="pd-t">' |
53 |
| - + "</span></td>" |
54 |
| - in s |
55 |
| - ) |
56 |
| - |
57 |
| - def test_tooltip_reindex(self): |
58 |
| - # GH 39317 |
59 |
| - df = DataFrame( |
60 |
| - data=[[0, 1, 2], [3, 4, 5], [6, 7, 8]], columns=[0, 1, 2], index=[0, 1, 2] |
61 |
| - ) |
62 |
| - ttips = DataFrame( |
63 |
| - data=[["Mi", "Ma"], ["Mu", "Mo"]], |
64 |
| - columns=[0, 2], |
65 |
| - index=[0, 2], |
66 |
| - ) |
67 |
| - s = Styler(df, uuid_len=0).set_tooltips(DataFrame(ttips)).render() |
68 |
| - assert '#T__ #T__row0_col0 .pd-t::after {\n content: "Mi";\n}' in s |
69 |
| - assert '#T__ #T__row0_col2 .pd-t::after {\n content: "Ma";\n}' in s |
70 |
| - assert '#T__ #T__row2_col0 .pd-t::after {\n content: "Mu";\n}' in s |
71 |
| - assert '#T__ #T__row2_col2 .pd-t::after {\n content: "Mo";\n}' in s |
72 |
| - |
73 |
| - def test_tooltip_ignored(self): |
74 |
| - # GH 21266 |
75 |
| - df = DataFrame(data=[[0, 1], [2, 3]]) |
76 |
| - s = Styler(df).render() # no set_tooltips() creates no <span> |
77 |
| - assert '<style type="text/css">\n</style>' in s |
78 |
| - assert '<span class="pd-t"></span>' not in s |
79 |
| - |
80 |
| - def test_tooltip_css_class(self): |
81 |
| - # GH 21266 |
82 |
| - df = DataFrame(data=[[0, 1], [2, 3]]) |
83 |
| - s = ( |
84 |
| - Styler(df, uuid_len=0) |
85 |
| - .set_tooltips( |
86 |
| - DataFrame([["tooltip"]]), |
87 |
| - css_class="other-class", |
88 |
| - props=[("color", "green")], |
89 |
| - ) |
90 |
| - .render() |
91 |
| - ) |
92 |
| - assert "#T__ .other-class {\n color: green;\n" in s |
93 |
| - assert '#T__ #T__row0_col0 .other-class::after {\n content: "tooltip";\n' in s |
94 |
| - |
95 |
| - # GH 39563 |
96 |
| - s = ( |
97 |
| - Styler(df, uuid_len=0) |
98 |
| - .set_tooltips( |
99 |
| - DataFrame([["tooltip"]]), |
100 |
| - css_class="other-class", |
101 |
| - props="color:green;color:red;", |
102 |
| - ) |
103 |
| - .render() |
104 |
| - ) |
105 |
| - assert "#T__ .other-class {\n color: green;\n color: red;\n}" in s |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def styler(df): |
| 21 | + return Styler(df, uuid_len=0) |
| 22 | + |
| 23 | + |
| 24 | +@pytest.mark.parametrize( |
| 25 | + "ttips", |
| 26 | + [ |
| 27 | + DataFrame( # Test basic reindex and ignoring blank |
| 28 | + data=[["Min", "Max"], [np.nan, ""]], |
| 29 | + columns=["A", "C"], |
| 30 | + index=["x", "y"], |
| 31 | + ), |
| 32 | + DataFrame( # Test non-referenced columns, reversed col names, short index |
| 33 | + data=[["Max", "Min", "Bad-Col"]], columns=["C", "A", "D"], index=["x"] |
| 34 | + ), |
| 35 | + ], |
| 36 | +) |
| 37 | +def test_tooltip_render(ttips, styler): |
| 38 | + # GH 21266 |
| 39 | + result = styler.set_tooltips(ttips).render() |
| 40 | + |
| 41 | + # test tooltip table level class |
| 42 | + assert "#T__ .pd-t {\n visibility: hidden;\n" in result |
| 43 | + |
| 44 | + # test 'Min' tooltip added |
| 45 | + assert "#T__ #T__row0_col0:hover .pd-t {\n visibility: visible;\n}" in result |
| 46 | + assert '#T__ #T__row0_col0 .pd-t::after {\n content: "Min";\n}' in result |
| 47 | + assert 'class="data row0 col0" >0<span class="pd-t"></span></td>' in result |
| 48 | + |
| 49 | + # test 'Max' tooltip added |
| 50 | + assert "#T__ #T__row0_col2:hover .pd-t {\n visibility: visible;\n}" in result |
| 51 | + assert '#T__ #T__row0_col2 .pd-t::after {\n content: "Max";\n}' in result |
| 52 | + assert 'class="data row0 col2" >2<span class="pd-t"></span></td>' in result |
| 53 | + |
| 54 | + # test Nan, empty string and bad column ignored |
| 55 | + assert "#T__ #T__row1_col0:hover .pd-t {\n visibility: visible;\n}" not in result |
| 56 | + assert "#T__ #T__row1_col1:hover .pd-t {\n visibility: visible;\n}" not in result |
| 57 | + assert "#T__ #T__row0_col1:hover .pd-t {\n visibility: visible;\n}" not in result |
| 58 | + assert "#T__ #T__row1_col2:hover .pd-t {\n visibility: visible;\n}" not in result |
| 59 | + assert "Bad-Col" not in result |
| 60 | + |
| 61 | + |
| 62 | +def test_tooltip_ignored(styler): |
| 63 | + # GH 21266 |
| 64 | + result = styler.render() # no set_tooltips() creates no <span> |
| 65 | + assert '<style type="text/css">\n</style>' in result |
| 66 | + assert '<span class="pd-t"></span>' not in result |
| 67 | + |
| 68 | + |
| 69 | +def test_tooltip_css_class(styler): |
| 70 | + # GH 21266 |
| 71 | + result = styler.set_tooltips( |
| 72 | + DataFrame([["tooltip"]], index=["x"], columns=["A"]), |
| 73 | + css_class="other-class", |
| 74 | + props=[("color", "green")], |
| 75 | + ).render() |
| 76 | + assert "#T__ .other-class {\n color: green;\n" in result |
| 77 | + assert '#T__ #T__row0_col0 .other-class::after {\n content: "tooltip";\n' in result |
| 78 | + |
| 79 | + # GH 39563 |
| 80 | + result = styler.set_tooltips( # set_tooltips overwrites previous |
| 81 | + DataFrame([["tooltip"]], index=["x"], columns=["A"]), |
| 82 | + css_class="another-class", |
| 83 | + props="color:green;color:red;", |
| 84 | + ).render() |
| 85 | + assert "#T__ .another-class {\n color: green;\n color: red;\n}" in result |
0 commit comments