Skip to content
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

ENH: styler.render.encoding option #43177

Merged
merged 6 commits into from
Sep 1, 2021
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
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.
styler.format.formatter None Object to specify formatting functions to ``Styler.format``.
styler.format.na_rep None String representation for missing data.
styler.format.precision 6 Precision to display floating point and complex numbers.
Expand Down
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +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 formatting options (:issue:`41395`)
- Global options have been extended to configure default ``Styler`` properties including formatting and encoding options (:issue:`41395`)

There are also bug fixes and deprecations listed below.

Expand Down
7 changes: 7 additions & 0 deletions pandas/core/config_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,11 @@ def register_converter_cb(key):
A formatter object to be used as default within ``Styler.format``.
"""

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 @@ -807,6 +812,8 @@ def register_converter_cb(key):
validator=is_nonnegative_int,
)

cf.register_option("render.encoding", "utf-8", styler_encoding, validator=is_str)

cf.register_option("format.decimal", ".", styler_decimal, validator=is_str)

cf.register_option(
Expand Down
17 changes: 11 additions & 6 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,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 of "utf-8".
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 @@ -849,7 +850,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 @@ -898,8 +902,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 of "utf-8".
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 @@ -934,12 +938,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