jtpio / ipylab

Control JupyterLab from Python Notebooks with Jupyter Widgets 🧪 ☢️ 🐍
BSD 3-Clause "New" or "Revised" License
199 stars 13 forks source link

Disable "save widget state automatically" via notebook command? #137

Open kolibril13 opened 9 months ago

kolibril13 commented 9 months ago

I'm looking for a snippet similar to

from ipylab import JupyterFrontEnd
app = JupyterFrontEnd()
app.commands.execute('notebook:insert-cell-below')

but instead of insert-cell-below, I would like to disable "save widget state automatically". That way, I could enable "save widget state automatically" by default for all my notebooks, and exclude some notebooks where I don't want to save widget states. Here's a list of notebook commands that I found, but I don't know which one is right for disabling saving the widget state https://github.com/jupyterlab/jupyterlab/blob/49f6ec28b738243a53a947d544bebafafcbd4a73/packages/notebook-extension/src/index.ts#L136-L325

Looking forward to suggestions and ideas!

jtpio commented 9 months ago

Looks like the command id should be: @jupyter-widgets/jupyterlab-manager:saveWidgetState.

It's not part of core JupyterLab, it comes from the ipywidgets extension.

For reference you can use app.commands.list_commands() to get a list of available commands as in this example notebook: https://github.com/jtpio/ipylab/blob/main/examples/commands.ipynb

image

kolibril13 commented 9 months ago

Ohh, that's amazing, thanks for these great snippets! I've tried

from ipylab import JupyterFrontEnd
app = JupyterFrontEnd()
app.commands.execute('@jupyter-widgets/jupyterlab-manager:saveWidgetState')

and indeed, this toggles the saveWidgetState. If it was off before, it's on afterward.

That's great, however this is not very useful in a real live scenario. It would be amazing to explicitly turn saveWidgetState on or respectively off. Would that be possible as well?

jtpio commented 9 months ago

Maybe the best would be to add commands for saving and clearing the state to ipywidgets directly, like it was the case in the classic notebook.

There is an issue tracking this: https://github.com/jupyter-widgets/ipywidgets/issues/3672

To have 3 commands: