Skip to content

Commit

Permalink
Don't support _ipython_display_ rich repr. Too complicated atm!
Browse files Browse the repository at this point in the history
  • Loading branch information
eriknw committed Sep 27, 2021
1 parent c58b17b commit 403e9e5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
18 changes: 3 additions & 15 deletions afar/_core.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Define the user-facing `run` object; this is where it all comes together."""
import dis
import sys
import traceback
from inspect import currentframe
from uuid import uuid4
from weakref import WeakKeyDictionary, WeakSet
Expand Down Expand Up @@ -345,22 +344,11 @@ def run_afar(magic_func, names, futures, capture_print, channel, unique_key):
if magic_func._display_expr and worker is not None:
# Hopefully computing the repr is fast. If it is slow, perhaps it would be
# better to add the return value to rv and call repr_afar as a separate task.
# Also, pretty_repr must be msgpack serializable if done via events.
# Hence, custom _ipython_display_ probably won't work, and we resort to
# trying to use a basic repr (if that fails, we show the first exception).
# Also, pretty_repr must be msgpack serializable if done via events. Hence,
# custom _ipython_display_ doesn't work, and we resort to using a basic repr.
pretty_repr = repr_afar(results.return_value, magic_func._repr_methods)
if pretty_repr is not None:
try:
worker.log_event(channel, (unique_key, "display_expr", pretty_repr))
except Exception:
exc_info = sys.exc_info()
tb = traceback.format_exception(*exc_info)
try:
basic_repr = (repr(results.return_value), "__repr__", False)
worker.log_event(channel, (unique_key, "display_expr", basic_repr))
except Exception:
exc_repr = (tb, pretty_repr[1], True)
worker.log_event(channel, (unique_key, "display_expr", exc_repr))
worker.log_event(channel, (unique_key, "display_expr", pretty_repr))
send_finish = False
finally:
if capture_print and worker is not None and send_finish:
Expand Down
6 changes: 5 additions & 1 deletion afar/_reprs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ def __init__(self):
self._attrs = []

def __getattr__(self, attr):
if "canary" not in attr:
if "canary" not in attr and attr != "_ipython_display_":
# _ipython_display_ requires sending the object back to the client.
# Let's not bother with this hassle for now.
self._attrs.append(attr)
raise AttributeError(attr)

Expand Down Expand Up @@ -44,6 +46,7 @@ def repr_afar(val, repr_methods):
continue
if method_name == "_ipython_display_":
# Custom display! Send the object to the client
# We don't allow _ipython_display_ at the moment
return val, method_name, False
try:
rv = method()
Expand Down Expand Up @@ -100,6 +103,7 @@ def display_repr(results, out=None):
from IPython.display import display

if method_name == "_ipython_display_":
# We don't allow _ipython_display_ at the moment
if out is None:
display(val)
else:
Expand Down

0 comments on commit 403e9e5

Please sign in to comment.