TreescopeRenderer#

class penzai.treescope.renderer.TreescopeRenderer[source]#

Bases: object

A renderer object with a configurable set of handlers.

Variables:
  • handlers (list[TreescopeNodeHandler]) – List of handlers, ordered from most specific to least specific. Handlers will be tried in order until one returns something other than NotImplemented.

  • wrapper_hooks (list[TreescopeCustomWrapperHook]) – List of wrapper hooks, ordered from outermost to innermost. All wrapper hooks will run in order on every node.

  • context_builders (list[Callable[[], contextlib.AbstractContextManager[Any] | None]]) – List of functions that build rendering context managers. Each of these context managers will be entered before rendering the tree, and will be exited after converting the returned tags to an HTML string. The main use for this is to set up shared state needed by the handlers or wrapper hooks (e.g. using a penzai.ContextualValue). Context builders can also return None instead of a context manager, if they just need to do processing at the start of rendering and don’t need to clean up.

Methods

__init__(handlers, wrapper_hooks, ...)

extended_with(*[, handlers, wrapper_hooks, ...])

Extends a renderer with additional steps, returning a new renderer.

to_foldable_representation(value[, ...])

Renders an object to the foldable intermediate representation.

to_html(value[, roundtrip_mode])

Convenience method to render an object to HTML.

to_text(value[, roundtrip_mode])

Convenience method to render an object to text.

Attributes

handlers

wrapper_hooks

context_builders

extended_with(*, handlers: Iterable[TreescopeNodeHandler] = (), wrapper_hooks: Iterable[TreescopeCustomWrapperHook] = (), context_builders: Iterable[Callable[[], contextlib.AbstractContextManager[Any] | None]] = ()) TreescopeRenderer[source]#

Extends a renderer with additional steps, returning a new renderer.

Parameters:
  • handlers – New handlers to insert. These will be inserted at the beginning of the handler list, to allow them to override existing handlers.

  • wrapper_hooks – New wrapper hooks. These will be inserted before the existing wrapper hooks, to allow them to override existing hooks.

  • context_builders – New context builders. These will be inserted after the existing context builders.

Returns:

A copy of this renderer extended with the new parts.

to_foldable_representation(value: Any, ignore_exceptions: bool = False, root_keypath: tuple[Any, ...] | None = ()) part_interface.RenderableAndLineAnnotations[source]#

Renders an object to the foldable intermediate representation.

Parameters:
  • value – Value to render.

  • ignore_exceptions – Whether to catch errors during rendering of subtrees and show a fallback for those subtrees, instead of failing the entire renderer. Best used in contexts where to_foldable_representation is not being called directly by the user, e.g. when registering this as a default pretty-printer.

  • root_keypath – Keypath to the value being rendered. Can be None to disable all keypaths during rendering.

Returns:

Renderable representation of the object.

to_html(value: Any, roundtrip_mode: bool = False) str[source]#

Convenience method to render an object to HTML.

Parameters:
  • value – Value to render.

  • roundtrip_mode – Whether to render in roundtrip mode.

Returns:

HTML source code for the foldable representation of the object.

to_text(value: Any, roundtrip_mode: bool = False) str[source]#

Convenience method to render an object to text.

Parameters:
  • value – Value to render.

  • roundtrip_mode – Whether to render in roundtrip mode.

Returns:

A text representation of the object.