Skip to content

Commit 524bc7c

Browse files
attack68feefladder
authored andcommitted
ENH: Styler.to_html gets bold_headers and caption kwarg (pandas-dev#43149)
1 parent cc7b33a commit 524bc7c

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

doc/source/whatsnew/v1.4.0.rst

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

pandas/io/formats/style.py

+18
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,8 @@ def to_html(
863863
table_attributes: str | None = None,
864864
sparse_index: bool | None = None,
865865
sparse_columns: bool | None = None,
866+
bold_headers: bool = False,
867+
caption: str | None = None,
866868
encoding: str | None = None,
867869
doctype_html: bool = False,
868870
exclude_styles: bool = False,
@@ -900,6 +902,14 @@ def to_html(
900902
will display each explicit level element in a hierarchical key for each
901903
column. Defaults to ``pandas.options.styler.sparse.columns`` value.
902904
905+
.. versionadded:: 1.4.0
906+
bold_headers : bool, optional
907+
Adds "font-weight: bold;" as a CSS property to table style header cells.
908+
909+
.. versionadded:: 1.4.0
910+
caption : str, optional
911+
Set, or overwrite, the caption on Styler before rendering.
912+
903913
.. versionadded:: 1.4.0
904914
encoding : str, optional
905915
Character encoding setting for file output, and HTML meta tags.
@@ -938,6 +948,14 @@ def to_html(
938948
if sparse_columns is None:
939949
sparse_columns = get_option("styler.sparse.columns")
940950

951+
if bold_headers:
952+
obj.set_table_styles(
953+
[{"selector": "th", "props": "font-weight: bold;"}], overwrite=False
954+
)
955+
956+
if caption is not None:
957+
obj.set_caption(caption)
958+
941959
encoding = encoding or get_option("styler.render.encoding")
942960
# Build HTML string..
943961
html = obj._render_html(

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

+14
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,20 @@ def test_doctype_encoding(styler):
199199
assert '<meta charset="ANSI">' in result
200200

201201

202+
def test_bold_headers_arg(styler):
203+
result = styler.to_html(bold_headers=True)
204+
assert "th {\n font-weight: bold;\n}" in result
205+
result = styler.to_html()
206+
assert "th {\n font-weight: bold;\n}" not in result
207+
208+
209+
def test_caption_arg(styler):
210+
result = styler.to_html(caption="foo bar")
211+
assert "<caption>foo bar</caption>" in result
212+
result = styler.to_html()
213+
assert "<caption>foo bar</caption>" not in result
214+
215+
202216
def test_block_names(tpl_style, tpl_table):
203217
# catch accidental removal of a block
204218
expected_style = {

0 commit comments

Comments
 (0)