Skip to content

Commit c0de83b

Browse files
authored
BUG: don't manipulate styler object due to args in to_html (#43034)
1 parent 3af1a4f commit c0de83b

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

doc/source/whatsnew/v1.4.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ ExtensionArray
355355
Styler
356356
^^^^^^
357357
- Minor bug in :class:`.Styler` where the ``uuid`` at initialization maintained a floating underscore (:issue:`43037`)
358-
-
358+
- Bug in :meth:`.Styler.to_html` where the ``Styler`` object was updated if the ``to_html`` method was called with some args (:issue:`43034`)
359359

360360
Other
361361
^^^^^

pandas/io/formats/style.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -900,19 +900,21 @@ def to_html(
900900
--------
901901
DataFrame.to_html: Write a DataFrame to a file, buffer or string in HTML format.
902902
"""
903+
obj = self._copy(deepcopy=True) # manipulate table_styles on obj, not self
904+
903905
if table_uuid:
904-
self.set_uuid(table_uuid)
906+
obj.set_uuid(table_uuid)
905907

906908
if table_attributes:
907-
self.set_table_attributes(table_attributes)
909+
obj.set_table_attributes(table_attributes)
908910

909911
if sparse_index is None:
910912
sparse_index = get_option("styler.sparse.index")
911913
if sparse_columns is None:
912914
sparse_columns = get_option("styler.sparse.columns")
913915

914916
# Build HTML string..
915-
html = self._render_html(
917+
html = obj._render_html(
916918
sparse_index=sparse_index,
917919
sparse_columns=sparse_columns,
918920
exclude_styles=exclude_styles,
@@ -1088,6 +1090,10 @@ def _copy(self, deepcopy: bool = False) -> Styler:
10881090
"caption",
10891091
"uuid",
10901092
"uuid_len",
1093+
"template_latex", # also copy templates if these have been customised
1094+
"template_html_style",
1095+
"template_html_table",
1096+
"template_html",
10911097
]
10921098
deep = [ # nested lists or dicts
10931099
"_display_funcs",

pandas/tests/io/formats/style/test_html.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ def test_from_custom_template_style(tmpdir):
260260
assert result.env is not Styler.env
261261
assert result.template_html_style is not Styler.template_html_style
262262
styler = result(DataFrame({"A": [1, 2]}))
263-
assert '<link rel="stylesheet" href="mystyle.css">\n\n<style' in styler.render()
263+
assert '<link rel="stylesheet" href="mystyle.css">\n\n<style' in styler.to_html()
264264

265265

266266
def test_caption_as_sequence(styler):

pandas/tests/io/formats/style/test_style.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def mi_styler(mi_df):
4242
def mi_styler_comp(mi_styler):
4343
# comprehensively add features to mi_styler
4444
mi_styler.uuid_len = 5
45-
mi_styler.uuid = "abcde_"
45+
mi_styler.uuid = "abcde"
4646
mi_styler.set_caption("capt")
4747
mi_styler.set_table_styles([{"selector": "a", "props": "a:v;"}])
4848
mi_styler.hide_columns()

0 commit comments

Comments
 (0)