ipywidgets icon indicating copy to clipboard operation
ipywidgets copied to clipboard

We should save the (minimal) widget state in the notebook file by default

Open maartenbreddels opened this issue 3 years ago • 7 comments

Problem

People just want to save a notebook with widgets, and the state should be embedded, they by default should not have to worry about saving the widget state, nor do they want the state of 'stale' (unused) widgets in the notebook output.

Proposed Solution

Similar to what we do on the Python side in https://github.com/jupyter-widgets/ipywidgets/blob/671fd0f4ac5887e6a8ef0f225e5f627bd6a637b8/ipywidgets/embed.py#L116 we should simply start from all visible widgets, and collect all children, and children of children etc, till we have a consistent (small) state. By default we should store the widget state, unless the user disables it (because saving can be slow).

Maybe there are edge cases where we cannot detect the children, to protect for that we should have a method in WidgetModel to override finding the children, but I think in 99.99% I can find them.

Related #2598

cc @martinRenou

maartenbreddels avatar Feb 09 '21 19:02 maartenbreddels

One corner case at least: If you use traitlets.link, you will only be able to find the dependency between two widgets on the Python side, and not on the JS side. OTOH, this link will only be significant when you have a live kernel, so..

vidartf avatar Feb 10 '21 10:02 vidartf

That is a good point, that means we cannot 'prune' (close) the seemingly stale widgets, since a live kernel can always include hidden connections.

maartenbreddels avatar Feb 10 '21 10:02 maartenbreddels

@maartenbreddels is this related to #3007 where in jupyter lab, if we set the "automatically save state" option, then the notebook size balloons as state always seems to be appended?

stevejpurves avatar Mar 11 '21 16:03 stevejpurves

@stevejpurves indeed, see https://github.com/jupyter-widgets/ipywidgets/pull/3114#issuecomment-800131821

maartenbreddels avatar Mar 16 '21 10:03 maartenbreddels

@vidartf widgets.jslink can still be useful in cases when disconnected from the kernel- I'm wondering if would be possible to structure the implementation to be more of a bidirectional link from widget A to widget B, so it would appear as a dependency of either widget A or widget B.

blois avatar Oct 01 '21 18:10 blois

This PR does pick up jslink, btw.

maartenbreddels avatar Oct 01 '21 19:10 maartenbreddels

This PR helps me understand how to workaround this for my widget library, thanks for pushing this. I can use what you have here to patch my widget library in the meantime. Any chance this will ever be merged?

J-Rojas avatar Jun 23 '22 22:06 J-Rojas