From 8d8d1e567bbbeb25a16b64e26e50fc3dabea68ec Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sat, 14 Aug 2021 16:40:27 +0200 Subject: [PATCH 1/2] address the floating "_" in styler.uuid --- pandas/io/formats/style_render.py | 4 +-- pandas/io/formats/templates/html_style.tpl | 2 +- pandas/io/formats/templates/html_table.tpl | 4 +-- pandas/tests/io/formats/style/test_html.py | 28 +++++++++---------- pandas/tests/io/formats/style/test_style.py | 8 +++--- pandas/tests/io/formats/style/test_tooltip.py | 24 ++++++++-------- 6 files changed, 35 insertions(+), 35 deletions(-) diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index aa58b3abbd06c..241ae945039a5 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}} {%- if c.id is defined %} id="T_{{uuid}}_{{c.id}}" {%- endif %} class="{{c.class}}" {{c.attributes}}>{{c.value}} {% 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}} {%- if c.id is defined %} id="T_{{uuid}}_{{c.id}}" {%- endif %} class="{{c.class}}" {{c.attributes}}>{{c.display_value}} {% endif %}{% endfor %} {% endif %} diff --git a/pandas/tests/io/formats/style/test_html.py b/pandas/tests/io/formats/style/test_html.py index bcf3c4dbad3a8..5b718a84a0a24 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 - a - 2.6 + a + 2.6 - b - 2.7 + b + 2.7 @@ -142,7 +142,7 @@ def test_rowspan_w3(): def test_styles(styler): - styler.set_uuid("abc_") + styler.set_uuid("abc") styler.set_table_styles([{"selector": "td", "props": "color: red;"}]) result = styler.to_html(doctype_html=True) expected = dedent( @@ -152,13 +152,13 @@ def test_styles(styler): - +
@@ -418,15 +418,15 @@ def test_applymap_header_cell_ids(styler, index, columns): # test index header ids where needed and css styles assert ( - '' in result + '' in result ) is index assert ( - '' in result + '' 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 ( - '' in result + '' 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 6cc4b889d369a..746ddb4d72232 100644 --- a/pandas/tests/io/formats/style/test_style.py +++ b/pandas/tests/io/formats/style/test_style.py @@ -1331,11 +1331,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.render() + assert "#T_ .col0 {\n color: blue;\n}" in s.render() s = s.set_table_styles( {0: [{"selector": "", "props": [("color", "blue")]}]}, axis=1 ) - assert "#T__ .row0 {\n color: blue;\n}" in s.render() + assert "#T_ .row0 {\n color: blue;\n}" in s.render() @pytest.mark.parametrize("len_", [1, 5, 32, 33, 100]) def test_uuid_len(self, len_): @@ -1345,9 +1345,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 71ce496cca030..bd34051cf29e1 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).render() # 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")], ).render() - 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;", ).render() - 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 From 321e7ca195002cd2dee77c481919b53c5a28159c Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sat, 14 Aug 2021 16:52:58 +0200 Subject: [PATCH 2/2] address the floating "_" in styler.uuid --- doc/source/whatsnew/v1.4.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index f0af60f80edd5..9dc369d8c4258 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
 aabbAA