Skip to content

DataFrame_repr_html changes according to #772 discussion. #829

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 1 commit into from
Feb 27, 2012
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
10 changes: 9 additions & 1 deletion pandas/core/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,8 @@ def _has_names(index):
# Global formatting options

def set_printoptions(precision=None, column_space=None, max_rows=None,
max_columns=None, colheader_justify='right'):
max_columns=None, colheader_justify='right',
notebook_repr_html=None):
"""
Alter default behavior of DataFrame.toString

Expand All @@ -597,6 +598,10 @@ def set_printoptions(precision=None, column_space=None, max_rows=None,
Either one, or both can be set to 0 (experimental). Pandas will figure
out how big the terminal is and will not display more rows or/and
columns that can fit on it.
colheader_justify
notebook_repr_html : boolean
When True (default), IPython notebook will use html representation for
pandas objects (if it is available).
"""
if precision is not None:
print_config.precision = precision
Expand All @@ -608,6 +613,8 @@ def set_printoptions(precision=None, column_space=None, max_rows=None,
print_config.max_columns = max_columns
if colheader_justify is not None:
print_config.colheader_justify = colheader_justify
if notebook_repr_html is not None:
print_config.notebook_repr_html = notebook_repr_html

def reset_printoptions():
print_config.reset()
Expand Down Expand Up @@ -733,6 +740,7 @@ def __init__(self):
self.max_rows = 200
self.max_columns = 0
self.colheader_justify = 'right'
self.notebook_repr_html = True

def reset(self):
self.__init__()
Expand Down
54 changes: 35 additions & 19 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,10 @@ def __nonzero__(self):
# e.g. "if frame: ..."
return len(self.columns) > 0 and len(self.index) > 0

def __repr__(self):
def _need_info_repr_(self):
"""
Return a string representation for a particular DataFrame
Check if it is needed to use info/summary view to represent a
particular DataFrame.
"""
config = fmt.print_config

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

buf = StringIO()
if max_columns > 0:
if len(self.index) < max_rows and \
if len(self.index) <= max_rows and \
len(self.columns) <= max_columns:
self.to_string(buf=buf)
return False
else:
self.info(buf=buf, verbose=self._verbose_info)
return True
else:
if len(self.index) > max_rows:
self.info(buf=buf, verbose=self._verbose_info)
return True
else:
buf = StringIO()
self.to_string(buf=buf)
value = buf.getvalue()
if max([len(l) for l in value.split('\n')]) > terminal_width:
buf = StringIO()
self.info(buf=buf, verbose=self._verbose_info)
value = buf.getvalue()
return com.console_encode(value)
return com.console_encode(buf.getvalue())
return True
else:
return False

def __repr__(self):
"""
Return a string representation for a particular DataFrame
"""
buf = StringIO()
if self._need_info_repr_():
self.info(buf=buf, verbose=self._verbose_info)
else:
self.to_string(buf=buf)
value = buf.getvalue()
return com.console_encode(value)

def _repr_html_(self):
if len(self.index) <= 1000 and len(self.columns)<= 20:
return ('<div style="max-height:1000px;'
'max-width:1500px;overflow:auto;">' +
self.to_html() + '</div>')
"""
Return a html representation for a particular DataFrame.
Mainly for IPython notebook.
"""
if fmt.print_config.notebook_repr_html:
if self._need_info_repr_():
return None
else:
return ('<div style="max-height:1000px;'
'max-width:1500px;overflow:auto;">\n' +
self.to_html() + '\n</div>')
else:
buf = StringIO()
self.info(buf=buf, verbose=self._verbose_info)
return '<pre>' + com.console_encode(buf.getvalue()) + '</pre>'
return None

def __iter__(self):
"""
Expand Down