Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions doc/source/user_guide/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ styler.sparse.columns True "Sparsify" MultiIndex displ
in Styler output.
styler.render.max_elements 262144 Maximum number of datapoints that Styler will render
trimming either rows, columns or both to fit.
styler.render.encoding utf-8 Default encoding for output HTML or LaTeX files.
======================================= ============ ==================================


Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Styler
- :meth:`.Styler.to_latex` introduces keyword argument ``environment``, which also allows a specific "longtable" entry through a separate jinja2 template (:issue:`41866`).
- :meth:`.Styler.to_html` introduces keyword arguments ``sparse_index`` and ``sparse_columns`` (:issue:`41946`)
- Keyword argument ``level`` is added to :meth:`.Styler.hide_index` and :meth:`.Styler.hide_columns` for optionally controlling hidden levels in a MultiIndex (:issue:`25475`)
- Global options have been extended to configure default ``Styler`` properties including encoding options (:issue:`41395`)

There are also bug fixes and deprecations listed below.

Expand Down
8 changes: 8 additions & 0 deletions pandas/core/config_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
is_int,
is_nonnegative_int,
is_one_of_factory,
is_str,
is_text,
)

Expand Down Expand Up @@ -762,6 +763,11 @@ def register_converter_cb(key):
trimming will occur over columns, rows or both if needed.
"""

styler_encoding = """
: str
The encoding used for output HTML and LaTeX files.
"""

with cf.config_prefix("styler"):
cf.register_option("sparse.index", True, styler_sparse_index_doc, validator=bool)

Expand All @@ -775,3 +781,5 @@ def register_converter_cb(key):
styler_max_elements,
validator=is_nonnegative_int,
)

cf.register_option("render.encoding", "utf-8", styler_encoding, validator=is_str)
17 changes: 11 additions & 6 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,9 @@ def to_latex(
rendered.

.. versionadded:: 1.4.0
encoding : str, default "utf-8"
Character encoding setting.
encoding : str, optional
Character encoding setting. Defaults
to ``pandas.options.styler.render.encoding`` value.
convert_css : bool, default False
Convert simple cell-styles from CSS to LaTeX format. Any CSS not found in
conversion table is dropped. A style can be forced by adding option
Expand Down Expand Up @@ -828,7 +829,10 @@ def to_latex(
convert_css=convert_css,
)

return save_to_buffer(latex, buf=buf, encoding=encoding)
encoding = encoding or get_option("styler.render.encoding")
return save_to_buffer(
latex, buf=buf, encoding=None if buf is None else encoding
)

def to_html(
self,
Expand Down Expand Up @@ -877,8 +881,8 @@ def to_html(

.. versionadded:: 1.4.0
encoding : str, optional
Character encoding setting for file output, and HTML meta tags,
defaults to "utf-8" if None.
Character encoding setting for file output, and HTML meta tags.
Defaults to ``pandas.options.styler.render.encoding`` value.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would still say the default is utf8 (from the option)

doctype_html : bool, default False
Whether to output a fully structured HTML file including all
HTML elements, or just the core ``<style>`` and ``<table>`` elements.
Expand Down Expand Up @@ -913,12 +917,13 @@ def to_html(
if sparse_columns is None:
sparse_columns = get_option("styler.sparse.columns")

encoding = encoding or get_option("styler.render.encoding")
# Build HTML string..
html = obj._render_html(
sparse_index=sparse_index,
sparse_columns=sparse_columns,
exclude_styles=exclude_styles,
encoding=encoding if encoding else "utf-8",
encoding=encoding,
doctype_html=doctype_html,
**kwargs,
)
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/io/formats/style/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ def test_doctype(styler):
assert "<head>" not in result


def test_doctype_encoding(styler):
with option_context("styler.render.encoding", "ASCII"):
result = styler.to_html(doctype_html=True)
assert '<meta charset="ASCII">' in result
result = styler.to_html(doctype_html=True, encoding="ANSI")
assert '<meta charset="ANSI">' in result


def test_block_names(tpl_style, tpl_table):
# catch accidental removal of a block
expected_style = {
Expand Down