diff --git a/pandas/core/config_init.py b/pandas/core/config_init.py index 81fb8090a7afe..7e6ffaaffb72b 100644 --- a/pandas/core/config_init.py +++ b/pandas/core/config_init.py @@ -9,7 +9,6 @@ module is imported, register them here rather then in the module. """ -import sys import warnings import pandas.core.config as cf @@ -342,36 +341,8 @@ def mpl_style_cb(key): def table_schema_cb(key): - # first, check if we are in IPython - if 'IPython' not in sys.modules: - # definitely not in IPython - return - from IPython import get_ipython - ip = get_ipython() - if ip is None: - # still not in IPython - return - - formatters = ip.display_formatter.formatters - - mimetype = "application/vnd.dataresource+json" - - if cf.get_option(key): - if mimetype not in formatters: - # define tableschema formatter - from IPython.core.formatters import BaseFormatter - - class TableSchemaFormatter(BaseFormatter): - print_method = '_repr_table_schema_' - _return_type = (dict,) - # register it: - formatters[mimetype] = TableSchemaFormatter() - # enable it if it's been disabled: - formatters[mimetype].enabled = True - else: - # unregister tableschema mime-type - if mimetype in formatters: - formatters[mimetype].enabled = False + from pandas.io.formats.printing import _enable_data_resource_formatter + _enable_data_resource_formatter(cf.get_option(key)) with cf.config_prefix('display'): diff --git a/pandas/core/generic.py b/pandas/core/generic.py index b3498583f6e14..2bc64795b5f20 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -129,7 +129,7 @@ def __init__(self, data, axes=None, copy=False, dtype=None, object.__setattr__(self, '_data', data) object.__setattr__(self, '_item_cache', {}) - def _repr_table_schema_(self): + def _repr_data_resource_(self): """ Not a real Jupyter special repr method, but we use the same naming convention. diff --git a/pandas/io/formats/printing.py b/pandas/io/formats/printing.py index 5ea47df2c817f..cbad603630bd3 100644 --- a/pandas/io/formats/printing.py +++ b/pandas/io/formats/printing.py @@ -2,6 +2,7 @@ printing tools """ +import sys from pandas.core.dtypes.inference import is_sequence from pandas import compat from pandas.compat import u @@ -233,3 +234,34 @@ def as_escaped_unicode(thing, escape_chars=escape_chars): def pprint_thing_encoded(object, encoding='utf-8', errors='replace', **kwds): value = pprint_thing(object) # get unicode representation of object return value.encode(encoding, errors, **kwds) + + +def _enable_data_resource_formatter(enable): + if 'IPython' not in sys.modules: + # definitely not in IPython + return + from IPython import get_ipython + ip = get_ipython() + if ip is None: + # still not in IPython + return + + formatters = ip.display_formatter.formatters + mimetype = "application/vnd.dataresource+json" + + if enable: + if mimetype not in formatters: + # define tableschema formatter + from IPython.core.formatters import BaseFormatter + + class TableSchemaFormatter(BaseFormatter): + print_method = '_repr_data_resource_' + _return_type = (dict,) + # register it: + formatters[mimetype] = TableSchemaFormatter() + # enable it if it's been disabled: + formatters[mimetype].enabled = True + else: + # unregister tableschema mime-type + if mimetype in formatters: + formatters[mimetype].enabled = False diff --git a/pandas/tests/io/formats/test_printing.py b/pandas/tests/io/formats/test_printing.py index 3acd5c7a5e8c5..44fbd5a958d8c 100644 --- a/pandas/tests/io/formats/test_printing.py +++ b/pandas/tests/io/formats/test_printing.py @@ -180,23 +180,19 @@ def test_publishes_not_implemented(self): def test_config_on(self): df = pd.DataFrame({"A": [1, 2]}) with pd.option_context("display.html.table_schema", True): - result = df._repr_table_schema_() + result = df._repr_data_resource_() assert result is not None def test_config_default_off(self): df = pd.DataFrame({"A": [1, 2]}) with pd.option_context("display.html.table_schema", False): - result = df._repr_table_schema_() + result = df._repr_data_resource_() assert result is None - def test_config_monkeypatches(self): + def test_enable_data_resource_formatter(self): # GH 10491 - df = pd.DataFrame({"A": [1, 2]}) - assert not hasattr(df, '_ipython_display_') - assert not hasattr(df['A'], '_ipython_display_') - formatters = self.display_formatter.formatters mimetype = 'application/vnd.dataresource+json'