Skip to content

Commit 3ae4229

Browse files
simonjayhawkinsjreback
authored andcommitted
REF: io/formats/html.py (and io/formats/format.py) (#24637)
1 parent 8730ea9 commit 3ae4229

File tree

2 files changed

+55
-66
lines changed

2 files changed

+55
-66
lines changed

pandas/io/formats/format.py

+20-9
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ def space_format(x, y):
778778
for i, (col, x) in enumerate(zip(columns,
779779
fmt_columns))]
780780

781-
if self.show_index_names and self.has_index_names:
781+
if self.show_row_idx_names:
782782
for x in str_columns:
783783
x.append('')
784784

@@ -793,30 +793,41 @@ def has_index_names(self):
793793
def has_column_names(self):
794794
return _has_names(self.frame.columns)
795795

796+
@property
797+
def show_row_idx_names(self):
798+
return all((self.has_index_names,
799+
self.index,
800+
self.show_index_names))
801+
802+
@property
803+
def show_col_idx_names(self):
804+
return all((self.has_column_names,
805+
self.show_index_names,
806+
self.header))
807+
796808
def _get_formatted_index(self, frame):
797809
# Note: this is only used by to_string() and to_latex(), not by
798810
# to_html().
799811
index = frame.index
800812
columns = frame.columns
801-
802-
show_index_names = self.show_index_names and self.has_index_names
803-
show_col_names = (self.show_index_names and self.has_column_names)
804-
805813
fmt = self._get_formatter('__index__')
806814

807815
if isinstance(index, ABCMultiIndex):
808-
fmt_index = index.format(sparsify=self.sparsify, adjoin=False,
809-
names=show_index_names, formatter=fmt)
816+
fmt_index = index.format(
817+
sparsify=self.sparsify, adjoin=False,
818+
names=self.show_row_idx_names, formatter=fmt)
810819
else:
811-
fmt_index = [index.format(name=show_index_names, formatter=fmt)]
820+
fmt_index = [index.format(
821+
name=self.show_row_idx_names, formatter=fmt)]
822+
812823
fmt_index = [tuple(_make_fixed_width(list(x), justify='left',
813824
minimum=(self.col_space or 0),
814825
adj=self.adj)) for x in fmt_index]
815826

816827
adjoined = self.adj.adjoin(1, *fmt_index).split('\n')
817828

818829
# empty space for columns
819-
if show_col_names:
830+
if self.show_col_idx_names:
820831
col_header = ['{x}'.format(x=x)
821832
for x in self._get_column_name_list()]
822833
else:

pandas/io/formats/html.py

+35-57
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,11 @@ def __init__(self, formatter, classes=None, notebook=False, border=None,
4545

4646
@property
4747
def show_row_idx_names(self):
48-
return all((self.fmt.has_index_names,
49-
self.fmt.index,
50-
self.fmt.show_index_names))
48+
return self.fmt.show_row_idx_names
5149

5250
@property
5351
def show_col_idx_names(self):
54-
# see gh-22579
55-
# Column misalignment also occurs for
56-
# a standard index when the columns index is named.
57-
# Determine if ANY column names need to be displayed
58-
# since if the row index is not displayed a column of
59-
# blank cells need to be included before the DataFrame values.
60-
# TODO: refactor to add show_col_idx_names property to
61-
# DataFrameFormatter
62-
return all((self.fmt.has_column_names,
63-
self.fmt.show_index_names,
64-
self.fmt.header))
52+
return self.fmt.show_col_idx_names
6553

6654
@property
6755
def row_levels(self):
@@ -184,14 +172,28 @@ def write_style(self):
184172
template = dedent('\n'.join((template_first,
185173
template_mid,
186174
template_last)))
187-
if self.notebook:
188-
self.write(template)
175+
self.write(template)
189176

190177
def write_result(self, buf):
191-
indent = 0
192-
id_section = ""
193-
frame = self.frame
178+
if self.notebook:
179+
self.write('<div>')
180+
self.write_style()
181+
182+
self._write_table()
183+
184+
if self.should_show_dimensions:
185+
by = chr(215) if compat.PY3 else unichr(215) # ×
186+
self.write(u('<p>{rows} rows {by} {cols} columns</p>')
187+
.format(rows=len(self.frame),
188+
by=by,
189+
cols=len(self.frame.columns)))
194190

191+
if self.notebook:
192+
self.write('</div>')
193+
194+
buffer_put_lines(buf, self.elements)
195+
196+
def _write_table(self, indent=0):
195197
_classes = ['dataframe'] # Default class.
196198
use_mathjax = get_option("display.html.use_mathjax")
197199
if not use_mathjax:
@@ -204,33 +206,21 @@ def write_result(self, buf):
204206
.format(typ=type(self.classes)))
205207
_classes.extend(self.classes)
206208

