Skip to content
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

ENH: styler.render.repr option #43180

Merged
merged 16 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from all 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.
styler.render.encoding utf-8 Default encoding for output HTML or LaTeX files.
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 @@ -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 (<td>) elements that will be rendered before
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 11 additions & 3 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,17 @@ def __init__(

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.
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
Copy link
Member

Choose a reason for hiding this comment

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

@attack68 i think this is causing the mypy failures on CI

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will take a look


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