From b6f64637b084a929fb7b2809760e46f3fbdcc8f3 Mon Sep 17 00:00:00 2001 From: Thierry Moisan Date: Sat, 6 Oct 2018 14:19:48 -0400 Subject: [PATCH 1/4] PERF: only output an html id if a style is applied --- pandas/io/formats/style.py | 17 +++++++++-------- pandas/io/formats/templates/html.tpl | 24 ++++++++++++++---------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index f4bb53ba4f218..13b2e3744f74c 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -307,14 +307,15 @@ def format_attr(pair): cs.extend(cell_context.get("data", {}).get(r, {}).get(c, [])) formatter = self._display_funcs[(r, c)] value = self.data.iloc[r, c] - row_es.append({ - "type": "td", - "value": value, - "class": " ".join(cs), - "id": "_".join(cs[1:]), - "display_value": formatter(value), - "is_visible": (c not in hidden_columns) - }) + row_dict = {"type": "td", + "value": value, + "class": " ".join(cs), + "display_value": formatter(value), + "is_visible": (c not in hidden_columns)} + # only add an id if the cell has a style + if not(len(ctx[r, c]) == 1 and ctx[r, c][0] == ''): + row_dict["id"] = "_".join(cs[1:]) + row_es.append(row_dict) props = [] for x in ctx[r, c]: # have to handle empty styles like [''] diff --git a/pandas/io/formats/templates/html.tpl b/pandas/io/formats/templates/html.tpl index 706db1ecdd961..36e65e25496fe 100644 --- a/pandas/io/formats/templates/html.tpl +++ b/pandas/io/formats/templates/html.tpl @@ -50,17 +50,21 @@ {%- endblock thead %} {%- block tbody %} - {%- block before_rows %}{%- endblock before_rows %} - {%- for r in body %} - {%- block tr scoped %} - - {%- for c in r %} - {%- if c.is_visible != False %} + {% block before_rows %}{% endblock before_rows %} + {% for r in body %} + {% block tr scoped %} + + {%- for c in r %} + {%- if c.is_visible != False %} + {%- if c.id: %} <{{ c.type }} id="T_{{ uuid }}{{ c.id }}" class="{{ c.class }}" {{ c.attributes|join(" ") }}>{{ c.display_value }} - {%- endif %} - {%- endfor %} - - {%- endblock tr %} + {% else: -%} + <{{ c.type }} class="{{ c.class }}" {{ c.attributes|join(" ") }}>{{ c.display_value }} + {%- endif %} + {%- endif %} + {%- endfor %} + + {% endblock tr %} {%- endfor %} {%- block after_rows %}{%- endblock after_rows %} From 3d5a030b2c8950945ab33bc37033b1f2fdc82e92 Mon Sep 17 00:00:00 2001 From: Thierry Moisan Date: Sat, 6 Oct 2018 22:42:00 -0400 Subject: [PATCH 2/4] Add a parameter to Styler constructor --- pandas/io/formats/style.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 13b2e3744f74c..6474a2617ceb0 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -65,6 +65,8 @@ class Styler(object): a unique identifier to avoid CSS collisions; generated automatically caption: str, default None caption to attach to the table + all_ids: bool, default False + if True, each cell will have an ``id`` attribute in their HTML tag. Attributes ---------- @@ -113,7 +115,7 @@ class Styler(object): template = env.get_template("html.tpl") def __init__(self, data, precision=None, table_styles=None, uuid=None, - caption=None, table_attributes=None): + caption=None, table_attributes=None, all_ids=False): self.ctx = defaultdict(list) self._todo = [] @@ -137,6 +139,7 @@ def __init__(self, data, precision=None, table_styles=None, uuid=None, self.table_attributes = table_attributes self.hidden_index = False self.hidden_columns = [] + self.all_ids = all_ids # display_funcs maps (row, col) -> formatting function @@ -313,7 +316,8 @@ def format_attr(pair): "display_value": formatter(value), "is_visible": (c not in hidden_columns)} # only add an id if the cell has a style - if not(len(ctx[r, c]) == 1 and ctx[r, c][0] == ''): + if self.all_ids or \ + not(len(ctx[r, c]) == 1 and ctx[r, c][0] == ''): row_dict["id"] = "_".join(cs[1:]) row_es.append(row_dict) props = [] From 7a755c8a4e84412e07e19d3539325958181f54f8 Mon Sep 17 00:00:00 2001 From: Thierry Moisan Date: Thu, 11 Oct 2018 14:48:06 -0500 Subject: [PATCH 3/4] Use cell_ids instead of all_ids and set default to True --- pandas/io/formats/style.py | 13 ++++++++----- pandas/io/formats/templates/html.tpl | 12 ++++-------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 6474a2617ceb0..236ba1f9369ef 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -65,8 +65,11 @@ class Styler(object): a unique identifier to avoid CSS collisions; generated automatically caption: str, default None caption to attach to the table - all_ids: bool, default False - if True, each cell will have an ``id`` attribute in their HTML tag. + cell_ids: bool, default True + If True, each cell will have an ``id`` attribute in their HTML tag. + The ``id`` takes the form ``T__row_col`` + where ```` is the unique identifier, ```` is the row + number and ```` is the column number. Attributes ---------- @@ -115,7 +118,7 @@ class Styler(object): template = env.get_template("html.tpl") def __init__(self, data, precision=None, table_styles=None, uuid=None, - caption=None, table_attributes=None, all_ids=False): + caption=None, table_attributes=None, cell_ids=True): self.ctx = defaultdict(list) self._todo = [] @@ -139,7 +142,7 @@ def __init__(self, data, precision=None, table_styles=None, uuid=None, self.table_attributes = table_attributes self.hidden_index = False self.hidden_columns = [] - self.all_ids = all_ids + self.cell_ids = cell_ids # display_funcs maps (row, col) -> formatting function @@ -316,7 +319,7 @@ def format_attr(pair): "display_value": formatter(value), "is_visible": (c not in hidden_columns)} # only add an id if the cell has a style - if self.all_ids or \ + if self.cell_ids or \ not(len(ctx[r, c]) == 1 and ctx[r, c][0] == ''): row_dict["id"] = "_".join(cs[1:]) row_es.append(row_dict) diff --git a/pandas/io/formats/templates/html.tpl b/pandas/io/formats/templates/html.tpl index 36e65e25496fe..01ecde7d081f5 100644 --- a/pandas/io/formats/templates/html.tpl +++ b/pandas/io/formats/templates/html.tpl @@ -54,14 +54,10 @@ {% for r in body %} {% block tr scoped %} - {%- for c in r %} - {%- if c.is_visible != False %} - {%- if c.id: %} - <{{ c.type }} id="T_{{ uuid }}{{ c.id }}" class="{{ c.class }}" {{ c.attributes|join(" ") }}>{{ c.display_value }} - {% else: -%} - <{{ c.type }} class="{{ c.class }}" {{ c.attributes|join(" ") }}>{{ c.display_value }} - {%- endif %} - {%- endif %} + {% 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 }} + {% endif %} {%- endfor %} {% endblock tr %} From 1d726a43be62c9ead7e1f08cdfdfa8b65e785ede Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Fri, 12 Oct 2018 10:18:47 -0500 Subject: [PATCH 4/4] remove backslash --- pandas/io/formats/style.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 92b6358c1df1b..3b3238586b310 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -318,8 +318,8 @@ def format_attr(pair): "display_value": formatter(value), "is_visible": (c not in hidden_columns)} # only add an id if the cell has a style - if self.cell_ids or \ - not(len(ctx[r, c]) == 1 and ctx[r, c][0] == ''): + if (self.cell_ids or + not(len(ctx[r, c]) == 1 and ctx[r, c][0] == '')): row_dict["id"] = "_".join(cs[1:]) row_es.append(row_dict) props = []