Skip to content

Commit 5d50a24

Browse files
authored
REF: address the floating "_" in styler.uuid (#43037)
1 parent 73a69fd commit 5d50a24

File tree

7 files changed

+36
-36
lines changed

7 files changed

+36
-36
lines changed

doc/source/whatsnew/v1.4.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ ExtensionArray
349349

350350
Styler
351351
^^^^^^
352-
-
352+
- Minor bug in :class:`.Styler` where the ``uuid`` at initialization maintained a floating underscore (:issue:`43037`)
353353
-
354354

355355
Other

pandas/io/formats/style_render.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def __init__(
9090
if not isinstance(uuid_len, int) or not uuid_len >= 0:
9191
raise TypeError("``uuid_len`` must be an integer in range [0, 32].")
9292
self.uuid_len = min(32, uuid_len)
93-
self.uuid = (uuid or uuid4().hex[: self.uuid_len]) + "_"
93+
self.uuid = uuid or uuid4().hex[: self.uuid_len]
9494
self.table_styles = table_styles
9595
self.table_attributes = table_attributes
9696
self.caption = caption
@@ -1210,7 +1210,7 @@ def _pseudo_css(self, uuid: str, name: str, row: int, col: int, text: str):
12101210
-------
12111211
pseudo_css : List
12121212
"""
1213-
selector_id = "#T_" + uuid + "row" + str(row) + "_col" + str(col)
1213+
selector_id = "#T_" + uuid + "_row" + str(row) + "_col" + str(col)
12141214
return [
12151215
{
12161216
"selector": selector_id + f":hover .{name}",

pandas/io/formats/templates/html_style.tpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
{% block cellstyle %}
1515
{% for cs in [cellstyle, cellstyle_index, cellstyle_columns] %}
1616
{% for s in cs %}
17-
{% for selector in s.selectors %}{% if not loop.first %}, {% endif %}#T_{{uuid}}{{selector}}{% endfor %} {
17+
{% for selector in s.selectors %}{% if not loop.first %}, {% endif %}#T_{{uuid}}_{{selector}}{% endfor %} {
1818
{% for p,val in s.props %}
1919
{{p}}: {{val}};
2020
{% endfor %}

pandas/io/formats/templates/html_table.tpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
{% else %}
2828
{% for c in r %}
2929
{% if c.is_visible != False %}
30-
<{{c.type}} {%- if c.id is defined %} id="T_{{uuid}}{{c.id}}" {%- endif %} class="{{c.class}}" {{c.attributes}}>{{c.value}}</{{c.type}}>
30+
<{{c.type}} {%- if c.id is defined %} id="T_{{uuid}}_{{c.id}}" {%- endif %} class="{{c.class}}" {{c.attributes}}>{{c.value}}</{{c.type}}>
3131
{% endif %}
3232
{% endfor %}
3333
{% endif %}
@@ -49,7 +49,7 @@
4949
{% endif %}{% endfor %}
5050
{% else %}
5151
{% for c in r %}{% if c.is_visible != False %}
52-
<{{c.type}} {%- if c.id is defined %} id="T_{{uuid}}{{c.id}}" {%- endif %} class="{{c.class}}" {{c.attributes}}>{{c.display_value}}</{{c.type}}>
52+
<{{c.type}} {%- if c.id is defined %} id="T_{{uuid}}_{{c.id}}" {%- endif %} class="{{c.class}}" {{c.attributes}}>{{c.display_value}}</{{c.type}}>
5353
{% endif %}{% endfor %}
5454
{% endif %}
5555
</tr>

pandas/tests/io/formats/style/test_html.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def test_w3_html_format(styler):
9999
#T_ th {
100100
att2: v2;
101101
}
102-
#T_row0_col0, #T_row1_col0 {
102+
#T__row0_col0, #T__row1_col0 {
103103
att1: v1;
104104
}
105105
</style>
@@ -108,17 +108,17 @@ def test_w3_html_format(styler):
108108
<thead>
109109
<tr>
110110
<th class="blank level0" >&nbsp;</th>
111-
<th id="T_level0_col0" class="col_heading level0 col0" >A</th>
111+
<th id="T__level0_col0" class="col_heading level0 col0" >A</th>
112112
</tr>
113113
</thead>
114114
<tbody>
115115
<tr>
116-
<th id="T_level0_row0" class="row_heading level0 row0" >a</th>
117-
<td id="T_row0_col0" class="data row0 col0 my-cls2" >2.6</td>
116+
<th id="T__level0_row0" class="row_heading level0 row0" >a</th>
117+
<td id="T__row0_col0" class="data row0 col0 my-cls2" >2.6</td>
118118
</tr>
119119
<tr>
120-
<th id="T_level0_row1" class="row_heading level0 row1" >b</th>
121-
<td id="T_row1_col0" class="data row1 col0" >2.7</td>
120+
<th id="T__level0_row1" class="row_heading level0 row1" >b</th>
121+
<td id="T__row1_col0" class="data row1 col0" >2.7</td>
122122
</tr>
123123
</tbody>
124124
</table>
@@ -142,7 +142,7 @@ def test_rowspan_w3():
142142

143143

144144
def test_styles(styler):
145-
styler.set_uuid("abc_")
145+
styler.set_uuid("abc")
146146
styler.set_table_styles([{"selector": "td", "props": "color: red;"}])
147147
result = styler.to_html(doctype_html=True)
148148
expected = dedent(
@@ -152,13 +152,13 @@ def test_styles(styler):
152152
<head>
153153
<meta charset="utf-8">
154154
<style type="text/css">
155-
#T_abc_ td {
155+
#T_abc td {
156156
color: red;
157157
}
158158
</style>
159159
</head>
160160
<body>
161-
<table id="T_abc_">
161+
<table id="T_abc">
162162
<thead>
163163
<tr>
164164
<th class="blank level0" >&nbsp;</th>
@@ -418,15 +418,15 @@ def test_applymap_header_cell_ids(styler, index, columns):
418418

419419
# test index header ids where needed and css styles
420420
assert (
421-
'<th id="T_level0_row0" class="row_heading level0 row0" >a</th>' in result
421+
'<th id="T__level0_row0" class="row_heading level0 row0" >a</th>' in result
422422
) is index
423423
assert (
424-
'<th id="T_level0_row1" class="row_heading level0 row1" >b</th>' in result
424+
'<th id="T__level0_row1" class="row_heading level0 row1" >b</th>' in result
425425
) is index
426-
assert ("#T_level0_row0, #T_level0_row1 {\n attr: val;\n}" in result) is index
426+
assert ("#T__level0_row0, #T__level0_row1 {\n attr: val;\n}" in result) is index
427427

428428
# test column header ids where needed and css styles
429429
assert (
430-
'<th id="T_level0_col0" class="col_heading level0 col0" >A</th>' in result
430+
'<th id="T__level0_col0" class="col_heading level0 col0" >A</th>' in result
431431
) is columns
432-
assert ("#T_level0_col0 {\n attr: val;\n}" in result) is columns
432+
assert ("#T__level0_col0 {\n attr: val;\n}" in result) is columns

pandas/tests/io/formats/style/test_style.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1216,11 +1216,11 @@ def test_column_and_row_styling(self):
12161216
df = DataFrame(data=[[0, 1], [1, 2]], columns=["A", "B"])
12171217
s = Styler(df, uuid_len=0)
12181218
s = s.set_table_styles({"A": [{"selector": "", "props": [("color", "blue")]}]})
1219-
assert "#T__ .col0 {\n color: blue;\n}" in s.to_html()
1219+
assert "#T_ .col0 {\n color: blue;\n}" in s.to_html()
12201220
s = s.set_table_styles(
12211221
{0: [{"selector": "", "props": [("color", "blue")]}]}, axis=1
12221222
)
1223-
assert "#T__ .row0 {\n color: blue;\n}" in s.to_html()
1223+
assert "#T_ .row0 {\n color: blue;\n}" in s.to_html()
12241224

12251225
@pytest.mark.parametrize("len_", [1, 5, 32, 33, 100])
12261226
def test_uuid_len(self, len_):
@@ -1230,9 +1230,9 @@ def test_uuid_len(self, len_):
12301230
strt = s.find('id="T_')
12311231
end = s[strt + 6 :].find('"')
12321232
if len_ > 32:
1233-
assert end == 32 + 1
1233+
assert end == 32
12341234
else:
1235-
assert end == len_ + 1
1235+
assert end == len_
12361236

12371237
@pytest.mark.parametrize("len_", [-2, "bad", None])
12381238
def test_uuid_len_raises(self, len_):

pandas/tests/io/formats/style/test_tooltip.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,23 @@ def test_tooltip_render(ttips, styler):
3939
result = styler.set_tooltips(ttips).to_html()
4040

4141
# test tooltip table level class
42-
assert "#T__ .pd-t {\n visibility: hidden;\n" in result
42+
assert "#T_ .pd-t {\n visibility: hidden;\n" in result
4343

4444
# 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
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
4747
assert 'class="data row0 col0" >0<span class="pd-t"></span></td>' in result
4848

4949
# 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
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
5252
assert 'class="data row0 col2" >2<span class="pd-t"></span></td>' in result
5353

5454
# 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
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
5959
assert "Bad-Col" not in result
6060

6161

@@ -73,13 +73,13 @@ def test_tooltip_css_class(styler):
7373
css_class="other-class",
7474
props=[("color", "green")],
7575
).to_html()
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
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
7878

7979
# GH 39563
8080
result = styler.set_tooltips( # set_tooltips overwrites previous
8181
DataFrame([["tooltip"]], index=["x"], columns=["A"]),
8282
css_class="another-class",
8383
props="color:green;color:red;",
8484
).to_html()
85-
assert "#T__ .another-class {\n color: green;\n color: red;\n}" in result
85+
assert "#T_ .another-class {\n color: green;\n color: red;\n}" in result

0 commit comments

Comments
 (0)