207-
if self.notebook:
208-
self.write('<div>')
209-
210-
self.write_style()
211-
212-
if self.table_id is not None:
209+
if self.table_id is None:
210+
id_section = ""
211+
else:
213212
id_section = ' id="{table_id}"'.format(table_id=self.table_id)
213+
214214
self.write('<table border="{border}" class="{cls}"{id_section}>'
215215
.format(border=self.border, cls=' '.join(_classes),
216216
id_section=id_section), indent)
217217

218-
indent += self.indent_delta
219-
indent = self._write_header(indent)
220-
indent = self._write_body(indent)
218+
if self.fmt.header or self.show_row_idx_names:
219+
self._write_header(indent + self.indent_delta)
221220

222-
self.write('</table>', indent)
223-
if self.should_show_dimensions:
224-
by = chr(215) if compat.PY3 else unichr(215) # ×
225-
self.write(u('<p>{rows} rows {by} {cols} columns</p>')
226-
.format(rows=len(frame),
227-
by=by,
228-
cols=len(frame.columns)))
221+
self._write_body(indent + self.indent_delta)
229222

230-
if self.notebook:
231-
self.write('</div>')
232-
233-
buffer_put_lines(buf, self.elements)
223+
self.write('</table>', indent)
234224

235225
def _write_col_header(self, indent):
236226
truncate_h = self.fmt.truncate_h
@@ -359,41 +349,29 @@ def _write_row_header(self, indent):
359349
self.write_tr(row, indent, self.indent_delta, header=True)
360350

361351
def _write_header(self, indent):
362-
if not (self.fmt.header or self.show_row_idx_names):
363-
# write nothing
364-
return indent
365-
366352
self.write('<thead>', indent)
367-
indent += self.indent_delta
368353

369354
if self.fmt.header:
370-
self._write_col_header(indent)
355+
self._write_col_header(indent + self.indent_delta)
371356

372357
if self.show_row_idx_names:
373-
self._write_row_header(indent)
358+
self._write_row_header(indent + self.indent_delta)
374359

375-
indent -= self.indent_delta
376360
self.write('</thead>', indent)
377361

378-
return indent
379-
380362
def _write_body(self, indent):
381363
self.write('<tbody>', indent)
382-
indent += self.indent_delta
383-
384364
fmt_values = {i: self.fmt._format_col(i) for i in range(self.ncols)}
385365

386366
# write values
387367
if self.fmt.index and isinstance(self.frame.index, ABCMultiIndex):
388-
self._write_hierarchical_rows(fmt_values, indent)
368+
self._write_hierarchical_rows(
369+
fmt_values, indent + self.indent_delta)
389370
else:
390-
self._write_regular_rows(fmt_values, indent)
371+
self._write_regular_rows(
372+
fmt_values, indent + self.indent_delta)
391373

392-
indent -= self.indent_delta
393374
self.write('</tbody>', indent)
394-
indent -= self.indent_delta
395-
396-
return indent
397375

398376
def _write_regular_rows(self, fmt_values, indent):
399377
truncate_h = self.fmt.truncate_h

0 commit comments

Comments
 (0)