diff --git a/doc/source/user_guide/options.rst b/doc/source/user_guide/options.rst index 1ba7ea21d6c8c..1193dff4361b4 100644 --- a/doc/source/user_guide/options.rst +++ b/doc/source/user_guide/options.rst @@ -487,6 +487,8 @@ 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. + 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. styler.render.encoding utf-8 Default encoding for output HTML or LaTeX files. diff --git a/pandas/core/config_init.py b/pandas/core/config_init.py index b048eaee4aee1..f594232f5fb6d 100644 --- a/pandas/core/config_init.py +++ b/pandas/core/config_init.py @@ -757,6 +757,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 @@ -827,6 +832,13 @@ def register_converter_cb(key): "sparse.columns", True, styler_sparse_columns_doc, validator=is_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 a4bb19e395e38..54711473c1f5d 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -233,9 +233,17 @@ 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. """ - return self.to_html() + if get_option("styler.render.repr") == "html": + return self.to_html() + return None + + def _repr_latex_(self) -> str: + if get_option("styler.render.repr") == "latex": + return self.to_latex() + return None def render( self, @@ -927,7 +935,7 @@ def to_html( ``class`` and ``id`` identifiers, or solely the ```` element without styling identifiers. **kwargs - Any additional keyword arguments are passed through to the jinj2 + Any additional keyword arguments are passed through to the jinja2 ``self.template.render`` process. This is useful when you need to provide additional variables for a custom template. diff --git a/pandas/tests/io/formats/style/test_to_latex.py b/pandas/tests/io/formats/style/test_to_latex.py index 914eeff97c0fe..671219872bb34 100644 --- a/pandas/tests/io/formats/style/test_to_latex.py +++ b/pandas/tests/io/formats/style/test_to_latex.py @@ -705,3 +705,11 @@ def test_apply_map_header_render_mi(df, index, columns): """ ) assert (expected_columns in result) is columns + + +def test_repr_option(styler): + assert "