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 all commits
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Styler
- Keyword arguments ``level`` and ``names`` added to :meth:`.Styler.hide_index` and :meth:`.Styler.hide_columns` for additional control of visibility of MultiIndexes and index names (:issue:`25475`, :issue:`43404`, :issue:`43346`)
- Global options have been extended to configure default ``Styler`` properties including formatting and encoding and mathjax options and LaTeX (:issue:`41395`)
- Naive sparsification is now possible for LaTeX without the multirow package (:issue:`43369`)
- :meth:`Styler.to_html` omits CSSStyle rules for hidden table elements (:issue:`43619`)

Formerly Styler relied on ``display.html.use_mathjax``, which has now been replaced by ``styler.html.mathjax``.

Expand Down
27 changes: 21 additions & 6 deletions pandas/io/formats/style_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,11 +378,12 @@ def _translate_header(
if clabels:
column_headers = []
for c, value in enumerate(clabels[r]):
header_element_visible = _is_visible(c, r, col_lengths)
header_element = _element(
"th",
f"{col_heading_class} level{r} col{c}",
value,
_is_visible(c, r, col_lengths),
header_element_visible,
display_value=self._display_funcs_columns[(r, c)](value),
attributes=(
f'colspan="{col_lengths.get((r, c), 0)}"'
Expand All @@ -393,7 +394,11 @@ def _translate_header(

if self.cell_ids:
header_element["id"] = f"level{r}_col{c}"
if (r, c) in self.ctx_columns and self.ctx_columns[r, c]:
if (
header_element_visible
and (r, c) in self.ctx_columns
and self.ctx_columns[r, c]
):
header_element["id"] = f"level{r}_col{c}"
self.cellstyle_map_columns[
tuple(self.ctx_columns[r, c])
Expand Down Expand Up @@ -537,11 +542,14 @@ def _translate_body(

index_headers = []
for c, value in enumerate(rlabels[r]):
header_element_visible = (
_is_visible(r, c, idx_lengths) and not self.hide_index_[c]
)
header_element = _element(
"th",
f"{row_heading_class} level{c} row{r}",
value,
_is_visible(r, c, idx_lengths) and not self.hide_index_[c],
header_element_visible,
display_value=self._display_funcs_index[(r, c)](value),
attributes=(
f'rowspan="{idx_lengths.get((c, r), 0)}"'
Expand All @@ -552,7 +560,11 @@ def _translate_body(

if self.cell_ids:
header_element["id"] = f"level{c}_row{r}" # id is specified
if (r, c) in self.ctx_index and self.ctx_index[r, c]:
if (
header_element_visible
and (r, c) in self.ctx_index
and self.ctx_index[r, c]
):
# always add id if a style is specified
header_element["id"] = f"level{c}_row{r}"
self.cellstyle_map_index[tuple(self.ctx_index[r, c])].append(
Expand Down Expand Up @@ -580,18 +592,21 @@ def _translate_body(
if (r, c) in self.cell_context:
cls = " " + self.cell_context[r, c]

data_element_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_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_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
63 changes: 63 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,66 @@ 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_include_css_style_rules_only_for_visible_cells(styler_mi):
# GH 43619
result = (
styler_mi.set_uuid("")
.applymap(lambda v: "color: blue;")
.hide_columns(styler_mi.data.columns[1:])
.hide_index(styler_mi.data.index[1:])
.to_html()
)
expected_styles = dedent(
"""\
<style type="text/css">
#T__row0_col0 {
color: blue;
}
</style>
"""
)
assert expected_styles in result


def test_include_css_style_rules_only_for_visible_index_labels(styler_mi):
# GH 43619
result = (
styler_mi.set_uuid("")
.applymap_index(lambda v: "color: blue;", axis="index")
.hide_columns(styler_mi.data.columns)
.hide_index(styler_mi.data.index[1:])
.to_html()
)
expected_styles = dedent(
"""\
<style type="text/css">
#T__level0_row0, #T__level1_row0 {
color: blue;
}
</style>
"""
)
assert expected_styles in result


def test_include_css_style_rules_only_for_visible_column_labels(styler_mi):
# GH 43619
result = (
styler_mi.set_uuid("")
.applymap_index(lambda v: "color: blue;", axis="columns")
.hide_columns(styler_mi.data.columns[1:])
.hide_index(styler_mi.data.index)
.to_html()
)
expected_styles = dedent(
"""\
<style type="text/css">
#T__level0_col0, #T__level1_col0 {
color: blue;
}
</style>
"""
)
assert expected_styles in result