From 3f62bb493f8b99e67e974a42d7e10b6806323aa7 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Fri, 23 Apr 2021 15:22:58 +0200 Subject: [PATCH] rename the render process in Styler --- asv_bench/benchmarks/io/style.py | 8 +++--- pandas/io/formats/style.py | 41 ++++++++++++++++++++++++++++++- pandas/io/formats/style_render.py | 38 +++------------------------- 3 files changed, 47 insertions(+), 40 deletions(-) diff --git a/asv_bench/benchmarks/io/style.py b/asv_bench/benchmarks/io/style.py index 6c0ca6fac6ec3..e4369d67ca67e 100644 --- a/asv_bench/benchmarks/io/style.py +++ b/asv_bench/benchmarks/io/style.py @@ -17,19 +17,19 @@ def setup(self, cols, rows): def time_apply_render(self, cols, rows): self._style_apply() - self.st.render() + self.st._render_html() def peakmem_apply_render(self, cols, rows): self._style_apply() - self.st.render() + self.st._render_html() def time_classes_render(self, cols, rows): self._style_classes() - self.st.render() + self.st._render_html() def peakmem_classes_render(self, cols, rows): self._style_classes() - self.st.render() + self.st._render_html() def _style_apply(self): def _apply_func(s): diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index ff25bb1411189..e5f28149eaac7 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -200,7 +200,46 @@ def _repr_html_(self) -> str: """ Hooks into Jupyter notebook rich display system. """ - return self.render() + return self._render_html() + + def render(self, **kwargs) -> str: + """ + Render the ``Styler`` including all applied styles to HTML. + + Parameters + ---------- + **kwargs + Any additional keyword arguments are passed + through to ``self.template.render``. + This is useful when you need to provide + additional variables for a custom template. + + Returns + ------- + rendered : str + The rendered HTML. + + Notes + ----- + Styler objects have defined the ``_repr_html_`` method + which automatically calls ``self.render()`` when it's the + last item in a Notebook cell. When calling ``Styler.render()`` + directly, wrap the result in ``IPython.display.HTML`` to view + the rendered HTML in the notebook. + + Pandas uses the following keys in render. Arguments passed + in ``**kwargs`` take precedence, so think carefully if you want + to override them: + + * head + * cellstyle + * body + * uuid + * table_styles + * caption + * table_attributes + """ + return self._render_html(**kwargs) def set_tooltips( self, diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index 15557c993eab4..24107bb130f4c 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -102,42 +102,10 @@ def __init__( tuple[int, int], Callable[[Any], str] ] = defaultdict(lambda: partial(_default_formatter, precision=def_precision)) - def render(self, **kwargs) -> str: + def _render_html(self, **kwargs) -> str: """ - Render the ``Styler`` including all applied styles to HTML. - - Parameters - ---------- - **kwargs - Any additional keyword arguments are passed - through to ``self.template.render``. - This is useful when you need to provide - additional variables for a custom template. - - Returns - ------- - rendered : str - The rendered HTML. - - Notes - ----- - Styler objects have defined the ``_repr_html_`` method - which automatically calls ``self.render()`` when it's the - last item in a Notebook cell. When calling ``Styler.render()`` - directly, wrap the result in ``IPython.display.HTML`` to view - the rendered HTML in the notebook. - - Pandas uses the following keys in render. Arguments passed - in ``**kwargs`` take precedence, so think carefully if you want - to override them: - - * head - * cellstyle - * body - * uuid - * table_styles - * caption - * table_attributes + Renders the ``Styler`` including all applied styles to HTML. + Generates a dict with necessary kwargs passed to jinja2 template. """ self._compute() # TODO: namespace all the pandas keys