Skip to content

Commit

Permalink
CLN: io/formats/html.py: refactor (pandas-dev#22726)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonjayhawkins authored and jreback committed Nov 18, 2018
1 parent deedb5f commit cc712c9
Showing 1 changed file with 9 additions and 45 deletions.
54 changes: 9 additions & 45 deletions pandas/io/formats/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def _write_cell(self, s, kind='td', indent=0, tags=None):
self.write(u'{start}{rs}</{kind}>'
.format(start=start_tag, rs=rs, kind=kind), indent)

def write_tr(self, line, indent=0, indent_delta=4, header=False,
def write_tr(self, line, indent=0, indent_delta=0, header=False,
align=None, tags=None, nindex_levels=0):
if tags is None:
tags = {}
Expand Down Expand Up @@ -200,26 +200,6 @@ def _write_header(self, indent):
# write nothing
return indent

def _column_header():
if self.fmt.index:
row = [''] * (self.frame.index.nlevels - 1)
else:
row = []

if isinstance(self.columns, ABCMultiIndex):
if self.fmt.has_column_names and self.fmt.index:
row.append(single_column_table(self.columns.names))
else:
row.append('')
style = "text-align: {just};".format(just=self.fmt.justify)
row.extend([single_column_table(c, self.fmt.justify, style)
for c in self.columns])
else:
if self.fmt.index:
row.append(self.columns.name or '')
row.extend(self.columns)
return row

self.write('<thead>', indent)

indent += self.indent_delta
Expand Down Expand Up @@ -301,16 +281,21 @@ def _column_header():
self.write_tr(row, indent, self.indent_delta, tags=tags,
header=True)
else:
col_row = _column_header()
if self.fmt.index:
row = [''] * (self.frame.index.nlevels - 1)
row.append(self.columns.name or '')
else:
row = []
row.extend(self.columns)
align = self.fmt.justify

if truncate_h:
if not self.fmt.index:
row_levels = 0
ins_col = row_levels + self.fmt.tr_col_num
col_row.insert(ins_col, '...')
row.insert(ins_col, '...')

self.write_tr(col_row, indent, self.indent_delta, header=True,
self.write_tr(row, indent, self.indent_delta, header=True,
align=align)

if all((self.fmt.has_index_names,
Expand Down Expand Up @@ -486,24 +471,3 @@ def _write_hierarchical_rows(self, fmt_values, indent):
row.insert(row_levels + self.fmt.tr_col_num, '...')
self.write_tr(row, indent, self.indent_delta, tags=None,
nindex_levels=frame.index.nlevels)


def single_column_table(column, align=None, style=None):
table = '<table'
if align is not None:
table += (' align="{align}"'.format(align=align))
if style is not None:
table += (' style="{style}"'.format(style=style))
table += '><tbody>'
for i in column:
table += ('<tr><td>{i!s}</td></tr>'.format(i=i))
table += '</tbody></table>'
return table


def single_row_table(row): # pragma: no cover
table = '<table><tbody><tr>'
for i in row:
table += ('<td>{i!s}</td>'.format(i=i))
table += '</tr></tbody></table>'
return table

0 comments on commit cc712c9

Please sign in to comment.