Skip to content

Commit f1cf3e1

Browse files
attack68feefladder
authored andcommitted
ENH: styler.render.encoding option (pandas-dev#43177)
1 parent e4cb663 commit f1cf3e1

File tree

5 files changed

+28
-7
lines changed

5 files changed

+28
-7
lines changed

doc/source/user_guide/options.rst

+1
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ styler.sparse.columns True "Sparsify" MultiIndex displ
489489
in Styler output.
490490
styler.render.max_elements 262144 Maximum number of datapoints that Styler will render
491491
trimming either rows, columns or both to fit.
492+
styler.render.encoding utf-8 Default encoding for output HTML or LaTeX files.
492493
styler.format.formatter None Object to specify formatting functions to ``Styler.format``.
493494
styler.format.na_rep None String representation for missing data.
494495
styler.format.precision 6 Precision to display floating point and complex numbers.

doc/source/whatsnew/v1.4.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Styler
7575
- :meth:`.Styler.to_latex` introduces keyword argument ``environment``, which also allows a specific "longtable" entry through a separate jinja2 template (:issue:`41866`).
7676
- :meth:`.Styler.to_html` introduces keyword arguments ``sparse_index`` and ``sparse_columns`` (:issue:`41946`)
7777
- 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`)
78-
- Global options have been extended to configure default ``Styler`` properties including formatting options (:issue:`41395`)
78+
- Global options have been extended to configure default ``Styler`` properties including formatting and encoding options (:issue:`41395`)
7979

8080
There are also bug fixes and deprecations listed below.
8181

pandas/core/config_init.py

+7
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,11 @@ def register_converter_cb(key):
793793
A formatter object to be used as default within ``Styler.format``.
794794
"""
795795

796+
styler_encoding = """
797+
: str
798+
The encoding used for output HTML and LaTeX files.
799+
"""
800+
796801
with cf.config_prefix("styler"):
797802
cf.register_option("sparse.index", True, styler_sparse_index_doc, validator=bool)
798803

@@ -807,6 +812,8 @@ def register_converter_cb(key):
807812
validator=is_nonnegative_int,
808813
)
809814

815+
cf.register_option("render.encoding", "utf-8", styler_encoding, validator=is_str)
816+
810817
cf.register_option("format.decimal", ".", styler_decimal, validator=is_str)
811818

812819
cf.register_option(

pandas/io/formats/style.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,9 @@ def to_latex(
526526
rendered.
527527
528528
.. versionadded:: 1.4.0
529-
encoding : str, default "utf-8"
530-
Character encoding setting.
529+
encoding : str, optional
530+
Character encoding setting. Defaults
531+
to ``pandas.options.styler.render.encoding`` value of "utf-8".
531532
convert_css : bool, default False
532533
Convert simple cell-styles from CSS to LaTeX format. Any CSS not found in
533534
conversion table is dropped. A style can be forced by adding option
@@ -849,7 +850,10 @@ def to_latex(
849850
convert_css=convert_css,
850851
)
851852

852-
return save_to_buffer(latex, buf=buf, encoding=encoding)
853+
encoding = encoding or get_option("styler.render.encoding")
854+
return save_to_buffer(
855+
latex, buf=buf, encoding=None if buf is None else encoding
856+
)
853857

854858
def to_html(
855859
self,
@@ -898,8 +902,8 @@ def to_html(
898902
899903
.. versionadded:: 1.4.0
900904
encoding : str, optional
901-
Character encoding setting for file output, and HTML meta tags,
902-
defaults to "utf-8" if None.
905+
Character encoding setting for file output, and HTML meta tags.
906+
Defaults to ``pandas.options.styler.render.encoding`` value of "utf-8".
903907
doctype_html : bool, default False
904908
Whether to output a fully structured HTML file including all
905909
HTML elements, or just the core ``<style>`` and ``<table>`` elements.
@@ -934,12 +938,13 @@ def to_html(
934938
if sparse_columns is None:
935939
sparse_columns = get_option("styler.sparse.columns")
936940

941+
encoding = encoding or get_option("styler.render.encoding")
937942
# Build HTML string..
938943
html = obj._render_html(
939944
sparse_index=sparse_index,
940945
sparse_columns=sparse_columns,
941946
exclude_styles=exclude_styles,
942-
encoding=encoding if encoding else "utf-8",
947+
encoding=encoding,
943948
doctype_html=doctype_html,
944949
**kwargs,
945950
)

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

+8
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,14 @@ def test_doctype(styler):
191191
assert "<head>" not in result
192192

193193

194+
def test_doctype_encoding(styler):
195+
with option_context("styler.render.encoding", "ASCII"):
196+
result = styler.to_html(doctype_html=True)
197+
assert '<meta charset="ASCII">' in result
198+
result = styler.to_html(doctype_html=True, encoding="ANSI")
199+
assert '<meta charset="ANSI">' in result
200+
201+
194202
def test_block_names(tpl_style, tpl_table):
195203
# catch accidental removal of a block
196204
expected_style = {

0 commit comments

Comments
 (0)