From d1b0f8926d0e7284ae228f23d26f17fe4724ff9d Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Mon, 23 Aug 2021 15:19:04 +0200 Subject: [PATCH 1/4] add styler encoding option --- doc/source/user_guide/options.rst | 1 + pandas/core/config_init.py | 8 ++++++++ pandas/io/formats/style.py | 13 ++++++++----- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/doc/source/user_guide/options.rst b/doc/source/user_guide/options.rst index 62a347acdaa34..8564777b09533 100644 --- a/doc/source/user_guide/options.rst +++ b/doc/source/user_guide/options.rst @@ -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. ======================================= ============ ================================== diff --git a/pandas/core/config_init.py b/pandas/core/config_init.py index 27b898782fbef..50c9772df3559 100644 --- a/pandas/core/config_init.py +++ b/pandas/core/config_init.py @@ -20,6 +20,7 @@ is_int, is_nonnegative_int, is_one_of_factory, + is_str, is_text, ) @@ -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) @@ -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) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 81bd14629cfd3..d9c923f92561e 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -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 @@ -828,6 +829,7 @@ def to_latex( convert_css=convert_css, ) + encoding = encoding or get_option("styler.render.encoding") return save_to_buffer(latex, buf=buf, encoding=encoding) def to_html( @@ -877,8 +879,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. doctype_html : bool, default False Whether to output a fully structured HTML file including all HTML elements, or just the core ``