google-deepmind / treescope

An interactive HTML pretty-printer for machine learning research in IPython notebooks.
https://treescope.readthedocs.io/
Apache License 2.0
331 stars 17 forks source link

IPython cell magic for enabling Treescope in a single cell #5

Open JEM-Mosig opened 3 months ago

JEM-Mosig commented 3 months ago

I'd like to get the treescope visualizations in notebooks only if I decorate a cell with %%autovisualize. However, %%autovisualize doesn't seem to have any effect. I can only use treescope everywhere (with basic_interactive_setup()) or nowhere.

image

I am aware of treescope.show, but I'd still like to use %%autovisualize because it is nicely separated from the code content of the cell.

Thank you for sharing this great library!

danieldjohnson commented 3 months ago

Ah, this isn't actually what %%autovisualize is intended to be used for!

The %%autovisualize magic turns on automatic array visualization for the active Treescope renderer, but doesn't enable the renderer itself. This means that calls to treescope.show that happen inside an %%autovisualize cell will show visual summaries of array data, even if array visualization was turned off during initial setup. Conversely, %%autovisualize None will omit array visualizations and instead show the numerical values in text form.

It sounds like you'd like to have a cell magic that displays the output with Treescope instead of displaying it with the ordinary IPython pretty-printer?

I'm not sure how doable this would be in IPython. I suppose it might be possible to make a cell magic that wraps the final cell output with treescope.show, but this would also prevent accessing the cell output with the outputs dictionary Out[...], so I don't think it should be the default behavior of %%autovisualize.

I think for now it would be easiest to just use treescope.show directly when you want to visualize the final cell output.

JEM-Mosig commented 3 months ago

Thanks for the quick feedback!

It sounds like you'd like to have a cell magic that displays the output with Treescope instead of displaying it with the ordinary IPython pretty-printer?

Yes, this is what I mean. Sorry, I misunderstood the meaning of %%autovisualize.

I'm not sure how doable this would be in IPython

I am not familiar with how IPython works under the hood, but could the treescope visualizer check if the %%autovisualize is present, and if yes, just not do anything / act as identity function / print like it was the default IPython printer?

danieldjohnson commented 3 months ago

I'm not sure, it might be possible but I don't know exactly how that would work.

I think if this does get added, it will probably be a different cell magic, not %%autovisualize (since %%autovisualize already has a separate meaning).