diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 33179cbd3a4d2..daa6a5d894191 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -355,7 +355,7 @@ ExtensionArray Styler ^^^^^^ - Minor bug in :class:`.Styler` where the ``uuid`` at initialization maintained a floating underscore (:issue:`43037`) -- +- Bug in :meth:`.Styler.to_html` where the ``Styler`` object was updated if the ``to_html`` method was called with some args (:issue:`43034`) Other ^^^^^ diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 452b478623fa6..eff0bd9637859 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -900,11 +900,13 @@ def to_html( -------- DataFrame.to_html: Write a DataFrame to a file, buffer or string in HTML format. """ + obj = self._copy(deepcopy=True) # manipulate table_styles on obj, not self + if table_uuid: - self.set_uuid(table_uuid) + obj.set_uuid(table_uuid) if table_attributes: - self.set_table_attributes(table_attributes) + obj.set_table_attributes(table_attributes) if sparse_index is None: sparse_index = get_option("styler.sparse.index") @@ -912,7 +914,7 @@ def to_html( sparse_columns = get_option("styler.sparse.columns") # Build HTML string.. - html = self._render_html( + html = obj._render_html( sparse_index=sparse_index, sparse_columns=sparse_columns, exclude_styles=exclude_styles, @@ -1088,6 +1090,10 @@ def _copy(self, deepcopy: bool = False) -> Styler: "caption", "uuid", "uuid_len", + "template_latex", # also copy templates if these have been customised + "template_html_style", + "template_html_table", + "template_html", ] deep = [ # nested lists or dicts "_display_funcs", diff --git a/pandas/tests/io/formats/style/test_html.py b/pandas/tests/io/formats/style/test_html.py index 0087090feafce..777afe0ff0da4 100644 --- a/pandas/tests/io/formats/style/test_html.py +++ b/pandas/tests/io/formats/style/test_html.py @@ -260,7 +260,7 @@ def test_from_custom_template_style(tmpdir): assert result.env is not Styler.env assert result.template_html_style is not Styler.template_html_style styler = result(DataFrame({"A": [1, 2]})) - assert '\n\n\n\n