Skip to content

Commit aad12ca

Browse files
z3c0yehoshuadimarsky
authored andcommitted
ENH: exclude html table border w/False value (pandas-dev#45943)
1 parent f54dfd6 commit aad12ca

File tree

6 files changed

+37
-8
lines changed

6 files changed

+37
-8
lines changed

doc/source/whatsnew/v1.5.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,8 @@ I/O
569569
- Bug in Parquet roundtrip for Interval dtype with ``datetime64[ns]`` subtype (:issue:`45881`)
570570
- Bug in :func:`read_excel` when reading a ``.ods`` file with newlines between xml elements (:issue:`45598`)
571571
- Bug in :func:`read_parquet` when ``engine="fastparquet"`` where the file was not closed on error (:issue:`46555`)
572+
- :meth:`to_html` now excludes the ``border`` attribute from ``<table>`` elements when ``border`` keyword is set to ``False``.
573+
-
572574

573575
Period
574576
^^^^^^

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2892,7 +2892,7 @@ def to_html(
28922892
classes: str | list | tuple | None = None,
28932893
escape: bool = True,
28942894
notebook: bool = False,
2895-
border: int | None = None,
2895+
border: int | bool | None = None,
28962896
table_id: str | None = None,
28972897
render_links: bool = False,
28982898
encoding: str | None = None,

pandas/io/formats/format.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ def to_html(
10581058
encoding: str | None = None,
10591059
classes: str | list | tuple | None = None,
10601060
notebook: bool = False,
1061-
border: int | None = None,
1061+
border: int | bool | None = None,
10621062
table_id: str | None = None,
10631063
render_links: bool = False,
10641064
) -> str | None:

pandas/io/formats/html.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def __init__(
4444
self,
4545
formatter: DataFrameFormatter,
4646
classes: str | list[str] | tuple[str, ...] | None = None,
47-
border: int | None = None,
47+
border: int | bool | None = None,
4848
table_id: str | None = None,
4949
render_links: bool = False,
5050
) -> None:
@@ -57,8 +57,11 @@ def __init__(
5757
self.bold_rows = self.fmt.bold_rows
5858
self.escape = self.fmt.escape
5959
self.show_dimensions = self.fmt.show_dimensions
60-
if border is None:
60+
if border is None or border is True:
6161
border = cast(int, get_option("display.html.border"))
62+
elif not border:
63+
border = None
64+
6265
self.border = border
6366
self.table_id = table_id
6467
self.render_links = render_links
@@ -237,8 +240,13 @@ def _write_table(self, indent: int = 0) -> None:
237240
else:
238241
id_section = f' id="{self.table_id}"'
239242

243+
if self.border is None:
244+
border_attr = ""
245+
else:
246+
border_attr = f' border="{self.border}"'
247+
240248
self.write(
241-
f'<table border="{self.border}" class="{" ".join(_classes)}"{id_section}>',
249+
f'<table{border_attr} class="{" ".join(_classes)}"{id_section}>',
242250
indent,
243251
)
244252

pandas/tests/io/formats/test_to_html.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,9 @@ def test_to_html_truncate_multi_index(sparsify, expected, datapath):
342342
"option,result,expected",
343343
[
344344
(None, lambda df: df.to_html(), "1"),
345-
(None, lambda df: df.to_html(border=0), "0"),
346-
(0, lambda df: df.to_html(), "0"),
347-
(0, lambda df: df._repr_html_(), "0"),
345+
(None, lambda df: df.to_html(border=2), "2"),
346+
(2, lambda df: df.to_html(), "2"),
347+
(2, lambda df: df._repr_html_(), "2"),
348348
],
349349
)
350350
def test_to_html_border(option, result, expected):

pandas/tests/io/test_html.py

+19
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,25 @@ def test_to_html_timestamp(self):
11481148
result = df.to_html()
11491149
assert "2000-01-01" in result
11501150

1151+
def test_to_html_borderless(self):
1152+
df = DataFrame([{"A": 1, "B": 2}])
1153+
out_border_default = df.to_html()
1154+
out_border_true = df.to_html(border=True)
1155+
out_border_explicit_default = df.to_html(border=1)
1156+
out_border_nondefault = df.to_html(border=2)
1157+
out_border_zero = df.to_html(border=0)
1158+
1159+
out_border_false = df.to_html(border=False)
1160+
1161+
assert ' border="1"' in out_border_default
1162+
assert out_border_true == out_border_default
1163+
assert out_border_default == out_border_explicit_default
1164+
assert out_border_default != out_border_nondefault
1165+
assert ' border="2"' in out_border_nondefault
1166+
assert ' border="0"' not in out_border_zero
1167+
assert " border" not in out_border_false
1168+
assert out_border_zero == out_border_false
1169+
11511170
@pytest.mark.parametrize(
11521171
"displayed_only,exp0,exp1",
11531172
[

0 commit comments

Comments
 (0)