From b5895ed3383ef7615dedad87225531d2b58ad9e9 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Thu, 31 Jan 2019 10:02:37 -0600 Subject: [PATCH] Backport PR #25039: BUG: avoid usage in_qtconsole for recent IPython versions --- doc/source/whatsnew/v0.24.1.rst | 2 +- pandas/core/frame.py | 13 ++++++++++--- pandas/tests/io/formats/test_format.py | 15 +++++++++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v0.24.1.rst b/doc/source/whatsnew/v0.24.1.rst index 047404e93914b..521319c55a503 100644 --- a/doc/source/whatsnew/v0.24.1.rst +++ b/doc/source/whatsnew/v0.24.1.rst @@ -83,7 +83,7 @@ Bug Fixes **Other** -- +- Fixed AttributeError when printing a DataFrame's HTML repr after accessing the IPython config object (:issue:`25036`) - .. _whatsnew_0.241.contributors: diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 28c6f3c23a3ce..5b462b949abf9 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -17,6 +17,7 @@ import itertools import sys import warnings +from distutils.version import LooseVersion from textwrap import dedent import numpy as np @@ -646,9 +647,15 @@ def _repr_html_(self): # XXX: In IPython 3.x and above, the Qt console will not attempt to # display HTML, so this check can be removed when support for # IPython 2.x is no longer needed. - if console.in_qtconsole(): - # 'HTML output is disabled in QtConsole' - return None + try: + import IPython + except ImportError: + pass + else: + if LooseVersion(IPython.__version__) < LooseVersion('3.0'): + if console.in_qtconsole(): + # 'HTML output is disabled in QtConsole' + return None if self._info_repr(): buf = StringIO(u("")) diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index 5d922ccaf1fd5..b0cf5a2f17609 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -12,6 +12,7 @@ import os import re import sys +import textwrap import warnings import dateutil @@ -2777,3 +2778,17 @@ def test_format_percentiles(): fmt.format_percentiles([2, 0.1, 0.5]) with pytest.raises(ValueError, match=msg): fmt.format_percentiles([0.1, 0.5, 'a']) + + +def test_repr_html_ipython_config(ip): + code = textwrap.dedent("""\ + import pandas as pd + df = pd.DataFrame({"A": [1, 2]}) + df._repr_html_() + + cfg = get_ipython().config + cfg['IPKernelApp']['parent_appname'] + df._repr_html_() + """) + result = ip.run_cell(code) + assert not result.error_in_exec