Skip to content
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

Check for _repr_mimebundle_ when using Treescope as the default pretty-printer. #21

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Changes from all 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
65 changes: 37 additions & 28 deletions treescope/_internal/api/ipython_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,36 +264,45 @@ def _render_for_ipython(value):
value, "_repr_html_"
)
if repr_html_method:
# Directly call the _repr_html_ method. We need to do this because if this
# formatter returns None, the _repr_html_ method won't be called. (The
# _repr_html_ method is only called if no formatter for the type is
# found, but this formatter will run for every type due to being
# registered for `object`.)
return repr_html_method() # pylint: disable=protected-access
elif isinstance(value, ipython_display.DisplayObject) or (
object_inspection.safely_get_real_method(value, "_repr_pretty_")
and not (
object_inspection.safely_get_real_method(
value, "__treescope_repr__"
)
or object_inspection.safely_get_real_method(
value, "__treescope_root_repr__"
)
)
):
# Don't render this to HTML.

if isinstance(value, ipython_display.DisplayObject):
# Don't render this to HTML, since IPython already knows how to render it.
return None
else:
output_stealer = _display_and_maybe_steal(
value=value,
ignore_exceptions=True,
roundtrip_mode=False,
streaming=streaming,
compress_html=compress_html,
stealable=True,
)
# Executing the above call will have already displayed the output,
# but it may be in the wrong place (e.g. it may appear before the
# actual "Out" marker in JupyterLab). In `stealable` mode, by returning
# `output_stealer` as the rendering of the object, we can ensure that the
# output is moved to the right place.
assert output_stealer is not None
return output_stealer

has_ipython_format_method = any(
object_inspection.safely_get_real_method(value, method_name)
for method_name in ("_repr_pretty_", "_repr_mimebundle_")
)
has_treescope_format_method = any(
object_inspection.safely_get_real_method(value, method_name)
for method_name in ("__treescope_repr__", "__treescope_root_repr__")
)
if has_ipython_format_method and not has_treescope_format_method:
# Don't render this with Treescope, because it already has a different
# rendering method for a non-HTML format.
return None

output_stealer = _display_and_maybe_steal(
value=value,
ignore_exceptions=True,
roundtrip_mode=False,
streaming=streaming,
compress_html=compress_html,
stealable=True,
)
# Executing the above call will have already displayed the output,
# but it may be in the wrong place (e.g. it may appear before the
# actual "Out" marker in JupyterLab). In `stealable` mode, by returning
# `output_stealer` as the rendering of the object, we can ensure that the
# output is moved to the right place.
assert output_stealer is not None
return output_stealer

display_formatter = IPython.get_ipython().display_formatter
cur_html_formatter = display_formatter.formatters["text/html"]
Expand Down
Loading