diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index aef6128c63829..081441eddd7a7 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``, ``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`) - Global options have been extended to configure default ``Styler`` properties including formatting and encoding options (:issue:`41395`) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 4fc13fd54df02..ddafd8369ddd5 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -863,6 +863,8 @@ def to_html( table_attributes: str | None = None, 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, @@ -900,6 +902,14 @@ 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 + 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. @@ -938,6 +948,14 @@ 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 + ) + + if caption is not None: + obj.set_caption(caption) + encoding = encoding or get_option("styler.render.encoding") # Build HTML string.. html = obj._render_html( diff --git a/pandas/tests/io/formats/style/test_html.py b/pandas/tests/io/formats/style/test_html.py index 493e839d63d2b..9898f4a598bb7 100644 --- a/pandas/tests/io/formats/style/test_html.py +++ b/pandas/tests/io/formats/style/test_html.py @@ -199,6 +199,20 @@ def test_doctype_encoding(styler): assert '' in result +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 = {