Skip to content

Commit 1f16ca7

Browse files
COMPAT: HTML styling for MI and notebook 5.0 (#16080)
Fixes the visual styling of MI labels in notebook 5.0 - No change to non-MI dataframes - Left-align MI columns - Top-align MI row labels
1 parent ff0d5a4 commit 1f16ca7

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

doc/source/whatsnew/v0.20.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ Other Enhancements
540540
- HTML table output skips ``colspan`` or ``rowspan`` attribute if equal to 1. (:issue:`15403`)
541541
- ``pd.io.api.Styler`` template now has blocks for easier extension, :ref:`see the example notebook <style.ipynb#Subclassing>` (:issue:`15649`)
542542
- ``pd.io.api.Styler.render`` now accepts ``**kwargs`` to allow user-defined variables in the template (:issue:`15649`)
543-
543+
- Compatability with Jupyter notebook 5.0; MultiIndex column labels are left-aligned and MultiIndex row-labels are top-aligned (:issue:`15379`)
544544

545545
- ``TimedeltaIndex`` now has a custom datetick formatter specifically designed for nanosecond level precision (:issue:`8711`)
546546
- ``pd.api.types.union_categoricals`` gained the ``ignore_ordered`` argument to allow ignoring the ordered attribute of unioned categoricals (:issue:`13410`). See the :ref:`categorical union docs <categorical.union>` for more information.

pandas/io/formats/format.py

+20
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
# pylint: disable=W0141
1010

1111
import sys
12+
from textwrap import dedent
1213

1314
from pandas.core.dtypes.missing import isnull, notnull
1415
from pandas.core.dtypes.common import (
@@ -1107,6 +1108,24 @@ def write_tr(self, line, indent=0, indent_delta=4, header=False,
11071108
indent -= indent_delta
11081109
self.write('</tr>', indent)
11091110

1111+
def write_style(self):
1112+
template = dedent("""\
1113+
<style>
1114+
.dataframe thead tr:only-child th {
1115+
text-align: right;
1116+
}
1117+
1118+
.dataframe thead th {
1119+
text-align: left;
1120+
}
1121+
1122+
.dataframe tbody tr th {
1123+
vertical-align: top;
1124+
}
1125+
</style>""")
1126+
if self.notebook:
1127+
self.write(template)
1128+
11101129
def write_result(self, buf):
11111130
indent = 0
11121131
frame = self.frame
@@ -1131,6 +1150,7 @@ def write_result(self, buf):
11311150

11321151
self.write('<div{0}>'.format(div_style))
11331152

1153+
self.write_style()
11341154
self.write('<table border="%s" class="%s">' % (self.border,
11351155
' '.join(_classes)),
11361156
indent)

pandas/tests/io/formats/test_to_html.py

+10
Original file line numberDiff line numberDiff line change
@@ -1859,3 +1859,13 @@ def test_to_html_no_index_max_rows(self):
18591859
</tbody>
18601860
</table>""")
18611861
self.assertEqual(result, expected)
1862+
1863+
def test_to_html_notebook_has_style(self):
1864+
df = pd.DataFrame({"A": [1, 2, 3]})
1865+
result = df.to_html(notebook=True)
1866+
assert "thead tr:only-child" in result
1867+
1868+
def test_to_html_notebook_has_no_style(self):
1869+
df = pd.DataFrame({"A": [1, 2, 3]})
1870+
result = df.to_html()
1871+
assert "thead tr:only-child" not in result

0 commit comments

Comments
 (0)