Skip to content

ENH: exclude html table border w/False value #45943

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 21 commits into from
Apr 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
ENH: exclude html table border w/falsy value
Per the HTML5 spec, border attributes on table elements are obsolete.
Borders should be implemented via CSS instead.
https://html.spec.whatwg.org/multipage/obsolete.html#attr-table-border
This enhancement will retain the default behavior of the border keyword of to_html,
but will exclude the attribute entirely upon receiving a falsy value.
  • Loading branch information
z3c0 committed Feb 11, 2022
commit e375c1d30afe23c24f3632dc73f4f444bbb36124
3 changes: 1 addition & 2 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ Other enhancements
- :meth:`.GroupBy.min` and :meth:`.GroupBy.max` now supports `Numba <https://numba.pydata.org/>`_ execution with the ``engine`` keyword (:issue:`45428`)
- Implemented a ``bool``-dtype :class:`Index`, passing a bool-dtype array-like to ``pd.Index`` will now retain ``bool`` dtype instead of casting to ``object`` (:issue:`45061`)
- Implemented a complex-dtype :class:`Index`, passing a complex-dtype array-like to ``pd.Index`` will now retain complex dtype instead of casting to ``object`` (:issue:`45845`)

-
- :meth:`to_html` now excludes the ``border`` attribute from ``<table>`` elements when a falsy value is passed to the ``border`` keyword.

.. ---------------------------------------------------------------------------
.. _whatsnew_150.notable_bug_fixes:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2883,7 +2883,7 @@ def to_html(
classes: str | list | tuple | None = None,
escape: bool = True,
notebook: bool = False,
border: int | None = None,
border: int | bool | None = None,
table_id: str | None = None,
render_links: bool = False,
encoding: str | None = None,
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ def to_html(
encoding: str | None = None,
classes: str | list | tuple | None = None,
notebook: bool = False,
border: int | None = None,
border: int | bool | None = None,
table_id: str | None = None,
render_links: bool = False,
) -> str | None:
Expand Down
5 changes: 4 additions & 1 deletion pandas/io/formats/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(
self,
formatter: DataFrameFormatter,
classes: str | list[str] | tuple[str, ...] | None = None,
border: int | None = None,
border: int | bool | None = None,
table_id: str | None = None,
render_links: bool = False,
) -> None:
Expand All @@ -61,6 +61,9 @@ def __init__(
border = cast(int, get_option("display.html.border"))
elif not border:
border = None
elif isinstance(border, bool): # border=True
border = cast(int, get_option("display.html.border"))

self.border = border
self.table_id = table_id
self.render_links = render_links
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/io/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,23 @@ def test_to_html_timestamp(self):
result = df.to_html()
assert "2000-01-01" in result

def test_to_html_borderless(self):
df = DataFrame([{'A': 1, 'B': 2}])
out_border_default = df.to_html()
out_border_true = df.to_html(border=True)
out_border_explicit_default = df.to_html(border=1)
out_border_nondefault = df.to_html(border=2)
out_border_false = df.to_html(border=False)
out_border_zero = df.to_html(border=0)

assert ' border="1"' in out_border_default
assert out_border_true == out_border_default
assert out_border_default == out_border_explicit_default
assert out_border_default != out_border_nondefault
assert ' border="2"' in out_border_nondefault
assert " border" not in out_border_false
assert out_border_zero == out_border_false

@pytest.mark.parametrize(
"displayed_only,exp0,exp1",
[
Expand Down