Skip to content

Backport PR #47059 on branch 1.4.x (REGR: Styler buf and encoding in to_latex and to_html) #47072

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
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 6 additions & 5 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -1049,10 +1049,12 @@ def to_latex(
clines=clines,
)

encoding = encoding or get_option("styler.render.encoding")
return save_to_buffer(
latex, buf=buf, encoding=None if buf is None else encoding
encoding = (
(encoding or get_option("styler.render.encoding"))
if isinstance(buf, str) # i.e. a filepath
else encoding
)
return save_to_buffer(latex, buf=buf, encoding=encoding)

def to_html(
self,
Expand Down Expand Up @@ -1173,15 +1175,14 @@ def to_html(
if caption is not None:
obj.set_caption(caption)

encoding = encoding or get_option("styler.render.encoding")
# Build HTML string..
html = obj._render_html(
sparse_index=sparse_index,
sparse_columns=sparse_columns,
max_rows=max_rows,
max_cols=max_columns,
exclude_styles=exclude_styles,
encoding=encoding,
encoding=encoding or get_option("styler.render.encoding"),
doctype_html=doctype_html,
**kwargs,
)
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/io/formats/style/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -1547,3 +1547,10 @@ def test_col_trimming_hide_columns():
assert ctx["head"][0][c + 2]["is_visible"] == vals[1]

assert len(ctx["body"][0]) == 6 # index + 2 hidden + 2 visible + trimming col


@pytest.mark.parametrize("format", ["html", "latex"])
def test_output_buffer(mi_styler, format):
# gh 47053
with open(f"delete_me.{format}", "w") as f:
getattr(mi_styler, f"to_{format}")(f)