Skip to content

ENH: sparse_columns and sparse_index added to Styler.to_html #41946

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 21 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
be15967
add sparsify to to_html
attack68 Jun 11, 2021
c187dc2
add tests
attack68 Jun 11, 2021
c53040e
doc fix
attack68 Jun 11, 2021
44cca16
Merge remote-tracking branch 'upstream/master' into styler_to_html_sp…
attack68 Jun 12, 2021
6427b88
Merge branch 'rls1.3.0' into styler_to_html_sparse_args
attack68 Jun 15, 2021
6bf29f8
Merge remote-tracking branch 'upstream/master' into styler_to_html_sp…
attack68 Jun 18, 2021
2b166b4
Merge remote-tracking branch 'upstream/master' into styler_to_html_sp…
attack68 Jun 20, 2021
ad4cee6
Merge remote-tracking branch 'upstream/master' into styler_to_html_sp…
attack68 Jun 29, 2021
3889890
Merge remote-tracking branch 'upstream/master' into styler_to_html_sp…
attack68 Jul 3, 2021
60053ab
Merge remote-tracking branch 'upstream/master' into styler_to_html_sp…
attack68 Jul 5, 2021
512af50
whats new 1.4.0
attack68 Jul 5, 2021
13090ab
better test
attack68 Jul 5, 2021
38559f5
versionadded
attack68 Jul 5, 2021
5ba2db9
Merge remote-tracking branch 'upstream/master' into styler_to_html_sp…
attack68 Jul 8, 2021
2ca6207
Merge remote-tracking branch 'upstream/master' into styler_to_html_sp…
attack68 Jul 10, 2021
855e886
Merge remote-tracking branch 'upstream/master' into styler_to_html_sp…
attack68 Jul 13, 2021
e796368
Merge remote-tracking branch 'upstream/master' into styler_to_html_sp…
attack68 Jul 14, 2021
1770300
Merge remote-tracking branch 'upstream/master' into styler_to_html_sp…
attack68 Jul 20, 2021
ad1f85d
Merge remote-tracking branch 'upstream/master' into styler_to_html_sp…
attack68 Jul 24, 2021
71c9dae
Merge branch 'master' into styler_to_html_sparse_args
jreback Jul 28, 2021
cadcc3e
Merge remote-tracking branch 'upstream/master' into styler_to_html_sp…
attack68 Jul 28, 2021
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 @@ -35,6 +35,7 @@ Other enhancements
- Additional options added to :meth:`.Styler.bar` to control alignment and display, with keyword only arguments (:issue:`26070`, :issue:`36419`)
- :meth:`Styler.bar` now validates the input argument ``width`` and ``height`` (:issue:`42511`)
- :meth:`Series.ewm`, :meth:`DataFrame.ewm`, now support a ``method`` argument with a ``'table'`` option that performs the windowing operation over an entire :class:`DataFrame`. See :ref:`Window Overview <window.overview>` for performance and functional benefits (:issue:`42273`)
- Added ``sparse_index`` and ``sparse_columns`` keyword arguments to :meth:`.Styler.to_html` (:issue:`41946`)
- Added keyword argument ``environment`` to :meth:`.Styler.to_latex` also allowing a specific "longtable" entry with a separate jinja2 template (:issue:`41866`)
-

Expand Down
27 changes: 24 additions & 3 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,8 @@ def to_latex(
Defaults to ``pandas.options.styler.sparse.index`` value.
sparse_columns : bool, optional
Whether to sparsify the display of a hierarchical index. Setting to False
will display each explicit level element in a hierarchical key for each row.
Defaults to ``pandas.options.styler.sparse.columns`` value.
will display each explicit level element in a hierarchical key for each
column. Defaults to ``pandas.options.styler.sparse.columns`` value.
multirow_align : {"c", "t", "b"}
If sparsifying hierarchical MultiIndexes whether to align text centrally,
at the top or bottom.
Expand Down Expand Up @@ -815,6 +815,8 @@ def to_html(
*,
table_uuid: str | None = None,
table_attributes: str | None = None,
sparse_index: bool | None = None,
sparse_columns: bool | None = None,
encoding: str | None = None,
doctype_html: bool = False,
exclude_styles: bool = False,
Expand All @@ -840,6 +842,18 @@ def to_html(
``<table .. <table_attributes> >``

If not given defaults to Styler's preexisting value.
sparse_index : bool, optional
Whether to sparsify the display of a hierarchical index. Setting to False
will display each explicit level element in a hierarchical key for each row.
Defaults to ``pandas.options.styler.sparse.index`` value.

.. versionadded:: 1.4.0
sparse_columns : bool, optional
Whether to sparsify the display of a hierarchical index. Setting to False
will display each explicit level element in a hierarchical key for each
column. Defaults to ``pandas.options.styler.sparse.columns`` value.

.. versionadded:: 1.4.0
encoding : str, optional
Character encoding setting for file output, and HTML meta tags,
defaults to "utf-8" if None.
Expand All @@ -866,8 +880,15 @@ def to_html(
if table_attributes:
self.set_table_attributes(table_attributes)

if sparse_index is None:
sparse_index = get_option("styler.sparse.index")
if sparse_columns is None:
sparse_columns = get_option("styler.sparse.columns")

# Build HTML string..
html = self.render(
html = self._render_html(
sparse_index=sparse_index,
sparse_columns=sparse_columns,
exclude_styles=exclude_styles,
encoding=encoding if encoding else "utf-8",
doctype_html=doctype_html,
Expand Down
22 changes: 22 additions & 0 deletions pandas/tests/io/formats/style/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pandas import (
DataFrame,
MultiIndex,
option_context,
)

jinja2 = pytest.importorskip("jinja2")
Expand Down Expand Up @@ -429,3 +430,24 @@ def test_sticky_levels(styler_mi, index, columns):
def test_sticky_raises(styler):
with pytest.raises(ValueError, match="`axis` must be"):
styler.set_sticky(axis="bad")


@pytest.mark.parametrize(
"sparse_index, sparse_columns",
[(True, True), (True, False), (False, True), (False, False)],
)
def test_sparse_options(sparse_index, sparse_columns):
cidx = MultiIndex.from_tuples([("Z", "a"), ("Z", "b"), ("Y", "c")])
ridx = MultiIndex.from_tuples([("A", "a"), ("A", "b"), ("B", "c")])
df = DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], index=ridx, columns=cidx)
styler = df.style

default_html = styler.to_html() # defaults under pd.options to (True , True)

with option_context(
"styler.sparse.index", sparse_index, "styler.sparse.columns", sparse_columns
):
html1 = styler.to_html()
assert (html1 == default_html) is (sparse_index and sparse_columns)
html2 = styler.to_html(sparse_index=sparse_index, sparse_columns=sparse_columns)
assert html1 == html2