Skip to content

Commit 7376b36

Browse files
authored
DEPR: Styler.render in favour of Styler.to_html (#42140)
1 parent b6c6e5a commit 7376b36

File tree

12 files changed

+252
-202
lines changed

12 files changed

+252
-202
lines changed

asv_bench/benchmarks/io/style.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ def peakmem_classes_render(self, cols, rows):
3636

3737
def time_format_render(self, cols, rows):
3838
self._style_format()
39-
self.st.render()
39+
self.st._render_html(True, True)
4040

4141
def peakmem_format_render(self, cols, rows):
4242
self._style_format()
43-
self.st.render()
43+
self.st._render_html(True, True)
4444

4545
def _style_apply(self):
4646
def _apply_func(s):

doc/source/reference/style.rst

+3-4
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,8 @@ Style export and import
7171
.. autosummary::
7272
:toctree: api/
7373

74-
Styler.render
75-
Styler.export
76-
Styler.use
7774
Styler.to_html
78-
Styler.to_excel
7975
Styler.to_latex
76+
Styler.to_excel
77+
Styler.export
78+
Styler.use

doc/source/user_guide/style.ipynb

+12-12
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@
6060
"cell_type": "markdown",
6161
"metadata": {},
6262
"source": [
63-
"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",
63+
"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",
6464
"\n",
65-
"[render]: ../reference/api/pandas.io.formats.style.Styler.render.rst"
65+
"[tohtml]: ../reference/api/pandas.io.formats.style.Styler.to_html.rst"
6666
]
6767
},
6868
{
@@ -381,7 +381,7 @@
381381
"metadata": {},
382382
"outputs": [],
383383
"source": [
384-
"out = s.set_table_attributes('class=\"my-table-cls\"').render()\n",
384+
"out = s.set_table_attributes('class=\"my-table-cls\"').to_html()\n",
385385
"print(out[out.find('<table'):][:109])"
386386
]
387387
},
@@ -1017,7 +1017,7 @@
10171017
" .set_table_styles([{'selector': 'td', 'props': props},\n",
10181018
" {'selector': '.col1', 'props': 'color:green;'},\n",
10191019
" {'selector': '.level0', 'props': 'color:blue;'}])\\\n",
1020-
" .render()\\\n",
1020+
" .to_html()\\\n",
10211021
" .replace('blank', '')\\\n",
10221022
" .replace('data', '')\\\n",
10231023
" .replace('level0', 'l0')\\\n",
@@ -1295,7 +1295,7 @@
12951295
" s.name=''\n",
12961296
" row += \"<td>{}</td>\".format(s.to_frame().style.hide_index().bar(align=align, \n",
12971297
" color=['#d65f5f', '#5fba7d'], \n",
1298-
" width=100).render()) #testn['width']\n",
1298+
" width=100).to_html()) #testn['width']\n",
12991299
" row += '</tr>'\n",
13001300
" head += row\n",
13011301
" \n",
@@ -1616,9 +1616,9 @@
16161616
"\n",
16171617
"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",
16181618
"\n",
1619-
"We can see example of the HTML by calling the [.render()][render] method.\n",
1619+
"We can see example of the HTML by calling the [.to_html()][tohtml] method.\n",
16201620
"\n",
1621-
"[render]: ../reference/api/pandas.io.formats.style.Styler.render.rst"
1621+
"[tohtml]: ../reference/api/pandas.io.formats.style.Styler.to_html.rst"
16221622
]
16231623
},
16241624
{
@@ -1627,7 +1627,7 @@
16271627
"metadata": {},
16281628
"outputs": [],
16291629
"source": [
1630-
"print(pd.DataFrame([[1,2],[3,4]], index=['i1', 'i2'], columns=['c1', 'c2']).style.render())"
1630+
"print(pd.DataFrame([[1,2],[3,4]], index=['i1', 'i2'], columns=['c1', 'c2']).style.to_html())"
16311631
]
16321632
},
16331633
{
@@ -1854,7 +1854,7 @@
18541854
"cell_type": "markdown",
18551855
"metadata": {},
18561856
"source": [
1857-
"Our custom template accepts a `table_title` keyword. We can provide the value in the `.render` method."
1857+
"Our custom template accepts a `table_title` keyword. We can provide the value in the `.to_html` method."
18581858
]
18591859
},
18601860
{
@@ -1863,7 +1863,7 @@
18631863
"metadata": {},
18641864
"outputs": [],
18651865
"source": [
1866-
"HTML(MyStyler(df3).render(table_title=\"Extending Example\"))"
1866+
"HTML(MyStyler(df3).to_html(table_title=\"Extending Example\"))"
18671867
]
18681868
},
18691869
{
@@ -1880,7 +1880,7 @@
18801880
"outputs": [],
18811881
"source": [
18821882
"EasyStyler = Styler.from_custom_template(\"templates\", \"myhtml.tpl\")\n",
1883-
"HTML(EasyStyler(df3).render(table_title=\"Another Title\"))"
1883+
"HTML(EasyStyler(df3).to_html(table_title=\"Another Title\"))"
18841884
]
18851885
},
18861886
{
@@ -1990,7 +1990,7 @@
19901990
"name": "python",
19911991
"nbconvert_exporter": "python",
19921992
"pygments_lexer": "ipython3",
1993-
"version": "3.8.6"
1993+
"version": "3.8.7"
19941994
}
19951995
},
19961996
"nbformat": 4,

