Skip to content

Commit ecaa78d

Browse files
authored
ENH: styler.render.repr option (#43180)
1 parent 4fd0afa commit ecaa78d

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

doc/source/user_guide/options.rst

+2
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,8 @@ styler.sparse.index True "Sparsify" MultiIndex displ
487487
elements in outer levels within groups).
488488
styler.sparse.columns True "Sparsify" MultiIndex display for columns
489489
in Styler output.
490+
styler.render.repr html Standard output format for Styler rendered in Jupyter Notebook.
491+
Should be one of "html" or "latex".
490492
styler.render.max_elements 262144 Maximum number of datapoints that Styler will render
491493
trimming either rows, columns or both to fit.
492494
styler.render.encoding utf-8 Default encoding for output HTML or LaTeX files.

pandas/core/config_init.py

+12
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,11 @@ def register_converter_cb(key):
757757
display each explicit level element in a hierarchical key for each column.
758758
"""
759759

760+
styler_render_repr = """
761+
: str
762+
Determine which output to use in Jupyter Notebook in {"html", "latex"}.
763+
"""
764+
760765
styler_max_elements = """
761766
: int
762767
The maximum number of data-cell (<td>) elements that will be rendered before
@@ -827,6 +832,13 @@ def register_converter_cb(key):
827832
"sparse.columns", True, styler_sparse_columns_doc, validator=is_bool
828833
)
829834

835+
cf.register_option(
836+
"render.repr",
837+
"html",
838+
styler_render_repr,
839+
validator=is_one_of_factory(["html", "latex"]),
840+
)
841+
830842
cf.register_option(
831843
"render.max_elements",
832844
2 ** 18,

pandas/io/formats/style.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,17 @@ def __init__(
233233

234234
def _repr_html_(self) -> str:
235235
"""
236-
Hooks into Jupyter notebook rich display system.
236+
Hooks into Jupyter notebook rich display system, which calls _repr_html_ by
237+
default if an object is returned at the end of a cell.
237238
"""
238-
return self.to_html()
239+
if get_option("styler.render.repr") == "html":
240+
return self.to_html()
241+
return None
242+
243+
def _repr_latex_(self) -> str:
244+
if get_option("styler.render.repr") == "latex":
245+
return self.to_latex()
246+
return None
239247

240248
def render(
241249
self,
@@ -927,7 +935,7 @@ def to_html(
927935
``class`` and ``id`` identifiers, or solely the ``<table>`` element without
928936
styling identifiers.
929937
**kwargs
930-
Any additional keyword arguments are passed through to the jinj2
938+
Any additional keyword arguments are passed through to the jinja2
931939
``self.template.render`` process. This is useful when you need to provide
932940
additional variables for a custom template.
933941

pandas/tests/io/formats/style/test_to_latex.py

+8
Original file line numberDiff line numberDiff line change
@@ -705,3 +705,11 @@ def test_apply_map_header_render_mi(df, index, columns):
705705
"""
706706
)
707707
assert (expected_columns in result) is columns
708+
709+
710+
def test_repr_option(styler):
711+
assert "<style" in styler._repr_html_()[:6]
712+
assert styler._repr_latex_() is None
713+
with option_context("styler.render.repr", "latex"):
714+
assert "\\begin{tabular}" in styler._repr_latex_()[:15]
715+
assert styler._repr_html_() is None

0 commit comments

Comments
 (0)