diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst
index b5e1dc51a5a2d..8fb989bbb60b7 100644
--- a/doc/source/whatsnew/v1.4.0.rst
+++ b/doc/source/whatsnew/v1.4.0.rst
@@ -349,7 +349,7 @@ ExtensionArray
Styler
^^^^^^
--
+- Minor bug in :class:`.Styler` where the ``uuid`` at initialization maintained a floating underscore (:issue:`43037`)
-
Other
diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py
index 979af71444854..8c8112cf585a1 100644
--- a/pandas/io/formats/style_render.py
+++ b/pandas/io/formats/style_render.py
@@ -90,7 +90,7 @@ def __init__(
if not isinstance(uuid_len, int) or not uuid_len >= 0:
raise TypeError("``uuid_len`` must be an integer in range [0, 32].")
self.uuid_len = min(32, uuid_len)
- self.uuid = (uuid or uuid4().hex[: self.uuid_len]) + "_"
+ self.uuid = uuid or uuid4().hex[: self.uuid_len]
self.table_styles = table_styles
self.table_attributes = table_attributes
self.caption = caption
@@ -1210,7 +1210,7 @@ def _pseudo_css(self, uuid: str, name: str, row: int, col: int, text: str):
-------
pseudo_css : List
"""
- selector_id = "#T_" + uuid + "row" + str(row) + "_col" + str(col)
+ selector_id = "#T_" + uuid + "_row" + str(row) + "_col" + str(col)
return [
{
"selector": selector_id + f":hover .{name}",
diff --git a/pandas/io/formats/templates/html_style.tpl b/pandas/io/formats/templates/html_style.tpl
index 5b0e7a2ed882b..5c3fcd97f51bb 100644
--- a/pandas/io/formats/templates/html_style.tpl
+++ b/pandas/io/formats/templates/html_style.tpl
@@ -14,7 +14,7 @@
{% block cellstyle %}
{% for cs in [cellstyle, cellstyle_index, cellstyle_columns] %}
{% for s in cs %}
-{% for selector in s.selectors %}{% if not loop.first %}, {% endif %}#T_{{uuid}}{{selector}}{% endfor %} {
+{% for selector in s.selectors %}{% if not loop.first %}, {% endif %}#T_{{uuid}}_{{selector}}{% endfor %} {
{% for p,val in s.props %}
{{p}}: {{val}};
{% endfor %}
diff --git a/pandas/io/formats/templates/html_table.tpl b/pandas/io/formats/templates/html_table.tpl
index 3e3a40b9fdaa6..3ecd911c1ec07 100644
--- a/pandas/io/formats/templates/html_table.tpl
+++ b/pandas/io/formats/templates/html_table.tpl
@@ -27,7 +27,7 @@
{% else %}
{% for c in r %}
{% if c.is_visible != False %}
- <{{c.type}} {%- if c.id is defined %} id="T_{{uuid}}{{c.id}}" {%- endif %} class="{{c.class}}" {{c.attributes}}>{{c.value}}{{c.type}}>
+ <{{c.type}} {%- if c.id is defined %} id="T_{{uuid}}_{{c.id}}" {%- endif %} class="{{c.class}}" {{c.attributes}}>{{c.value}}{{c.type}}>
{% endif %}
{% endfor %}
{% endif %}
@@ -49,7 +49,7 @@
{% endif %}{% endfor %}
{% else %}
{% for c in r %}{% if c.is_visible != False %}
- <{{c.type}} {%- if c.id is defined %} id="T_{{uuid}}{{c.id}}" {%- endif %} class="{{c.class}}" {{c.attributes}}>{{c.display_value}}{{c.type}}>
+ <{{c.type}} {%- if c.id is defined %} id="T_{{uuid}}_{{c.id}}" {%- endif %} class="{{c.class}}" {{c.attributes}}>{{c.display_value}}{{c.type}}>
{% endif %}{% endfor %}
{% endif %}
diff --git a/pandas/tests/io/formats/style/test_html.py b/pandas/tests/io/formats/style/test_html.py
index f5787a8f38d6c..0087090feafce 100644
--- a/pandas/tests/io/formats/style/test_html.py
+++ b/pandas/tests/io/formats/style/test_html.py
@@ -99,7 +99,7 @@ def test_w3_html_format(styler):
#T_ th {
att2: v2;
}
- #T_row0_col0, #T_row1_col0 {
+ #T__row0_col0, #T__row1_col0 {
att1: v1;
}
@@ -108,17 +108,17 @@ def test_w3_html_format(styler):
- A
+ A
@@ -418,15 +418,15 @@ def test_applymap_header_cell_ids(styler, index, columns): # test index header ids where needed and css styles assert ( - ' | a | ' in result + 'a | ' in result ) is index assert ( - 'b | ' in result + 'b | ' in result ) is index - assert ("#T_level0_row0, #T_level0_row1 {\n attr: val;\n}" in result) is index + assert ("#T__level0_row0, #T__level0_row1 {\n attr: val;\n}" in result) is index # test column header ids where needed and css styles assert ( - 'A | ' in result + 'A | ' in result ) is columns - assert ("#T_level0_col0 {\n attr: val;\n}" in result) is columns + assert ("#T__level0_col0 {\n attr: val;\n}" in result) is columns diff --git a/pandas/tests/io/formats/style/test_style.py b/pandas/tests/io/formats/style/test_style.py index cf65686463d50..683c537133aef 100644 --- a/pandas/tests/io/formats/style/test_style.py +++ b/pandas/tests/io/formats/style/test_style.py @@ -1216,11 +1216,11 @@ def test_column_and_row_styling(self): df = DataFrame(data=[[0, 1], [1, 2]], columns=["A", "B"]) s = Styler(df, uuid_len=0) s = s.set_table_styles({"A": [{"selector": "", "props": [("color", "blue")]}]}) - assert "#T__ .col0 {\n color: blue;\n}" in s.to_html() + assert "#T_ .col0 {\n color: blue;\n}" in s.to_html() s = s.set_table_styles( {0: [{"selector": "", "props": [("color", "blue")]}]}, axis=1 ) - assert "#T__ .row0 {\n color: blue;\n}" in s.to_html() + assert "#T_ .row0 {\n color: blue;\n}" in s.to_html() @pytest.mark.parametrize("len_", [1, 5, 32, 33, 100]) def test_uuid_len(self, len_): @@ -1230,9 +1230,9 @@ def test_uuid_len(self, len_): strt = s.find('id="T_') end = s[strt + 6 :].find('"') if len_ > 32: - assert end == 32 + 1 + assert end == 32 else: - assert end == len_ + 1 + assert end == len_ @pytest.mark.parametrize("len_", [-2, "bad", None]) def test_uuid_len_raises(self, len_): diff --git a/pandas/tests/io/formats/style/test_tooltip.py b/pandas/tests/io/formats/style/test_tooltip.py index 1bef89be78377..c49a0e05c6700 100644 --- a/pandas/tests/io/formats/style/test_tooltip.py +++ b/pandas/tests/io/formats/style/test_tooltip.py @@ -39,23 +39,23 @@ def test_tooltip_render(ttips, styler): result = styler.set_tooltips(ttips).to_html() # test tooltip table level class - assert "#T__ .pd-t {\n visibility: hidden;\n" in result + assert "#T_ .pd-t {\n visibility: hidden;\n" in result # test 'Min' tooltip added - assert "#T__ #T__row0_col0:hover .pd-t {\n visibility: visible;\n}" in result - assert '#T__ #T__row0_col0 .pd-t::after {\n content: "Min";\n}' in result + assert "#T_ #T__row0_col0:hover .pd-t {\n visibility: visible;\n}" in result + assert '#T_ #T__row0_col0 .pd-t::after {\n content: "Min";\n}' in result assert 'class="data row0 col0" >0' in result # test 'Max' tooltip added - assert "#T__ #T__row0_col2:hover .pd-t {\n visibility: visible;\n}" in result - assert '#T__ #T__row0_col2 .pd-t::after {\n content: "Max";\n}' in result + assert "#T_ #T__row0_col2:hover .pd-t {\n visibility: visible;\n}" in result + assert '#T_ #T__row0_col2 .pd-t::after {\n content: "Max";\n}' in result assert 'class="data row0 col2" >2' in result # test Nan, empty string and bad column ignored - assert "#T__ #T__row1_col0:hover .pd-t {\n visibility: visible;\n}" not in result - assert "#T__ #T__row1_col1:hover .pd-t {\n visibility: visible;\n}" not in result - assert "#T__ #T__row0_col1:hover .pd-t {\n visibility: visible;\n}" not in result - assert "#T__ #T__row1_col2:hover .pd-t {\n visibility: visible;\n}" not in result + assert "#T_ #T__row1_col0:hover .pd-t {\n visibility: visible;\n}" not in result + assert "#T_ #T__row1_col1:hover .pd-t {\n visibility: visible;\n}" not in result + assert "#T_ #T__row0_col1:hover .pd-t {\n visibility: visible;\n}" not in result + assert "#T_ #T__row1_col2:hover .pd-t {\n visibility: visible;\n}" not in result assert "Bad-Col" not in result @@ -73,8 +73,8 @@ def test_tooltip_css_class(styler): css_class="other-class", props=[("color", "green")], ).to_html() - assert "#T__ .other-class {\n color: green;\n" in result - assert '#T__ #T__row0_col0 .other-class::after {\n content: "tooltip";\n' in result + assert "#T_ .other-class {\n color: green;\n" in result + assert '#T_ #T__row0_col0 .other-class::after {\n content: "tooltip";\n' in result # GH 39563 result = styler.set_tooltips( # set_tooltips overwrites previous @@ -82,4 +82,4 @@ def test_tooltip_css_class(styler): css_class="another-class", props="color:green;color:red;", ).to_html() - assert "#T__ .another-class {\n color: green;\n color: red;\n}" in result + assert "#T_ .another-class {\n color: green;\n color: red;\n}" in result
---|