Skip to content

Commit

Permalink
ENH: exclude html table border w/False value (pandas-dev#45943)
Browse files Browse the repository at this point in the history
  • Loading branch information
z3c0 authored Apr 17, 2022
1 parent 7dd22b8 commit 57885c6
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 8 deletions.
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,8 @@ I/O
- Bug in Parquet roundtrip for Interval dtype with ``datetime64[ns]`` subtype (:issue:`45881`)
- Bug in :func:`read_excel` when reading a ``.ods`` file with newlines between xml elements (:issue:`45598`)
- Bug in :func:`read_parquet` when ``engine="fastparquet"`` where the file was not closed on error (:issue:`46555`)
- :meth:`to_html` now excludes the ``border`` attribute from ``<table>`` elements when ``border`` keyword is set to ``False``.
-

Period
^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2892,7 +2892,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
14 changes: 11 additions & 3 deletions 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 @@ -57,8 +57,11 @@ def __init__(
self.bold_rows = self.fmt.bold_rows
self.escape = self.fmt.escape
self.show_dimensions = self.fmt.show_dimensions
if border is None:
if border is None or border is True:
border = cast(int, get_option("display.html.border"))
elif not border:
border = None

self.border = border
self.table_id = table_id
self.render_links = render_links
Expand Down Expand Up @@ -237,8 +240,13 @@ def _write_table(self, indent: int = 0) -> None:
else:
id_section = f' id="{self.table_id}"'

if self.border is None:
border_attr = ""
else:
border_attr = f' border="{self.border}"'

self.write(
f'<table border="{self.border}" class="{" ".join(_classes)}"{id_section}>',
f'<table{border_attr} class="{" ".join(_classes)}"{id_section}>',
indent,
)

Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/io/formats/test_to_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,9 @@ def test_to_html_truncate_multi_index(sparsify, expected, datapath):
"option,result,expected",
[
(None, lambda df: df.to_html(), "1"),
(None, lambda df: df.to_html(border=0), "0"),
(0, lambda df: df.to_html(), "0"),
(0, lambda df: df._repr_html_(), "0"),
(None, lambda df: df.to_html(border=2), "2"),
(2, lambda df: df.to_html(), "2"),
(2, lambda df: df._repr_html_(), "2"),
],
)
def test_to_html_border(option, result, expected):
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/io/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,25 @@ 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_zero = df.to_html(border=0)

out_border_false = df.to_html(border=False)

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="0"' not in out_border_zero
assert " border" not in out_border_false
assert out_border_zero == out_border_false

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

0 comments on commit 57885c6

Please sign in to comment.