From 0cb0452b31431143ba22b7ad41bf4d3d9d878522 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Wed, 30 Jan 2019 14:21:10 -0600 Subject: [PATCH 1/4] Drop IPython<4.0 compat --- pandas/core/frame.py | 10 ---------- pandas/io/formats/console.py | 38 ------------------------------------ 2 files changed, 48 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 28c6f3c23a3ce..8735af840a029 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -640,16 +640,6 @@ def _repr_html_(self): Mainly for IPython notebook. """ - # qtconsole doesn't report its line width, and also - # behaves badly when outputting an HTML table - # that doesn't fit the window, so disable it. - # 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 - if self._info_repr(): buf = StringIO(u("")) self.info(buf=buf) diff --git a/pandas/io/formats/console.py b/pandas/io/formats/console.py index d5ef9f61bc132..ad63b3efdd832 100644 --- a/pandas/io/formats/console.py +++ b/pandas/io/formats/console.py @@ -108,44 +108,6 @@ def check_main(): return check_main() -def in_qtconsole(): - """ - check if we're inside an IPython qtconsole - - .. deprecated:: 0.14.1 - This is no longer needed, or working, in IPython 3 and above. - """ - try: - ip = get_ipython() # noqa - front_end = ( - ip.config.get('KernelApp', {}).get('parent_appname', "") or - ip.config.get('IPKernelApp', {}).get('parent_appname', "")) - if 'qtconsole' in front_end.lower(): - return True - except NameError: - return False - return False - - -def in_ipnb(): - """ - check if we're inside an IPython Notebook - - .. deprecated:: 0.14.1 - This is no longer needed, or working, in IPython 3 and above. - """ - try: - ip = get_ipython() # noqa - front_end = ( - ip.config.get('KernelApp', {}).get('parent_appname', "") or - ip.config.get('IPKernelApp', {}).get('parent_appname', "")) - if 'notebook' in front_end.lower(): - return True - except NameError: - return False - return False - - def in_ipython_frontend(): """ check if we're inside an an IPython zmq frontend From a23fc6eabf9b0f5023b0cf24f88cce733486afa0 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Thu, 31 Jan 2019 07:59:33 -0600 Subject: [PATCH 2/4] Revert "Drop IPython<4.0 compat" This reverts commit 0cb0452b31431143ba22b7ad41bf4d3d9d878522. --- pandas/core/frame.py | 10 ++++++++++ pandas/io/formats/console.py | 38 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index e4970a1e089fb..2049a8aa960bf 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -640,6 +640,16 @@ def _repr_html_(self): Mainly for IPython notebook. """ + # qtconsole doesn't report its line width, and also + # behaves badly when outputting an HTML table + # that doesn't fit the window, so disable it. + # 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 + if self._info_repr(): buf = StringIO(u("")) self.info(buf=buf) diff --git a/pandas/io/formats/console.py b/pandas/io/formats/console.py index ad63b3efdd832..d5ef9f61bc132 100644 --- a/pandas/io/formats/console.py +++ b/pandas/io/formats/console.py @@ -108,6 +108,44 @@ def check_main(): return check_main() +def in_qtconsole(): + """ + check if we're inside an IPython qtconsole + + .. deprecated:: 0.14.1 + This is no longer needed, or working, in IPython 3 and above. + """ + try: + ip = get_ipython() # noqa + front_end = ( + ip.config.get('KernelApp', {}).get('parent_appname', "") or + ip.config.get('IPKernelApp', {}).get('parent_appname', "")) + if 'qtconsole' in front_end.lower(): + return True + except NameError: + return False + return False + + +def in_ipnb(): + """ + check if we're inside an IPython Notebook + + .. deprecated:: 0.14.1 + This is no longer needed, or working, in IPython 3 and above. + """ + try: + ip = get_ipython() # noqa + front_end = ( + ip.config.get('KernelApp', {}).get('parent_appname', "") or + ip.config.get('IPKernelApp', {}).get('parent_appname', "")) + if 'notebook' in front_end.lower(): + return True + except NameError: + return False + return False + + def in_ipython_frontend(): """ check if we're inside an an IPython zmq frontend From 511d86f2c0009ffd905e29459c62a8dff0d4b654 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Thu, 31 Jan 2019 08:17:35 -0600 Subject: [PATCH 3/4] update --- pandas/core/frame.py | 13 ++++++++++--- pandas/tests/io/formats/test_format.py | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 2049a8aa960bf..78c9f2aa96472 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 From acd50bbff5e9e97d2f338f88096e65ab9f3a3747 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Thu, 31 Jan 2019 08:19:03 -0600 Subject: [PATCH 4/4] whatsnew --- doc/source/whatsnew/v0.24.1.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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: