Skip to content

BUG: Restore return value in notebook reprs #16171

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 1, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
44 changes: 43 additions & 1 deletion pandas/core/config_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,48 @@ def mpl_style_cb(key):
return val


def table_schema_cb(key):
# Having _ipython_display_ defined messes with the return value
# from cells, so the Out[x] dicitonary breaks.
Copy link
Contributor

Choose a reason for hiding this comment

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

don't write the full method here
instead inside the cb import a method a call it

Copy link
Contributor

Choose a reason for hiding this comment

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

actually i would create this dynamically
simply define 2 methods and have the cb just set one of them

we do in set_use_numexpr just like this

Copy link
Contributor

Choose a reason for hiding this comment

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

would not create dynamically (typo)

# Currently table schema is the only thing using it, so we'll
# monkey patch `_ipython_displa_` onto NDFrame when config option
# is set
# see https://github.com/pandas-dev/pandas/issues/16168
from pandas.core.generic import NDFrame

def _ipython_display_(self):
from pandas.errors import UnserializableWarning

try:
from IPython.display import display
except ImportError:
return None

# Series doesn't define _repr_html_ or _repr_latex_
latex = self._repr_latex_() if hasattr(self, '_repr_latex_') else None
html = self._repr_html_() if hasattr(self, '_repr_html_') else None
try:
table_schema = self._repr_table_schema_()
except Exception as e:
warnings.warn("Cannot create table schema representation. "
"{}".format(e), UnserializableWarning)
table_schema = None
# We need the inital newline since we aren't going through the
# usual __repr__. See
# https://github.com/pandas-dev/pandas/pull/14904#issuecomment-277829277
text = "\n" + repr(self)

reprs = {"text/plain": text, "text/html": html, "text/latex": latex,
"application/vnd.dataresource+json": table_schema}
reprs = {k: v for k, v in reprs.items() if v}
display(reprs, raw=True)

if cf.get_option(key):
NDFrame._ipython_display_ = _ipython_display_
elif getattr(NDFrame, '_ipython_display_', None):
del NDFrame._ipython_display_


with cf.config_prefix('display'):
cf.register_option('precision', 6, pc_precision_doc, validator=is_int)
cf.register_option('float_format', None, float_format_doc,
Expand Down Expand Up @@ -374,7 +416,7 @@ def mpl_style_cb(key):
cf.register_option('latex.multirow', False, pc_latex_multirow,
validator=is_bool)
cf.register_option('html.table_schema', False, pc_table_schema_doc,
validator=is_bool)
validator=is_bool, cb=table_schema_cb)


cf.deprecate_option('display.line_width',
Expand Down
30 changes: 6 additions & 24 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,30 +130,12 @@ def __init__(self, data, axes=None, copy=False, dtype=None,
object.__setattr__(self, '_data', data)
object.__setattr__(self, '_item_cache', {})

def _ipython_display_(self):
try:
from IPython.display import display
except ImportError:
return None

# Series doesn't define _repr_html_ or _repr_latex_
latex = self._repr_latex_() if hasattr(self, '_repr_latex_') else None
html = self._repr_html_() if hasattr(self, '_repr_html_') else None
try:
table_schema = self._repr_table_schema_()
except Exception as e:
warnings.warn("Cannot create table schema representation. "
"{}".format(e), UnserializableWarning)
table_schema = None
# We need the inital newline since we aren't going through the
# usual __repr__. See
# https://github.com/pandas-dev/pandas/pull/14904#issuecomment-277829277
text = "\n" + repr(self)

reprs = {"text/plain": text, "text/html": html, "text/latex": latex,
"application/vnd.dataresource+json": table_schema}
reprs = {k: v for k, v in reprs.items() if v}
display(reprs, raw=True)
# def _repr_ipython_(self):
# This is defined in config_init.py and monkey patched
# onto NDFrame, depending on the display.html.table_schema
# setting. Defining a _repr_iptyhon_ means notebook cells to not
# have a return value.
# See https://github.com/ipython/ipython/issues/10491

def _repr_table_schema_(self):
"""
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/io/formats/test_printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,22 @@ def test_config_default_off(self):

assert result is None

def test_config_monkeypatches(self):
# GH 10491
df = pd.DataFrame({"A": [1, 2]})
assert not hasattr(df, '_ipython_display_')
assert not hasattr(df['A'], '_ipython_display_')

with pd.option_context('display.html.table_schema', True):
assert hasattr(df, '_ipython_display_')
# smoke test that it works
df._ipython_display_()
assert hasattr(df['A'], '_ipython_display_')
df['A']._ipython_display_()

assert not hasattr(df, '_ipython_display_')
assert not hasattr(df['A'], '_ipython_display_')


# TODO: fix this broken test

Expand Down