Skip to content

ENH: Styler.to_html gets bold_headers and caption kwarg #43149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Sep 1, 2021
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)

Expand Down
18 changes: 18 additions & 0 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/io/formats/style/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,20 @@ def test_doctype_encoding(styler):
assert '<meta charset="ANSI">' 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 "<caption>foo bar</caption>" in result
result = styler.to_html()
assert "<caption>foo bar</caption>" not in result


def test_block_names(tpl_style, tpl_table):
# catch accidental removal of a block
expected_style = {
Expand Down