diff --git a/pandas/core/format.py b/pandas/core/format.py index 4e03a5c68b2b3..f210b801d8cee 100644 --- a/pandas/core/format.py +++ b/pandas/core/format.py @@ -12,6 +12,7 @@ from pandas.core.index import Index, MultiIndex, _ensure_index from pandas.util import py3compat from pandas.util.compat import OrderedDict +from pandas.util.terminal import get_terminal_size from pandas.core.config import get_option, set_option, reset_option import pandas.core.common as com import pandas.lib as lib @@ -1665,6 +1666,19 @@ def detect_console_encoding(): return encoding +def get_console_size(): + """Return console size as tuple = (width, height).""" + display_width = get_option('display.width') + display_height = get_option('display.height') + + if com.in_interactive_session(): + terminal_width, terminal_height = get_terminal_size() + else: + terminal_width, terminal_height = 80, 100 + + return (display_width or terminal_width, display_height or terminal_height) + + class EngFormatter(object): """ Formats float values according to engineering format. diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 2a1ee06984aa8..a457430d2f563 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -605,15 +605,11 @@ def _repr_fits_vertical_(self): options height and max_columns. In case off non-interactive session, no boundaries apply. """ - if not com.in_interactive_session(): - return True - - terminal_width, terminal_height = get_terminal_size() + width, height = fmt.get_console_size() # excluding column axis area - max_rows = get_option("display.max_rows") or terminal_height - display_height = get_option("display.height") or terminal_height - return len(self.index) <= min(max_rows, display_height) + max_rows = get_option("display.max_rows") or height + return len(self) <= min(max_rows, height) def _repr_fits_horizontal_(self): """ @@ -621,23 +617,19 @@ def _repr_fits_horizontal_(self): options width and max_columns. In case off non-interactive session, no boundaries apply. """ - if not com.in_interactive_session(): - return True - - terminal_width, terminal_height = get_terminal_size() + width, height = fmt.get_console_size() max_columns = get_option("display.max_columns") - display_width = get_option("display.width") or terminal_width nb_columns = len(self.columns) if ((max_columns and nb_columns > max_columns) or - (nb_columns > (display_width // 2))): + (nb_columns > (width // 2))): return False buf = StringIO() self.to_string(buf=buf) value = buf.getvalue() repr_width = max([len(l) for l in value.split('\n')]) - return repr_width <= display_width + return repr_width <= width def __str__(self): """ @@ -670,19 +662,21 @@ def __unicode__(self): """ buf = StringIO(u"") fits_vertical = self._repr_fits_vertical_() - fits_horizontal = self._repr_fits_horizontal_() + fits_horizontal = False + if fits_vertical: + fits_horizontal = self._repr_fits_horizontal_() + if fits_vertical and fits_horizontal: self.to_string(buf=buf) else: - terminal_width, terminal_height = get_terminal_size() - max_rows = get_option("display.max_rows") or terminal_height + width, height = fmt.get_console_size() + max_rows = get_option("display.max_rows") or height # Expand or info? Decide based on option display.expand_frame_repr # and keep it sane for the number of display rows used by the # expanded repr. if (get_option("display.expand_frame_repr") and fits_vertical and len(self.columns) < max_rows): - line_width = get_option("display.width") or terminal_width - self.to_string(buf=buf, line_width=line_width) + self.to_string(buf=buf, line_width=width) else: max_info_rows = get_option('display.max_info_rows') verbose = (max_info_rows is None or @@ -711,7 +705,12 @@ def _repr_html_(self): raise ValueError('Disable HTML output in QtConsole') if get_option("display.notebook_repr_html"): - if self._repr_fits_horizontal_() and self._repr_fits_vertical_(): + fits_vertical = self._repr_fits_vertical_() + fits_horizontal = False + if fits_vertical: + fits_horizontal = self._repr_fits_horizontal_() + + if fits_horizontal and fits_vertical: return ('