Skip to content

DEPR: Styler.render in favour of Styler.to_html #42140

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 50 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
be15967
add sparsify to to_html
attack68 Jun 11, 2021
c187dc2
add tests
attack68 Jun 11, 2021
c53040e
doc fix
attack68 Jun 11, 2021
44cca16
Merge remote-tracking branch 'upstream/master' into styler_to_html_sp…
attack68 Jun 12, 2021
6427b88
Merge branch 'rls1.3.0' into styler_to_html_sparse_args
attack68 Jun 15, 2021
6bf29f8
Merge remote-tracking branch 'upstream/master' into styler_to_html_sp…
attack68 Jun 18, 2021
93d78e6
deprecate styler.render() and amend tests
attack68 Jun 20, 2021
617e8d8
more cases to change
attack68 Jun 20, 2021
a2c6ad3
pytest
attack68 Jun 20, 2021
2b166b4
Merge remote-tracking branch 'upstream/master' into styler_to_html_sp…
attack68 Jun 20, 2021
7815f10
Merge branch 'styler_to_html_sparse_args' into styler_depr_render_after
attack68 Jun 20, 2021
ad4cee6
Merge remote-tracking branch 'upstream/master' into styler_to_html_sp…
attack68 Jun 29, 2021
5533f18
Merge branch 'styler_to_html_sparse_args' into styler_depr_render_after
attack68 Jun 29, 2021
3889890
Merge remote-tracking branch 'upstream/master' into styler_to_html_sp…
attack68 Jul 3, 2021
1731643
Merge branch 'styler_to_html_sparse_args' into styler_depr_render_after
attack68 Jul 3, 2021
60053ab
Merge remote-tracking branch 'upstream/master' into styler_to_html_sp…
attack68 Jul 5, 2021
512af50
whats new 1.4.0
attack68 Jul 5, 2021
90d2a32
Merge branch 'styler_to_html_sparse_args' into styler_depr_render_after
attack68 Jul 5, 2021
444d9f8
whats new 1.4.0
attack68 Jul 5, 2021
4e89e64
better test
attack68 Jul 5, 2021
13090ab
better test
attack68 Jul 5, 2021
340f641
Merge branch 'styler_to_html_sparse_args' into styler_depr_render_after
attack68 Jul 5, 2021
38559f5
versionadded
attack68 Jul 5, 2021
857a964
Merge branch 'styler_to_html_sparse_args' into styler_depr_render_after
attack68 Jul 5, 2021
5ba2db9
Merge remote-tracking branch 'upstream/master' into styler_to_html_sp…
attack68 Jul 8, 2021
03f0a18
Merge branch 'styler_to_html_sparse_args' into styler_depr_render_after
attack68 Jul 8, 2021
2ca6207
Merge remote-tracking branch 'upstream/master' into styler_to_html_sp…
attack68 Jul 10, 2021
6739922
Merge branch 'styler_to_html_sparse_args' into styler_depr_render_after
attack68 Jul 10, 2021
855e886
Merge remote-tracking branch 'upstream/master' into styler_to_html_sp…
attack68 Jul 13, 2021
7b0ae0b
Merge branch 'styler_to_html_sparse_args' into styler_depr_render_after
attack68 Jul 13, 2021
e796368
Merge remote-tracking branch 'upstream/master' into styler_to_html_sp…
attack68 Jul 14, 2021
54ea001
Merge branch 'styler_to_html_sparse_args' into styler_depr_render_after
attack68 Jul 14, 2021
1770300
Merge remote-tracking branch 'upstream/master' into styler_to_html_sp…
attack68 Jul 20, 2021
275a37a
Merge branch 'styler_to_html_sparse_args' into styler_depr_render_after
attack68 Jul 20, 2021
ad1f85d
Merge remote-tracking branch 'upstream/master' into styler_to_html_sp…
attack68 Jul 24, 2021
31c46ea
Merge branch 'styler_to_html_sparse_args' into styler_depr_render_after
attack68 Jul 24, 2021
71c9dae
Merge branch 'master' into styler_to_html_sparse_args
jreback Jul 28, 2021
cadcc3e
Merge remote-tracking branch 'upstream/master' into styler_to_html_sp…
attack68 Jul 28, 2021
0fcb800
Merge branch 'styler_to_html_sparse_args' into styler_depr_render_after
attack68 Jul 28, 2021
0f86ae5
Merge remote-tracking branch 'upstream/master' into styler_depr_rende…
attack68 Jul 29, 2021
14b2fb4
update user guide
attack68 Jul 29, 2021
770a545
whats new 1.4.0
attack68 Jul 29, 2021
2c1ca34
doc update
attack68 Jul 29, 2021
cb19785
Merge remote-tracking branch 'upstream/master' into styler_depr_rende…
attack68 Aug 1, 2021
693d2c5
Merge remote-tracking branch 'upstream/master' into styler_depr_rende…
attack68 Aug 5, 2021
b6bcb3b
Merge remote-tracking branch 'upstream/master' into styler_depr_rende…
attack68 Aug 5, 2021
fdb630c
Merge remote-tracking branch 'upstream/master' into styler_depr_rende…
attack68 Aug 6, 2021
8ce0afc
Merge remote-tracking branch 'upstream/master' into styler_depr_rende…
attack68 Aug 9, 2021
3b95422
Merge remote-tracking branch 'upstream/master' into styler_depr_rende…
attack68 Aug 11, 2021
646fb22
Merge remote-tracking branch 'upstream/master' into styler_depr_rende…
attack68 Aug 14, 2021
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
4 changes: 2 additions & 2 deletions asv_bench/benchmarks/io/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ def peakmem_classes_render(self, cols, rows):

