Skip to content

API Change repr name for table schema #16204

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 3 commits into from
May 3, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
32 changes: 2 additions & 30 deletions pandas/core/config_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,36 +342,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_'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomAugspurger pls move all of the table_schema code out of config_init into somewhere else; the cb should be really short/simple and not include all of this code

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe pandas.io.formats.printing

_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'):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
32 changes: 32 additions & 0 deletions pandas/io/formats/printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
10 changes: 3 additions & 7 deletions pandas/tests/io/formats/test_printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down