Skip to content

ENH: omit CSSStyle rules for hidden table elements (#43619) #43673

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions doc/source/whatsnew/v1.3.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ Bug fixes

.. _whatsnew_134.other:

Other
~~~~~
-
-
Performance improvements
~~~~~~~~~~~~~~~~~~~~~~~~
- Performance improvement in :meth:`Styler.to_html` by omitting CSSStyle rules for hidden table cells (:issue:`43619`)

.. ---------------------------------------------------------------------------

Expand Down
7 changes: 5 additions & 2 deletions pandas/io/formats/style_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,18 +580,21 @@ def _translate_body(
if (r, c) in self.cell_context:
cls = " " + self.cell_context[r, c]

data_element_is_visible = (
c not in self.hidden_columns and r not in self.hidden_rows
)
data_element = _element(
"td",
f"{data_class} row{r} col{c}{cls}",
value,
(c not in self.hidden_columns and r not in self.hidden_rows),
data_element_is_visible,
attributes="",
display_value=self._display_funcs[(r, c)](value),
)

if self.cell_ids:
data_element["id"] = f"row{r}_col{c}"
if (r, c) in self.ctx and self.ctx[r, c]:
if data_element_is_visible and (r, c) in self.ctx and self.ctx[r, c]:
# always add id if needed due to specified style
data_element["id"] = f"row{r}_col{c}"
self.cellstyle_map[tuple(self.ctx[r, c])].append(f"row{r}_col{c}")
Expand Down
39 changes: 39 additions & 0 deletions pandas/tests/io/formats/style/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,3 +467,42 @@ def test_maximums(styler_mi, rows, cols):
assert ">5</td>" in result # [[0,1], [4,5]] always visible
assert (">8</td>" in result) is not rows # first trimmed vertical element
assert (">2</td>" in result) is not cols # first trimmed horizontal element


def test_omit_css_style_rules_for_hidden_cells():
# GH 43619
df = DataFrame([[1, 2], [3, 4]], columns=["A", "B"])
result = (
df.style.set_uuid("")
.background_gradient()
.hide_columns(["A"])
.hide_index([0])
.to_html()
)
expected = dedent(
"""\
<style type="text/css">
#T__row1_col1 {
background-color: #023858;
color: #f1f1f1;
}
</style>
<table id="T_">
<thead>
<tr>
<th class="blank level0" >&nbsp;</th>
<th id="T__level0_col1" class="col_heading level0 col1" >B</th>
</tr>
</thead>
<tbody>
<tr>
</tr>
<tr>
<th id="T__level0_row1" class="row_heading level0 row1" >1</th>
<td id="T__row1_col1" class="data row1 col1" >4</td>
</tr>
</tbody>
</table>
"""
)
assert result == expected