From 13d70ff1fed0114330f300cf1de9c4b9f143f278 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sun, 21 Feb 2021 11:31:30 +0100 Subject: [PATCH 1/3] performance improvement by limiting .join() --- pandas/io/formats/templates/html.tpl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pandas/io/formats/templates/html.tpl b/pandas/io/formats/templates/html.tpl index b315c57a65cdf..a595538fb0af6 100644 --- a/pandas/io/formats/templates/html.tpl +++ b/pandas/io/formats/templates/html.tpl @@ -56,7 +56,11 @@ {% 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|join(" ")}}>{{c.display_value}} +{% if c.type == 'th' %} + {{c.display_value}} +{% else %} + {{c.display_value}} +{% endif %} {% endif %} {% endfor %} From 0b8ad47989c32fd44c32047f19090ca3f3be2a90 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sun, 21 Feb 2021 12:24:08 +0100 Subject: [PATCH 2/3] performance improvement by limiting .join() --- pandas/io/formats/style.py | 5 +++-- pandas/io/formats/templates/html.tpl | 8 ++------ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 877e146fd8681..be748dfbdb2d6 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -466,7 +466,7 @@ def _translate(self): } colspan = col_lengths.get((r, c), 0) if colspan > 1: - es["attributes"] = [f'colspan="{colspan}"'] + es["attributes"] = f'colspan="{colspan}"' row_es.append(es) head.append(row_es) @@ -517,7 +517,7 @@ def _translate(self): } rowspan = idx_lengths.get((c, r), 0) if rowspan > 1: - es["attributes"] = [f'rowspan="{rowspan}"'] + es["attributes"] = f'rowspan="{rowspan}"' row_es.append(es) for c, col in enumerate(self.data.columns): @@ -529,6 +529,7 @@ def _translate(self): "value": value, "display_value": formatter(value), "is_visible": (c not in hidden_columns), + "attributes": "", } # only add an id if the cell has a style diff --git a/pandas/io/formats/templates/html.tpl b/pandas/io/formats/templates/html.tpl index a595538fb0af6..65fc1dfbb37c4 100644 --- a/pandas/io/formats/templates/html.tpl +++ b/pandas/io/formats/templates/html.tpl @@ -39,7 +39,7 @@ {% for c in r %} {% if c.is_visible != False %} - <{{c.type}} class="{{c.class}}" {{c.attributes|join(" ")}}>{{c.value}} + <{{c.type}} class="{{c.class}}" {{c.attributes}}>{{c.value}} {% endif %} {% endfor %} @@ -56,11 +56,7 @@ {% for c in r %} {% if c.is_visible != False %} -{% if c.type == 'th' %} - {{c.display_value}} -{% else %} - {{c.display_value}} -{% endif %} + <{{c.type}} {% if c.id is defined -%} id="T_{{uuid}}{{c.id}}" {%- endif %} class="{{c.class}}" {{c.attributes}}>{{c.display_value}} {% endif %} {% endfor %} From b5248115ff76007ed204bf9072b8dcfab94e1e59 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sun, 21 Feb 2021 12:29:15 +0100 Subject: [PATCH 3/3] fix test --- pandas/tests/io/formats/test_style.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/formats/test_style.py b/pandas/tests/io/formats/test_style.py index d134f33e15525..2a6505a43b80a 100644 --- a/pandas/tests/io/formats/test_style.py +++ b/pandas/tests/io/formats/test_style.py @@ -1323,7 +1323,7 @@ def test_mi_sparse(self): "display_value": "a", "is_visible": True, "type": "th", - "attributes": ['rowspan="2"'], + "attributes": 'rowspan="2"', "class": "row_heading level0 row0", "id": "level0_row0", }