def time_format_render(self, cols, rows):
self._style_format()
self.st.render()
self.st._render_html(True, True)

def peakmem_format_render(self, cols, rows):
self._style_format()
self.st.render()
self.st._render_html(True, True)

def _style_apply(self):
def _apply_func(s):
Expand Down
7 changes: 3 additions & 4 deletions doc/source/reference/style.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ Style export and import
.. autosummary::
:toctree: api/

Styler.render
Styler.export
Styler.use
Styler.to_html
Styler.to_excel
Styler.to_latex
Styler.to_excel
Styler.export
Styler.use
24 changes: 12 additions & 12 deletions doc/source/user_guide/style.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The above output looks very similar to the standard DataFrame HTML representation. But the HTML here has already attached some CSS classes to each cell, even if we haven't yet created any styles. We can view these by calling the [.render()][render] method, which returns the raw HTML as string, which is useful for further processing or adding to a file - read on in [More about CSS and HTML](#More-About-CSS-and-HTML). Below we will show how we can use these to format the DataFrame to be more communicative. For example how we can build `s`:\n",
"The above output looks very similar to the standard DataFrame HTML representation. But the HTML here has already attached some CSS classes to each cell, even if we haven't yet created any styles. We can view these by calling the [.to_html()][to_html] method, which returns the raw HTML as string, which is useful for further processing or adding to a file - read on in [More about CSS and HTML](#More-About-CSS-and-HTML). Below we will show how we can use these to format the DataFrame to be more communicative. For example how we can build `s`:\n",
"\n",
"[render]: ../reference/api/pandas.io.formats.style.Styler.render.rst"
"[tohtml]: ../reference/api/pandas.io.formats.style.Styler.to_html.rst"
]
},
{
Expand Down Expand Up @@ -381,7 +381,7 @@
"metadata": {},
"outputs": [],
"source": [
"out = s.set_table_attributes('class=\"my-table-cls\"').render()\n",
"out = s.set_table_attributes('class=\"my-table-cls\"').to_html()\n",
"print(out[out.find('<table'):][:109])"
]
},
Expand Down Expand Up @@ -1017,7 +1017,7 @@
" .set_table_styles([{'selector': 'td', 'props': props},\n",
" {'selector': '.col1', 'props': 'color:green;'},\n",
" {'selector': '.level0', 'props': 'color:blue;'}])\\\n",
" .render()\\\n",
" .to_html()\\\n",
" .replace('blank', '')\\\n",
" .replace('data', '')\\\n",
" .replace('level0', 'l0')\\\n",
Expand Down Expand Up @@ -1295,7 +1295,7 @@
" s.name=''\n",
" row += \"<td>{}</td>\".format(s.to_frame().style.hide_index().bar(align=align, \n",
" color=['#d65f5f', '#5fba7d'], \n",
" width=100).render()) #testn['width']\n",
" width=100).to_html()) #testn['width']\n",
" row += '</tr>'\n",
" head += row\n",
" \n",
Expand Down Expand Up @@ -1616,9 +1616,9 @@
"\n",
"The structure of the `id` is `T_uuid_level<k>_row<m>_col<n>` where `level<k>` is used only on headings, and headings will only have either `row<m>` or `col<n>` whichever is needed. By default we've also prepended each row/column identifier with a UUID unique to each DataFrame so that the style from one doesn't collide with the styling from another within the same notebook or page. You can read more about the use of UUIDs in [Optimization](#Optimization).\n",
"\n",
"We can see example of the HTML by calling the [.render()][render] method.\n",
"We can see example of the HTML by calling the [.to_html()][tohtml] method.\n",
"\n",
"[render]: ../reference/api/pandas.io.formats.style.Styler.render.rst"
"[tohtml]: ../reference/api/pandas.io.formats.style.Styler.to_html.rst"
]
},
{
Expand All @@ -1627,7 +1627,7 @@
"metadata": {},
"outputs": [],
"source": [
"print(pd.DataFrame([[1,2],[3,4]], index=['i1', 'i2'], columns=['c1', 'c2']).style.render())"
"print(pd.DataFrame([[1,2],[3,4]], index=['i1', 'i2'], columns=['c1', 'c2']).style.to_html())"
]
},
{
Expand Down Expand Up @@ -1854,7 +1854,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Our custom template accepts a `table_title` keyword. We can provide the value in the `.render` method."
"Our custom template accepts a `table_title` keyword. We can provide the value in the `.to_html` method."
]
},
{
Expand All @@ -1863,7 +1863,7 @@
"metadata": {},
"outputs": [],
"source": [
"HTML(MyStyler(df3).render(table_title=\"Extending Example\"))"
"HTML(MyStyler(df3).to_html(table_title=\"Extending Example\"))"
]
},
{
Expand All @@ -1880,7 +1880,7 @@
"outputs": [],
"source": [
"EasyStyler = Styler.from_custom_template(\"templates\", \"myhtml.tpl\")\n",
"HTML(EasyStyler(df3).render(table_title=\"Another Title\"))"
"HTML(EasyStyler(df3).to_html(table_title=\"Another Title\"))"
]
},
{
Expand Down Expand Up @@ -1990,7 +1990,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.6"
"version": "3.8.7"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ Deprecations
- Deprecated the 'kind' argument in :meth:`Index.get_slice_bound`, :meth:`Index.slice_indexer`, :meth:`Index.slice_locs`; in a future version passing 'kind' will raise (:issue:`42857`)
- Deprecated dropping of nuisance columns in :class:`Rolling`, :class:`Expanding`, and :class:`EWM` aggregations (:issue:`42738`)
- Deprecated :meth:`Index.reindex` with a non-unique index (:issue:`42568`)
-
- Deprecated :meth:`.Styler.render` in favour of :meth:`.Styler.to_html` (:issue:`42140`)

.. ---------------------------------------------------------------------------

Expand Down
34 changes: 25 additions & 9 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class Styler(StylerRenderer):
be applied to the indicated cells.

If using in the Jupyter notebook, Styler has defined a ``_repr_html_``
to automatically render itself. Otherwise call Styler.render to get
to automatically render itself. Otherwise call Styler.to_html to get
the generated HTML.

CSS classes are attached to the generated HTML
Expand Down Expand Up @@ -214,7 +214,7 @@ def _repr_html_(self) -> str:
"""
Hooks into Jupyter notebook rich display system.
"""
return self.render()
return self.to_html()

def render(
self,
Expand All @@ -225,6 +225,8 @@ def render(
"""
Render the ``Styler`` including all applied styles to HTML.

.. deprecated:: 1.4.0

Parameters
----------
sparse_index : bool, optional
Expand All @@ -248,11 +250,14 @@ def render(

Notes
-----
This method is deprecated in favour of ``Styler.to_html``.

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.
which automatically calls ``self.to_html()`` 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
Expand All @@ -266,6 +271,11 @@ def render(
* caption
* table_attributes
"""
warnings.warn(
"this method is deprecated in favour of `Styler.to_html()`",
FutureWarning,
stacklevel=2,
)
if sparse_index is None:
sparse_index = get_option("styler.sparse.index")
if sparse_columns is None:
Expand Down Expand Up @@ -336,7 +346,7 @@ def set_tooltips(
>>> ttips = pd.DataFrame(
... data=[["Min", ""], [np.nan, "Max"]], columns=df.columns, index=df.index
... )
>>> s = df.style.set_tooltips(ttips).render()
>>> s = df.style.set_tooltips(ttips).to_html()

Optionally controlling the tooltip visual display

Expand Down Expand Up @@ -550,7 +560,7 @@ def to_latex(
>>> df = pd.DataFrame([[1,2], [3,4]])
>>> s = df.style.highlight_max(axis=None,
... props='background-color:red; font-weight:bold;')
>>> s.render() # doctest: +SKIP
>>> s.to_html() # doctest: +SKIP

The equivalent using LaTeX only commands is the following:

Expand Down Expand Up @@ -831,6 +841,7 @@ def to_html(
encoding: str | None = None,
doctype_html: bool = False,
exclude_styles: bool = False,
**kwargs,
):
"""
Write Styler to a file, buffer or string in HTML-CSS format.
Expand Down Expand Up @@ -875,6 +886,10 @@ def to_html(
Whether to include the ``<style>`` element and all associated element
``class`` and ``id`` identifiers, or solely the ``<table>`` element without
styling identifiers.
**kwargs
Any additional keyword arguments are passed through to the jinj2
``self.template.render`` process. This is useful when you need to provide
additional variables for a custom template.

Returns
-------
Expand Down Expand Up @@ -903,6 +918,7 @@ def to_html(
exclude_styles=exclude_styles,
encoding=encoding if encoding else "utf-8",
doctype_html=doctype_html,
**kwargs,
)

return save_to_buffer(
Expand Down Expand Up @@ -961,7 +977,7 @@ def set_td_classes(self, classes: DataFrame) -> Styler:
>>> df = pd.DataFrame([[1]])
>>> css = pd.DataFrame([["other-class"]])
>>> s = Styler(df, uuid="_", cell_ids=False).set_td_classes(css)
>>> s.hide_index().render() # doctest: +SKIP
>>> s.hide_index().to_html() # doctest: +SKIP
'<style type="text/css"></style>'
'<table id="T__">'
' <thead>'
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/formats/style_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ def format(
>>> s = df.style.format(
... '<a href="a.com/{0}">{0}</a>', escape="html", na_rep="NA"
... )
>>> s.render() # doctest: +SKIP
>>> s.to_html() # doctest: +SKIP
...
<td .. ><a href="a.com/&lt;div&gt;&lt;/div&gt;">&lt;div&gt;&lt;/div&gt;</a></td>
<td .. ><a href="a.com/&#34;A&amp;B&#34;">&#34;A&amp;B&#34;</a></td>
Expand Down
Loading