Skip to content

Commit 49e57e7

Browse files
committed
ENH: add parameter for HTML border
1 parent 10bf721 commit 49e57e7

File tree

6 files changed

+57
-7
lines changed

6 files changed

+57
-7
lines changed

doc/source/options.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,9 @@ display.width 80 Width of the display in characters.
392392
IPython qtconsole, or IDLE do not run in a
393393
terminal and hence it is not possible
394394
to correctly detect the width.
395+
html.border 1 A ``border=value`` attribute is
396+
inserted in the ``<table>`` tag
397+
for the DataFrame HTML repr.
395398
io.excel.xls.writer xlwt The default Excel writer engine for
396399
'xls' files.
397400
io.excel.xlsm.writer openpyxl The default Excel writer engine for

doc/source/whatsnew/v0.19.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ Other enhancements
426426
df.sort_values(by='row2', axis=1)
427427

428428
- Added documentation to :ref:`I/O<io.dtypes>` regarding the perils of reading in columns with mixed dtypes and how to handle it (:issue:`13746`)
429+
- :meth:`~DataFrame.to_html` now has a ``border`` argument to control the value in the opening ``<table>`` tag. The default is the value of the ``html.border`` option, which defaults to 1. This also affects the notebook HTML repr, but since Jupyter's CSS includes a border-width attribute, the visual effect is the same. (:issue:`11563`).
429430
- Raise ``ImportError`` in the sql functions when ``sqlalchemy`` is not installed and a connection string is used (:issue:`11920`).
430431
- Compatibility with matplotlib 2.0. Older versions of pandas should also work with matplotlib 2.0 (:issue:`13333`)
431432

pandas/core/config_init.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,17 @@ def mpl_style_cb(key):
346346
cf.deprecate_option('display.height', msg=pc_height_deprecation_warning,
347347
rkey='display.max_rows')
348348

349+
pc_html_border_doc = """
350+
: int
351+
A ``border=value`` attribute is inserted in the ``<table>`` tag
352+
for the DataFrame HTML repr.
353+
"""
354+
355+
with cf.config_prefix('html'):
356+
cf.register_option('border', 1, pc_html_border_doc,
357+
validator=is_int)
358+
359+
349360
tc_sim_interactive_doc = """
350361
: boolean
351362
Whether to simulate interactive mode for purposes of testing

pandas/core/frame.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,7 +1560,8 @@ def to_html(self, buf=None, columns=None, col_space=None, header=True,
15601560
index=True, na_rep='NaN', formatters=None, float_format=None,
15611561
sparsify=None, index_names=True, justify=None, bold_rows=True,
15621562
classes=None, escape=True, max_rows=None, max_cols=None,
1563-
show_dimensions=False, notebook=False, decimal='.'):
1563+
show_dimensions=False, notebook=False, decimal='.',
1564+
border=None):
15641565
"""
15651566
Render a DataFrame as an HTML table.
15661567
@@ -1582,6 +1583,11 @@ def to_html(self, buf=None, columns=None, col_space=None, header=True,
15821583
Character recognized as decimal separator, e.g. ',' in Europe
15831584
15841585
.. versionadded:: 0.18.0
1586+
border : int
1587+
A ``border=border`` attribute is included in the opening
1588+
`<table>` tag. Default ``pd.options.html.border``.
1589+
1590+
.. versionadded:: 0.19.0
15851591
"""
15861592

15871593
formatter = fmt.DataFrameFormatter(self, buf=buf, columns=columns,
@@ -1597,7 +1603,7 @@ def to_html(self, buf=None, columns=None, col_space=None, header=True,
15971603
show_dimensions=show_dimensions,
15981604
decimal=decimal)
15991605
# TODO: a generic formatter wld b in DataFrameFormatter
1600-
formatter.to_html(classes=classes, notebook=notebook)
1606+
formatter.to_html(classes=classes, notebook=notebook, border=border)
16011607

16021608
if buf is None:
16031609
return formatter.buf.getvalue()

pandas/formats/format.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -670,20 +670,28 @@ def _format_col(self, i):
670670
float_format=self.float_format, na_rep=self.na_rep,
671671
space=self.col_space, decimal=self.decimal)
672672

673-
def to_html(self, classes=None, notebook=False):
673+
def to_html(self, classes=None, notebook=False, border=None):
674674
"""
675675
Render a DataFrame to a html table.
676676
677677
Parameters
678678
----------
679+
classes : str or list-like
680+
classes to include in the `class` attribute of the opening
681+
``<table>`` tag, in addition to the default "dataframe".
679682
notebook : {True, False}, optional, default False
680683
Whether the generated HTML is for IPython Notebook.
684+
border : int
685+
A ``border=border`` attribute is included in the opening
686+
``<table>`` tag. Default ``pd.options.html.border``.
681687
682-
"""
688+
.. versionadded:: 0.19.0
689+
"""
683690
html_renderer = HTMLFormatter(self, classes=classes,
684691
max_rows=self.max_rows,
685692
max_cols=self.max_cols,
686-
notebook=notebook)
693+
notebook=notebook,
694+
border=border)
687695
if hasattr(self.buf, 'write'):
688696
html_renderer.write_result(self.buf)
689697
elif isinstance(self.buf, compat.string_types):
@@ -909,7 +917,7 @@ class HTMLFormatter(TableFormatter):
909917
indent_delta = 2
910918

911919
def __init__(self, formatter, classes=None, max_rows=None, max_cols=None,
912-
notebook=False):
920+
notebook=False, border=None):
913921
self.fmt = formatter
914922
self.classes = classes
915923

@@ -925,6 +933,9 @@ def __init__(self, formatter, classes=None, max_rows=None, max_cols=None,
925933
self.is_truncated = (self.max_rows < len(self.fmt.frame) or
926934
self.max_cols < len(self.fmt.columns))
927935
self.notebook = notebook
936+
if border is None:
937+
border = get_option('html.border')
938+
self.border = border
928939

929940
def write(self, s, indent=0):
930941
rs = pprint_thing(s)
@@ -1000,7 +1011,8 @@ def write_result(self, buf):
10001011

10011012
self.write('<div{0}>'.format(div_style))
10021013

1003-
self.write('<table border="1" class="%s">' % ' '.join(_classes),
1014+
self.write('<table border="%s" class="%s">' % (self.border,
1015+
' '.join(_classes)),
10041016
indent)
10051017

10061018
indent += self.indent_delta

pandas/tests/formats/test_format.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,6 +1650,23 @@ def test_to_html_truncate_multi_index_sparse_off(self):
16501650
expected = expected.decode('utf-8')
16511651
self.assertEqual(result, expected)
16521652

1653+
def test_to_html_border(self):
1654+
df = DataFrame({'A': [1, 2]})
1655+
result = df.to_html()
1656+
assert 'border="1"' in result
1657+
1658+
def test_to_html_border_option(self):
1659+
df = DataFrame({'A': [1, 2]})
1660+
with pd.option_context('html.border', 0):
1661+
result = df.to_html()
1662+
self.assertTrue('border="0"' in result)
1663+
self.assertTrue('border="0"' in df._repr_html_())
1664+
1665+
def test_to_html_border_zero(self):
1666+
df = DataFrame({'A': [1, 2]})
1667+
result = df.to_html(border=0)
1668+
self.assertTrue('border="0"' in result)
1669+
16531670
def test_nonunicode_nonascii_alignment(self):
16541671
df = DataFrame([["aa\xc3\xa4\xc3\xa4", 1], ["bbbb", 2]])
16551672
rep_str = df.to_string()

0 commit comments

Comments
 (0)