jtpio / ipylab

Control JupyterLab from Python Notebooks with Jupyter Widgets 🧪 ☢️ 🐍

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Disable "save widget state automatically" via notebook command?

kolibril13 opened this issue · comments

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!

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

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?

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: jupyter-widgets/ipywidgets#3672

To have 3 commands:

  • Save Widget State Automatically (toggle)
  • Save Widget State
  • Clear Widget State