From f917726fb6f589c2d1cb3351f9a4b01a3f1de527 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Mon, 16 Aug 2021 16:48:46 +0200 Subject: [PATCH 1/6] add to docs --- doc/source/user_guide/options.rst | 1 + pandas/core/config_init.py | 12 ++++++++++++ pandas/io/formats/style.py | 2 ++ 3 files changed, 15 insertions(+) diff --git a/doc/source/user_guide/options.rst b/doc/source/user_guide/options.rst index 62a347acdaa34..a4d98d3d35bcf 100644 --- a/doc/source/user_guide/options.rst +++ b/doc/source/user_guide/options.rst @@ -487,6 +487,7 @@ styler.sparse.index True "Sparsify" MultiIndex displ elements in outer levels within groups). styler.sparse.columns True "Sparsify" MultiIndex display for columns in Styler output. +styler.render.repr html Standard output format for Styler rendered in Jupyter Notebook. styler.render.max_elements 262144 Maximum number of datapoints that Styler will render trimming either rows, columns or both to fit. ======================================= ============ ================================== diff --git a/pandas/core/config_init.py b/pandas/core/config_init.py index 27b898782fbef..44fd72396ec23 100644 --- a/pandas/core/config_init.py +++ b/pandas/core/config_init.py @@ -756,6 +756,11 @@ def register_converter_cb(key): display each explicit level element in a hierarchical key for each column. """ +styler_render_repr = """ +: str + Determine which output to use in Jupyter Notebook in {"html", "latex"}. +""" + styler_max_elements = """ : int The maximum number of data-cell () elements that will be rendered before @@ -769,6 +774,13 @@ def register_converter_cb(key): "sparse.columns", True, styler_sparse_columns_doc, validator=bool ) + cf.register_option( + "render.repr", + "html", + styler_render_repr, + validator=is_one_of_factory(["html", "latex"]), + ) + cf.register_option( "render.max_elements", 2 ** 18, diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index a72de753d6a8a..f9f7ca64a27ca 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -214,6 +214,8 @@ def _repr_html_(self) -> str: """ Hooks into Jupyter notebook rich display system. """ + if get_option("styler.render.repr") == "latex": + return self.to_latex() return self.render() def render( From 0d459ef91f9ac8f5076d00875be473411d691a3c Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Mon, 16 Aug 2021 16:49:50 +0200 Subject: [PATCH 2/6] add to docs --- doc/source/user_guide/options.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/user_guide/options.rst b/doc/source/user_guide/options.rst index a4d98d3d35bcf..db6c763512207 100644 --- a/doc/source/user_guide/options.rst +++ b/doc/source/user_guide/options.rst @@ -488,6 +488,7 @@ styler.sparse.index True "Sparsify" MultiIndex displ styler.sparse.columns True "Sparsify" MultiIndex display for columns in Styler output. styler.render.repr html Standard output format for Styler rendered in Jupyter Notebook. + Should be one of "html" or "latex". styler.render.max_elements 262144 Maximum number of datapoints that Styler will render trimming either rows, columns or both to fit. ======================================= ============ ================================== From 983d6f4a911b1ac212c420710db8acd5f1553ff9 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Mon, 23 Aug 2021 15:33:17 +0200 Subject: [PATCH 3/6] add test --- pandas/tests/io/formats/style/test_to_latex.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pandas/tests/io/formats/style/test_to_latex.py b/pandas/tests/io/formats/style/test_to_latex.py index ac164f2de9fb2..1ef1f45c31e1d 100644 --- a/pandas/tests/io/formats/style/test_to_latex.py +++ b/pandas/tests/io/formats/style/test_to_latex.py @@ -675,3 +675,9 @@ def test_apply_map_header_render_mi(df, index, columns): """ ) assert (expected_columns in result) is columns + + +def test_repr_option(styler): + assert " Date: Sat, 28 Aug 2021 19:51:52 +0200 Subject: [PATCH 4/6] whats new --- doc/source/whatsnew/v1.4.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 450ecc85c725b..5496af31be2ce 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -75,6 +75,7 @@ Styler - :meth:`.Styler.to_latex` introduces keyword argument ``environment``, which also allows a specific "longtable" entry through a separate jinja2 template (:issue:`41866`). - :meth:`.Styler.to_html` introduces keyword arguments ``sparse_index`` and ``sparse_columns`` (:issue:`41946`) - Keyword argument ``level`` is added to :meth:`.Styler.hide_index` and :meth:`.Styler.hide_columns` for optionally controlling hidden levels in a MultiIndex (:issue:`25475`) + - Global options have been extended to configure default ``Styler`` properties including rendering options (:issue:`41395`) There are also bug fixes and deprecations listed below. From 5ddf521ca87a0266b21381a39a034953b449ad7b Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Mon, 30 Aug 2021 18:46:54 +0200 Subject: [PATCH 5/6] add _latex_repr_ --- pandas/io/formats/style.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 281692df92748..38bb5fa2a776a 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -212,12 +212,16 @@ def __init__( def _repr_html_(self) -> str: """ - Hooks into Jupyter notebook rich display system. + Hooks into Jupyter notebook rich display system, which calls _repr_html_ by + default if an object is returned at the end of a cell. """ if get_option("styler.render.repr") == "latex": - return self.to_latex() + return self._repr_latex_() return self.to_html() + def _repr_latex_(self) -> str: + return self.to_latex() + def render( self, sparse_index: bool | None = None, From 4cae9acbaff0818e50b4df41263ed2292e2de82b Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Wed, 1 Sep 2021 18:41:31 +0200 Subject: [PATCH 6/6] refactor --- pandas/io/formats/style.py | 10 ++++++---- pandas/tests/io/formats/style/test_to_latex.py | 4 +++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index be39c7aec2b51..955455b813638 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -236,12 +236,14 @@ def _repr_html_(self) -> str: Hooks into Jupyter notebook rich display system, which calls _repr_html_ by default if an object is returned at the end of a cell. """ - if get_option("styler.render.repr") == "latex": - return self._repr_latex_() - return self.to_html() + if get_option("styler.render.repr") == "html": + return self.to_html() + return None def _repr_latex_(self) -> str: - return self.to_latex() + if get_option("styler.render.repr") == "latex": + return self.to_latex() + return None def render( self, diff --git a/pandas/tests/io/formats/style/test_to_latex.py b/pandas/tests/io/formats/style/test_to_latex.py index 1ef1f45c31e1d..db9c77ab42c67 100644 --- a/pandas/tests/io/formats/style/test_to_latex.py +++ b/pandas/tests/io/formats/style/test_to_latex.py @@ -679,5 +679,7 @@ def test_apply_map_header_render_mi(df, index, columns): def test_repr_option(styler): assert "