Skip to content

Commit 2afe02a

Browse files
MoisanTomAugspurger
authored andcommitted
PERF: only output an html id if a style is applied (#23019)
* PERF: only output an html id if a style is applied * Add a parameter to Styler constructor * Use cell_ids instead of all_ids and set default to True * remove backslash
1 parent 21e8522 commit 2afe02a

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

pandas/io/formats/style.py

+17-9
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ class Styler(object):
6464
a unique identifier to avoid CSS collisions; generated automatically
6565
caption: str, default None
6666
caption to attach to the table
67+
cell_ids: bool, default True
68+
If True, each cell will have an ``id`` attribute in their HTML tag.
69+
The ``id`` takes the form ``T_<uuid>_row<num_row>_col<num_col>``
70+
where ``<uuid>`` is the unique identifier, ``<num_row>`` is the row
71+
number and ``<num_col>`` is the column number.
6772
6873
Attributes
6974
----------
@@ -112,7 +117,7 @@ class Styler(object):
112117
template = env.get_template("html.tpl")
113118

114119
def __init__(self, data, precision=None, table_styles=None, uuid=None,
115-
caption=None, table_attributes=None):
120+
caption=None, table_attributes=None, cell_ids=True):
116121
self.ctx = defaultdict(list)
117122
self._todo = []
118123

@@ -136,6 +141,7 @@ def __init__(self, data, precision=None, table_styles=None, uuid=None,
136141
self.table_attributes = table_attributes
137142
self.hidden_index = False
138143
self.hidden_columns = []
144+
self.cell_ids = cell_ids
139145

140146
# display_funcs maps (row, col) -> formatting function
141147

@@ -306,14 +312,16 @@ def format_attr(pair):
306312
cs.extend(cell_context.get("data", {}).get(r, {}).get(c, []))
307313
formatter = self._display_funcs[(r, c)]
308314
value = self.data.iloc[r, c]
309-
row_es.append({
310-
"type": "td",
311-
"value": value,
312-
"class": " ".join(cs),
313-
"id": "_".join(cs[1:]),
314-
"display_value": formatter(value),
315-
"is_visible": (c not in hidden_columns)
316-
})
315+
row_dict = {"type": "td",
316+
"value": value,
317+
"class": " ".join(cs),
318+
"display_value": formatter(value),
319+
"is_visible": (c not in hidden_columns)}
320+
# only add an id if the cell has a style
321+
if (self.cell_ids or
322+
not(len(ctx[r, c]) == 1 and ctx[r, c][0] == '')):
323+
row_dict["id"] = "_".join(cs[1:])
324+
row_es.append(row_dict)
317325
props = []
318326
for x in ctx[r, c]:
319327
# have to handle empty styles like ['']

pandas/io/formats/templates/html.tpl

+11-11
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,17 @@
5050
{%- endblock thead %}
5151
{%- block tbody %}
5252
<tbody>
53-
{%- block before_rows %}{%- endblock before_rows %}
54-
{%- for r in body %}
55-
{%- block tr scoped %}
56-
<tr>
57-
{%- for c in r %}
58-
{%- if c.is_visible != False %}
59-
<{{ c.type }} id="T_{{ uuid }}{{ c.id }}" class="{{ c.class }}" {{ c.attributes|join(" ") }}>{{ c.display_value }}</{{ c.type }}>
60-
{%- endif %}
61-
{%- endfor %}
62-
</tr>
63-
{%- endblock tr %}
53+
{% block before_rows %}{% endblock before_rows %}
54+
{% for r in body %}
55+
{% block tr scoped %}
56+
<tr>
57+
{% for c in r %}
58+
{% if c.is_visible != False %}
59+
<{{ c.type }} {% if c.id is defined -%} id="T_{{ uuid }}{{ c.id }}" {%- endif %} class="{{ c.class }}" {{ c.attributes|join(" ") }}>{{ c.display_value }}</{{ c.type }}>
60+
{% endif %}
61+
{%- endfor %}
62+
</tr>
63+
{% endblock tr %}
6464
{%- endfor %}
6565
{%- block after_rows %}{%- endblock after_rows %}
6666
</tbody>

0 commit comments

Comments
 (0)