Skip to content

Commit

Permalink
Catch exceptions during deferred selection rendering.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 673078070
  • Loading branch information
danieldjohnson authored and Penzai Developers committed Sep 10, 2024
1 parent c7a6354 commit 766eafc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
7 changes: 6 additions & 1 deletion penzai/core/_treescope_handlers/selection_rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,9 @@ def display_selection_streaming(
visible_selection=visible_selection,
ignore_exceptions=ignore_exceptions,
)
lowering.display_streaming_as_root(rendered_ir, deferreds, roundtrip=False)
lowering.display_streaming_as_root(
rendered_ir,
deferreds,
roundtrip=False,
ignore_exceptions=ignore_exceptions,
)
16 changes: 12 additions & 4 deletions penzai/core/selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1279,35 +1279,43 @@ def insert_before(self, value: Any, and_select: bool = False) -> Any:
)
return self.apply_and_inline(lambda x: (value, x))

def show_selection(self):
def show_selection(self, ignore_exceptions: bool = False):
"""Renders the selection in IPython.
This method is intended to visualize the selection object itself, and
renders boxes around the selected nodes.
This method should only be used when IPython is available.
Args:
ignore_exceptions: Whether to catch errors during rendering and show a
fallback for those subtrees.
"""
# Import selection_rendering here to avoid a circular import.
from penzai.core._treescope_handlers import selection_rendering # pylint: disable=g-import-not-at-top

selection_rendering.display_selection_streaming(
self, visible_selection=True
self, visible_selection=True, ignore_exceptions=ignore_exceptions
)

def show_value(self):
def show_value(self, ignore_exceptions: bool = False):
"""Renders the original tree in IPython, expanding up to the selected nodes.
This method is intended to visualize a value but emphasizing the selected
parts, where the selection is used to determine what to focus on by default
but isn't actually an object we care about.
This method should only be used when IPython is available.
Args:
ignore_exceptions: Whether to catch errors during rendering and show a
fallback for those subtrees.
"""
# Import selection_rendering here to avoid a circular import.
from penzai.core._treescope_handlers import selection_rendering # pylint: disable=g-import-not-at-top

selection_rendering.display_selection_streaming(
self, visible_selection=False
self, visible_selection=False, ignore_exceptions=ignore_exceptions
)

def assert_count_is(self, count: int) -> Selection:
Expand Down

0 comments on commit 766eafc

Please sign in to comment.