doc/source/whatsnew/v1.4.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ Deprecations
208208
- 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`)
209209
- Deprecated dropping of nuisance columns in :class:`Rolling`, :class:`Expanding`, and :class:`EWM` aggregations (:issue:`42738`)
210210
- Deprecated :meth:`Index.reindex` with a non-unique index (:issue:`42568`)
211-
-
211+
- Deprecated :meth:`.Styler.render` in favour of :meth:`.Styler.to_html` (:issue:`42140`)
212212

213213
.. ---------------------------------------------------------------------------
214214

pandas/io/formats/style.py

+25-9
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class Styler(StylerRenderer):
151151
be applied to the indicated cells.
152152
153153
If using in the Jupyter notebook, Styler has defined a ``_repr_html_``
154-
to automatically render itself. Otherwise call Styler.render to get
154+
to automatically render itself. Otherwise call Styler.to_html to get
155155
the generated HTML.
156156
157157
CSS classes are attached to the generated HTML
@@ -214,7 +214,7 @@ def _repr_html_(self) -> str:
214214
"""
215215
Hooks into Jupyter notebook rich display system.
216216
"""
217-
return self.render()
217+
return self.to_html()
218218

219219
def render(
220220
self,
@@ -225,6 +225,8 @@ def render(
225225
"""
226226
Render the ``Styler`` including all applied styles to HTML.
227227
228+
.. deprecated:: 1.4.0
229+
228230
Parameters
229231
----------
230232
sparse_index : bool, optional
@@ -248,11 +250,14 @@ def render(
248250
249251
Notes
250252
-----
253+
This method is deprecated in favour of ``Styler.to_html``.
254+
251255
Styler objects have defined the ``_repr_html_`` method
252-
which automatically calls ``self.render()`` when it's the
253-
last item in a Notebook cell. When calling ``Styler.render()``
254-
directly, wrap the result in ``IPython.display.HTML`` to view
255-
the rendered HTML in the notebook.
256+
which automatically calls ``self.to_html()`` when it's the
257+
last item in a Notebook cell.
258+
259+
When calling ``Styler.render()`` directly, wrap the result in
260+
``IPython.display.HTML`` to view the rendered HTML in the notebook.
256261
257262
Pandas uses the following keys in render. Arguments passed
258263
in ``**kwargs`` take precedence, so think carefully if you want
@@ -266,6 +271,11 @@ def render(
266271
* caption
267272
* table_attributes
268273
"""
274+
warnings.warn(
275+
"this method is deprecated in favour of `Styler.to_html()`",
276+
FutureWarning,
277+
stacklevel=2,
278+
)
269279
if sparse_index is None:
270280
sparse_index = get_option("styler.sparse.index")
271281
if sparse_columns is None:
@@ -336,7 +346,7 @@ def set_tooltips(
336346
>>> ttips = pd.DataFrame(
337347
... data=[["Min", ""], [np.nan, "Max"]], columns=df.columns, index=df.index
338348
... )
339-
>>> s = df.style.set_tooltips(ttips).render()
349+
>>> s = df.style.set_tooltips(ttips).to_html()
340350
341351
Optionally controlling the tooltip visual display
342352
@@ -550,7 +560,7 @@ def to_latex(
550560
>>> df = pd.DataFrame([[1,2], [3,4]])
551561
>>> s = df.style.highlight_max(axis=None,
552562
... props='background-color:red; font-weight:bold;')
553-
>>> s.render() # doctest: +SKIP
563+
>>> s.to_html() # doctest: +SKIP
554564
555565
The equivalent using LaTeX only commands is the following:
556566
@@ -831,6 +841,7 @@ def to_html(
831841
encoding: str | None = None,
832842
doctype_html: bool = False,
833843
exclude_styles: bool = False,
844+
**kwargs,
834845
):
835846
"""
836847
Write Styler to a file, buffer or string in HTML-CSS format.
@@ -875,6 +886,10 @@ def to_html(
875886
Whether to include the ``<style>`` element and all associated element
876887
``class`` and ``id`` identifiers, or solely the ``<table>`` element without
877888
styling identifiers.
889+
**kwargs
890+
Any additional keyword arguments are passed through to the jinj2
891+
``self.template.render`` process. This is useful when you need to provide
892+
additional variables for a custom template.
878893
879894
Returns
880895
-------
@@ -903,6 +918,7 @@ def to_html(
903918
exclude_styles=exclude_styles,
904919
encoding=encoding if encoding else "utf-8",
905920
doctype_html=doctype_html,
921+
**kwargs,
906922
)
907923

908924
return save_to_buffer(
@@ -961,7 +977,7 @@ def set_td_classes(self, classes: DataFrame) -> Styler:
961977
>>> df = pd.DataFrame([[1]])
962978
>>> css = pd.DataFrame([["other-class"]])
963979
>>> s = Styler(df, uuid="_", cell_ids=False).set_td_classes(css)
964-
>>> s.hide_index().render() # doctest: +SKIP
980+
>>> s.hide_index().to_html() # doctest: +SKIP
965981
'<style type="text/css"></style>'
966982
'<table id="T__">'
967983
' <thead>'

pandas/io/formats/style_render.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ def format(
735735
>>> s = df.style.format(
736736
... '<a href="a.com/{0}">{0}</a>', escape="html", na_rep="NA"
737737
... )
738-
>>> s.render() # doctest: +SKIP
738+
>>> s.to_html() # doctest: +SKIP
739739
...
740740
<td .. ><a href="a.com/&lt;div&gt;&lt;/div&gt;">&lt;div&gt;&lt;/div&gt;</a></td>
741741
<td .. ><a href="a.com/&#34;A&amp;B&#34;">&#34;A&amp;B&#34;</a></td>

0 commit comments

Comments
 (0)