Skip to content

Commit e873a2e

Browse files
committed
Merge pull request #829 from lodagro/repr_html
DataFrame_repr_html changes according to #772 discussion.
2 parents 3beba2c + ca357c7 commit e873a2e

File tree

2 files changed

+44
-20
lines changed

2 files changed

+44
-20
lines changed

pandas/core/format.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,8 @@ def _has_names(index):
581581
# Global formatting options
582582

583583
def set_printoptions(precision=None, column_space=None, max_rows=None,
584-
max_columns=None, colheader_justify='right'):
584+
max_columns=None, colheader_justify='right',
585+
notebook_repr_html=None):
585586
"""
586587
Alter default behavior of DataFrame.toString
587588
@@ -597,6 +598,10 @@ def set_printoptions(precision=None, column_space=None, max_rows=None,
597598
Either one, or both can be set to 0 (experimental). Pandas will figure
598599
out how big the terminal is and will not display more rows or/and
599600
columns that can fit on it.
601+
colheader_justify
602+
notebook_repr_html : boolean
603+
When True (default), IPython notebook will use html representation for
604+
pandas objects (if it is available).
600605
"""
601606
if precision is not None:
602607
print_config.precision = precision
@@ -608,6 +613,8 @@ def set_printoptions(precision=None, column_space=None, max_rows=None,
608613
print_config.max_columns = max_columns
609614
if colheader_justify is not None:
610615
print_config.colheader_justify = colheader_justify
616+
if notebook_repr_html is not None:
617+
print_config.notebook_repr_html = notebook_repr_html
611618

612619
def reset_printoptions():
613620
print_config.reset()
@@ -733,6 +740,7 @@ def __init__(self):
733740
self.max_rows = 200
734741
self.max_columns = 0
735742
self.colheader_justify = 'right'
743+
self.notebook_repr_html = True
736744

737745
def reset(self):
738746
self.__init__()

pandas/core/frame.py

+35-19
Original file line numberDiff line numberDiff line change
@@ -435,9 +435,10 @@ def __nonzero__(self):
435435
# e.g. "if frame: ..."
436436
return len(self.columns) > 0 and len(self.index) > 0
437437

438-
def __repr__(self):
438+
def _need_info_repr_(self):
439439
"""
440-
Return a string representation for a particular DataFrame
440+
Check if it is needed to use info/summary view to represent a
441+
particular DataFrame.
441442
"""
442443
config = fmt.print_config
443444

@@ -446,35 +447,50 @@ def __repr__(self):
446447
else config.max_rows)
447448
max_columns = config.max_columns
448449

449-
buf = StringIO()
450450
if max_columns > 0:
451-
if len(self.index) < max_rows and \
451+
if len(self.index) <= max_rows and \
452452
len(self.columns) <= max_columns:
453-
self.to_string(buf=buf)
453+
return False
454454
else:
455-
self.info(buf=buf, verbose=self._verbose_info)
455+
return True
456456
else:
457457
if len(self.index) > max_rows:
458-
self.info(buf=buf, verbose=self._verbose_info)
458+
return True
459459
else:
460+
buf = StringIO()
460461
self.to_string(buf=buf)
461462
value = buf.getvalue()
462463
if max([len(l) for l in value.split('\n')]) > terminal_width:
463-
buf = StringIO()
464-
self.info(buf=buf, verbose=self._verbose_info)
465-
value = buf.getvalue()
466-
return com.console_encode(value)
467-
return com.console_encode(buf.getvalue())
464+
return True
465+
else:
466+
return False
467+
468+
def __repr__(self):
469+
"""
470+
Return a string representation for a particular DataFrame
471+
"""
472+
buf = StringIO()
473+
if self._need_info_repr_():
474+
self.info(buf=buf, verbose=self._verbose_info)
475+
else:
476+
self.to_string(buf=buf)
477+
value = buf.getvalue()
478+
return com.console_encode(value)
468479

469480
def _repr_html_(self):
470-
if len(self.index) <= 1000 and len(self.columns)<= 20:
471-
return ('<div style="max-height:1000px;'
472-
'max-width:1500px;overflow:auto;">' +
473-
self.to_html() + '</div>')
481+
"""
482+
Return a html representation for a particular DataFrame.
483+
Mainly for IPython notebook.
484+
"""
485+
if fmt.print_config.notebook_repr_html:
486+
if self._need_info_repr_():
487+
return None
488+
else:
489+
return ('<div style="max-height:1000px;'
490+
'max-width:1500px;overflow:auto;">\n' +
491+
self.to_html() + '\n</div>')
474492
else:
475-
buf = StringIO()
476-
self.info(buf=buf, verbose=self._verbose_info)
477-
return '<pre>' + com.console_encode(buf.getvalue()) + '</pre>'
493+
return None
478494

479495
def __iter__(self):
480496
"""

0 commit comments

Comments
 (0)