Code Monkey home page Code Monkey logo

Comments (3)

danieldjohnson avatar danieldjohnson commented on June 12, 2024 1

Sounds good, if that works for you then that's the approach I'd recommend. I'll go ahead and close this issue then.

To register treescope as the default pretty printer and also change the handler, you could run something like this:

# Install things in IPython
pz.ts.register_as_default()
pz.ts.register_autovisualize_magic()

# Enable global overrides
pz.enable_interactive_context()

# Configure the custom handler
my_custom_renderer = pz.ts.active_renderer.get().extended_with(<stuff here>)
pz.ts.active_renderer.set_interactive(my_custom_renderer)

# Optional: enable automatic array visualization
pz.ts.active_autovisualizer.set_interactive(pz.ts.ArrayAutovisualizer())

You could alternatively set my_custom_renderer to a custom instance of penzai.treescope.renderer.TreescopeRenderer if you want to fully control its configuration.

from penzai.

danieldjohnson avatar danieldjohnson commented on June 12, 2024

Ah, that's an interesting interaction. Right now Treescope assumes things that define _repr_html_ are "figure-like", so render_to_html computes and adds the rich visualization as an annotation. This is useful for stuff like showing lists of plotly figures. I didn't anticipate that someone would call render_to_html inside _repr_html_.

This behavior is enabled by the repr_html_postprocessor.append_repr_html_when_present wrapper hook (here). You can disable this behavior by removing that wrapper hook from the wrapper_hooks list.

In this case, you might also want to avoid using pz.ts.render_to_html directly, because that uses the default global renderer which you might not always have control over. Instead, you could construct a new renderer specific to your types, and call renderer.to_html instead. That would let you fully control the set of handlers that are enabled for the renderer you're using inside _repr_html_.

(For context, are you doing this so that you can render your objects with treescope even if treescope isn't enabled by default? If treescope is enabled, you shouldn't have to define _repr_html_ at all.)

from penzai.

femtomc avatar femtomc commented on June 12, 2024

@danieldjohnson Got it -- I think I'll just install treescope by default in my notebooks, and not futz around with _repr_html_ at all.

In that case, is there a pithy few line incantation for me to install treescope and push my own custom handler onto the default?

from penzai.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.