Skip to content

ENH: styler.render.repr option #43180

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 16 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from 8 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
2 changes: 2 additions & 0 deletions doc/source/user_guide/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
======================================= ============ ==================================
Expand Down
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 @@ -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.

Expand Down
12 changes: 12 additions & 0 deletions pandas/core/config_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (<td>) elements that will be rendered before
Expand All @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ def _repr_html_(self) -> str:
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC aren't we supposed to have a _repr_latex_ ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know. See if you prefer the new pattern, simple change.

Hooks into Jupyter notebook rich display system.
"""
if get_option("styler.render.repr") == "latex":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we actually still need this? or is this something controlled by the notebook itself?

Copy link
Contributor Author

@attack68 attack68 Sep 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to look this up, and I may be wrong but I think Notebook uses _repr_latex_ when converting the notebook to other formats, so it is useful.

When _repr_html_ and _repr_latex are present the notebook still renders the html version in browser. And I can't find docs on native iPython how to configure it to use the other. So the pandas options is userfriendly override (per issue)

return self.to_latex()
return self.to_html()

def render(
Expand Down Expand Up @@ -887,7 +889,7 @@ def to_html(
``class`` and ``id`` identifiers, or solely the ``<table>`` 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.

Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/io/formats/style/test_to_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<style" in styler._repr_html_()[:6]
with option_context("styler.render.repr", "latex"):
assert "\\begin{tabular}" in styler._repr_html_()[:15]