Skip to content

Commit 59de03a

Browse files
Backport PR #47059 on branch 1.4.x (REGR: Styler buf and encoding in to_latex and to_html) (#47072)
1 parent 806faba commit 59de03a

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

pandas/io/formats/style.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -1049,10 +1049,12 @@ def to_latex(
10491049
clines=clines,
10501050
)
10511051

1052-
encoding = encoding or get_option("styler.render.encoding")
1053-
return save_to_buffer(
1054-
latex, buf=buf, encoding=None if buf is None else encoding
1052+
encoding = (
1053+
(encoding or get_option("styler.render.encoding"))
1054+
if isinstance(buf, str) # i.e. a filepath
1055+
else encoding
10551056
)
1057+
return save_to_buffer(latex, buf=buf, encoding=encoding)
10561058

10571059
def to_html(
10581060
self,
@@ -1173,15 +1175,14 @@ def to_html(
11731175
if caption is not None:
11741176
obj.set_caption(caption)
11751177

1176-
encoding = encoding or get_option("styler.render.encoding")
11771178
# Build HTML string..
11781179
html = obj._render_html(
11791180
sparse_index=sparse_index,
11801181
sparse_columns=sparse_columns,
11811182
max_rows=max_rows,
11821183
max_cols=max_columns,
11831184
exclude_styles=exclude_styles,
1184-
encoding=encoding,
1185+
encoding=encoding or get_option("styler.render.encoding"),
11851186
doctype_html=doctype_html,
11861187
**kwargs,
11871188
)

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

+7
Original file line numberDiff line numberDiff line change
@@ -1547,3 +1547,10 @@ def test_col_trimming_hide_columns():
15471547
assert ctx["head"][0][c + 2]["is_visible"] == vals[1]
15481548

15491549
assert len(ctx["body"][0]) == 6 # index + 2 hidden + 2 visible + trimming col
1550+
1551+
1552+
@pytest.mark.parametrize("format", ["html", "latex"])
1553+
def test_output_buffer(mi_styler, format):
1554+
# gh 47053
1555+
with open(f"delete_me.{format}", "w") as f:
1556+
getattr(mi_styler, f"to_{format}")(f)

0 commit comments

Comments
 (0)