Skip to content

CLN: io/formats/html.py: refactor #23818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ Backwards incompatible API changes
- A newly constructed empty :class:`DataFrame` with integer as the ``dtype`` will now only be cast to ``float64`` if ``index`` is specified (:issue:`22858`)
- :meth:`Series.str.cat` will now raise if `others` is a `set` (:issue:`23009`)
- Passing scalar values to :class:`DatetimeIndex` or :class:`TimedeltaIndex` will now raise ``TypeError`` instead of ``ValueError`` (:issue:`23539`)
- ``max_rows`` and ``max_cols`` parameters removed from :class:`HTMLFormatter` since truncation is handled by :class:`DataFrameFormatter` (:issue:`23818`)

.. _whatsnew_0240.api_breaking.deps:

Expand Down
8 changes: 2 additions & 6 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,12 +730,8 @@ def to_html(self, classes=None, notebook=False, border=None):
.. versionadded:: 0.19.0
"""
from pandas.io.formats.html import HTMLFormatter
html_renderer = HTMLFormatter(self, classes=classes,
max_rows=self.max_rows,
max_cols=self.max_cols,
notebook=notebook,
border=border,
table_id=self.table_id)
html_renderer = HTMLFormatter(self, classes=classes, notebook=notebook,
border=border, table_id=self.table_id)
if hasattr(self.buf, 'write'):
html_renderer.write_result(self.buf)
elif isinstance(self.buf, compat.string_types):
Expand Down
37 changes: 16 additions & 21 deletions pandas/io/formats/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class HTMLFormatter(TableFormatter):

indent_delta = 2

def __init__(self, formatter, classes=None, max_rows=None, max_cols=None,
notebook=False, border=None, table_id=None):
def __init__(self, formatter, classes=None, notebook=False, border=None,
table_id=None):
self.fmt = formatter
self.classes = classes

Expand All @@ -35,18 +35,21 @@ def __init__(self, formatter, classes=None, max_rows=None, max_cols=None,
self.elements = []
self.bold_rows = self.fmt.kwds.get('bold_rows', False)
self.escape = self.fmt.kwds.get('escape', True)

self.max_rows = max_rows or len(self.fmt.frame)
self.max_cols = max_cols or len(self.fmt.columns)
self.show_dimensions = self.fmt.show_dimensions
self.is_truncated = (self.max_rows < len(self.fmt.frame) or
self.max_cols < len(self.fmt.columns))
self.notebook = notebook
if border is None:
border = get_option('display.html.border')
self.border = border
self.table_id = table_id

@property
def is_truncated(self):
return self.fmt.is_truncated

@property
def ncols(self):
return len(self.fmt.tr_frame.columns)

def write(self, s, indent=0):
rs = pprint_thing(s)
self.elements.append(' ' * indent + rs)
Expand Down Expand Up @@ -301,12 +304,8 @@ def _write_header(self, indent):
if all((self.fmt.has_index_names,
self.fmt.index,
self.fmt.show_index_names)):
row = ([x if x is not None else ''
for x in self.frame.index.names] +
[''] * min(len(self.columns), self.max_cols))
if truncate_h:
ins_col = row_levels + self.fmt.tr_col_num
row.insert(ins_col, '')
row = ([x if x is not None else '' for x in self.frame.index.names]
+ [''] * (self.ncols + (1 if truncate_h else 0)))
self.write_tr(row, indent, self.indent_delta, header=True)

indent -= self.indent_delta
Expand All @@ -318,9 +317,7 @@ def _write_body(self, indent):
self.write('<tbody>', indent)
indent += self.indent_delta

fmt_values = {}
for i in range(min(len(self.columns), self.max_cols)):
fmt_values[i] = self.fmt._format_col(i)
fmt_values = {i: self.fmt._format_col(i) for i in range(self.ncols)}

# write values
if self.fmt.index and isinstance(self.frame.index, ABCMultiIndex):
Expand All @@ -338,7 +335,6 @@ def _write_regular_rows(self, fmt_values, indent):
truncate_h = self.fmt.truncate_h
truncate_v = self.fmt.truncate_v

ncols = len(self.fmt.tr_frame.columns)
nrows = len(self.fmt.tr_frame)

if self.fmt.index:
Expand All @@ -362,7 +358,7 @@ def _write_regular_rows(self, fmt_values, indent):
row = []
if self.fmt.index:
row.append(index_values[i])
row.extend(fmt_values[j][i] for j in range(ncols))
row.extend(fmt_values[j][i] for j in range(self.ncols))

if truncate_h:
dot_col_ix = self.fmt.tr_col_num + row_levels
Expand All @@ -376,7 +372,6 @@ def _write_hierarchical_rows(self, fmt_values, indent):
truncate_h = self.fmt.truncate_h
truncate_v = self.fmt.truncate_v
frame = self.fmt.tr_frame
ncols = len(frame.columns)
nrows = len(frame)
row_levels = self.frame.index.nlevels

Expand Down Expand Up @@ -454,7 +449,7 @@ def _write_hierarchical_rows(self, fmt_values, indent):
j += 1
row.append(v)

row.extend(fmt_values[j][i] for j in range(ncols))
row.extend(fmt_values[j][i] for j in range(self.ncols))
if truncate_h:
row.insert(row_levels - sparse_offset +
self.fmt.tr_col_num, '...')
Expand All @@ -466,7 +461,7 @@ def _write_hierarchical_rows(self, fmt_values, indent):
sparsify=False, adjoin=False, names=False)))
row = []
row.extend(idx_values[i])
row.extend(fmt_values[j][i] for j in range(ncols))
row.extend(fmt_values[j][i] for j in range(self.ncols))
if truncate_h:
row.insert(row_levels + self.fmt.tr_col_num, '...')
self.write_tr(row, indent, self.indent_delta, tags=None,
Expand Down