Skip to content

Commit

Permalink
PERF: only output an html id if a style is applied (#23019)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Moisan authored and TomAugspurger committed Oct 14, 2018
1 parent 21e8522 commit 2afe02a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
26 changes: 17 additions & 9 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ class Styler(object):
a unique identifier to avoid CSS collisions; generated automatically
caption: str, default None
caption to attach to the table
cell_ids: bool, default True
If True, each cell will have an ``id`` attribute in their HTML tag.
The ``id`` takes the form ``T_<uuid>_row<num_row>_col<num_col>``
where ``<uuid>`` is the unique identifier, ``<num_row>`` is the row
number and ``<num_col>`` is the column number.
Attributes
----------
Expand Down Expand Up @@ -112,7 +117,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, cell_ids=True):
self.ctx = defaultdict(list)
self._todo = []

Expand All @@ -136,6 +141,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.cell_ids = cell_ids

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

Expand Down Expand Up @@ -306,14 +312,16 @@ 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 (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 = []
for x in ctx[r, c]:
# have to handle empty styles like ['']
Expand Down
22 changes: 11 additions & 11 deletions pandas/io/formats/templates/html.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@
{%- endblock thead %}
{%- block tbody %}
<tbody>
{%- block before_rows %}{%- endblock before_rows %}
{%- for r in body %}
{%- block tr scoped %}
<tr>
{%- for c in r %}
{%- if c.is_visible != False %}
<{{ c.type }} id="T_{{ uuid }}{{ c.id }}" class="{{ c.class }}" {{ c.attributes|join(" ") }}>{{ c.display_value }}</{{ c.type }}>
{%- endif %}
{%- endfor %}
</tr>
{%- endblock tr %}
{% block before_rows %}{% endblock before_rows %}
{% for r in body %}
{% block tr scoped %}
<tr>
{% 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 }}</{{ c.type }}>
{% endif %}
{%- endfor %}
</tr>
{% endblock tr %}
{%- endfor %}
{%- block after_rows %}{%- endblock after_rows %}
</tbody>
Expand Down

0 comments on commit 2afe02a

Please sign in to comment.