From b8938b1685ebcbca8255e5cbc0929d2ef7b43bb2 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sat, 21 Aug 2021 09:35:23 +0200 Subject: [PATCH 1/4] add bold_headers kwarg --- doc/source/whatsnew/v1.4.0.rst | 2 +- pandas/io/formats/style.py | 6 ++++++ pandas/tests/io/formats/style/test_html.py | 7 +++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 205a49e7786a7..d60e103c1bcc4 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -73,7 +73,7 @@ Styler - Styling of indexing has been added, with :meth:`.Styler.apply_index` and :meth:`.Styler.applymap_index`. These mirror the signature of the methods already used to style data values, and work with both HTML and LaTeX format (:issue:`41893`). - :meth:`.Styler.bar` introduces additional arguments to control alignment and display (:issue:`26070`, :issue:`36419`), and it also validates the input arguments ``width`` and ``height`` (:issue:`42511`). - :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`) + - :meth:`.Styler.to_html` introduces keyword arguments ``sparse_index``, ``sparse_columns``, ``bold_headers`` (:issue:`41946`, :issue:`xxxxx`). - 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`) There are also bug fixes and deprecations listed below. diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 81bd14629cfd3..b385b3a5dd556 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -838,6 +838,7 @@ def to_html( table_attributes: str | None = None, sparse_index: bool | None = None, sparse_columns: bool | None = None, + bold_headers: bool = False, encoding: str | None = None, doctype_html: bool = False, exclude_styles: bool = False, @@ -913,6 +914,11 @@ def to_html( if sparse_columns is None: sparse_columns = get_option("styler.sparse.columns") + if bold_headers: + obj.set_table_styles( + [{"selector": "th", "props": "font-weight: bold;"}], overwrite=False + ) + # Build HTML string.. html = obj._render_html( sparse_index=sparse_index, diff --git a/pandas/tests/io/formats/style/test_html.py b/pandas/tests/io/formats/style/test_html.py index 777afe0ff0da4..958c82a20a591 100644 --- a/pandas/tests/io/formats/style/test_html.py +++ b/pandas/tests/io/formats/style/test_html.py @@ -191,6 +191,13 @@ def test_doctype(styler): assert "" not in result +def test_bold_headers(styler): + result = styler.to_html(bold_headers=True) + assert "th {\n font-weight: bold;\n}" in result + result = styler.to_html() + assert "th {\n font-weight: bold;\n}" not in result + + def test_block_names(tpl_style, tpl_table): # catch accidental removal of a block expected_style = { From 50e4e89ad556fb8d734cac6391f6ae2aa7bc7a56 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sat, 21 Aug 2021 09:37:19 +0200 Subject: [PATCH 2/4] docs --- pandas/io/formats/style.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index b385b3a5dd556..18ee97f590317 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -876,6 +876,10 @@ def to_html( will display each explicit level element in a hierarchical key for each column. Defaults to ``pandas.options.styler.sparse.columns`` value. + .. versionadded:: 1.4.0 + bold_headers : bool, optional + Adds "font-weight: bold;" as a CSS property to table style header cells. + .. versionadded:: 1.4.0 encoding : str, optional Character encoding setting for file output, and HTML meta tags, From b30839018e5d60d7c5e7e75e14a201a380d8fa05 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sat, 21 Aug 2021 09:38:40 +0200 Subject: [PATCH 3/4] docs --- doc/source/whatsnew/v1.4.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index d60e103c1bcc4..d1f69380aac71 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -73,7 +73,7 @@ Styler - Styling of indexing has been added, with :meth:`.Styler.apply_index` and :meth:`.Styler.applymap_index`. These mirror the signature of the methods already used to style data values, and work with both HTML and LaTeX format (:issue:`41893`). - :meth:`.Styler.bar` introduces additional arguments to control alignment and display (:issue:`26070`, :issue:`36419`), and it also validates the input arguments ``width`` and ``height`` (:issue:`42511`). - :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``, ``sparse_columns``, ``bold_headers`` (:issue:`41946`, :issue:`xxxxx`). + - :meth:`.Styler.to_html` introduces keyword arguments ``sparse_index``, ``sparse_columns``, ``bold_headers`` (:issue:`41946`, :issue:`43149`). - 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`) There are also bug fixes and deprecations listed below. From 2603d03558ce0946a8e0522605e4871d1551384c Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sat, 21 Aug 2021 10:12:14 +0200 Subject: [PATCH 4/4] add caption --- doc/source/whatsnew/v1.4.0.rst | 2 +- pandas/io/formats/style.py | 8 ++++++++ pandas/tests/io/formats/style/test_html.py | 9 ++++++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index d1f69380aac71..29974cf49b5e2 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -73,7 +73,7 @@ Styler - Styling of indexing has been added, with :meth:`.Styler.apply_index` and :meth:`.Styler.applymap_index`. These mirror the signature of the methods already used to style data values, and work with both HTML and LaTeX format (:issue:`41893`). - :meth:`.Styler.bar` introduces additional arguments to control alignment and display (:issue:`26070`, :issue:`36419`), and it also validates the input arguments ``width`` and ``height`` (:issue:`42511`). - :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``, ``sparse_columns``, ``bold_headers`` (:issue:`41946`, :issue:`43149`). + - :meth:`.Styler.to_html` introduces keyword arguments ``sparse_index``, ``sparse_columns``, ``bold_headers``, ``caption`` (:issue:`41946`, :issue:`43149`). - 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`) There are also bug fixes and deprecations listed below. diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 18ee97f590317..d1d2e7369ac69 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -839,6 +839,7 @@ def to_html( sparse_index: bool | None = None, sparse_columns: bool | None = None, bold_headers: bool = False, + caption: str | None = None, encoding: str | None = None, doctype_html: bool = False, exclude_styles: bool = False, @@ -880,6 +881,10 @@ def to_html( bold_headers : bool, optional Adds "font-weight: bold;" as a CSS property to table style header cells. + .. versionadded:: 1.4.0 + caption : str, optional + Set, or overwrite, the caption on Styler before rendering. + .. versionadded:: 1.4.0 encoding : str, optional Character encoding setting for file output, and HTML meta tags, @@ -923,6 +928,9 @@ def to_html( [{"selector": "th", "props": "font-weight: bold;"}], overwrite=False ) + if caption is not None: + obj.set_caption(caption) + # Build HTML string.. html = obj._render_html( sparse_index=sparse_index, diff --git a/pandas/tests/io/formats/style/test_html.py b/pandas/tests/io/formats/style/test_html.py index 958c82a20a591..e22ca1fabee65 100644 --- a/pandas/tests/io/formats/style/test_html.py +++ b/pandas/tests/io/formats/style/test_html.py @@ -191,13 +191,20 @@ def test_doctype(styler): assert "" not in result -def test_bold_headers(styler): +def test_bold_headers_arg(styler): result = styler.to_html(bold_headers=True) assert "th {\n font-weight: bold;\n}" in result result = styler.to_html() assert "th {\n font-weight: bold;\n}" not in result +def test_caption_arg(styler): + result = styler.to_html(caption="foo bar") + assert "foo bar" in result + result = styler.to_html() + assert "foo bar" not in result + + def test_block_names(tpl_style, tpl_table): # catch accidental removal of a block expected_style = {