ipywidgets
ipywidgets copied to clipboard
We should save the (minimal) widget state in the notebook file by default
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
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..
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 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 indeed, see https://github.com/jupyter-widgets/ipywidgets/pull/3114#issuecomment-800131821
@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.
This PR does pick up jslink, btw.
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?