Accessing widget state

Hi Folks,

I’m looking at rendering jupyter-widgets within a web context and although I think I can successfully render the view of a widget, I am having trouble getting hold of or figuring out how to access the widget state information.

Specifically I am looking for widget state that i can drop into a <script type="application/vnd.jupyter.widget-state+json"></script> tag and then have this available on page for the widget view to render.

I am looking for how to retrieve that programatically but not had any luck so far, even though this appears to be connected to the “Save Widget State” setting in lab and the “Embed Widgets” menu item in the classic notebook.

If anyone has any pointers of where to look to better understand where widget state is stored and how to retrieve it, or if i’m just on the wrong track! I’d appreciate the info.

e.g.

  • can this be accessed via the jupyterlab-manager? toJSON?
  • is there somewhere on the global jupyterlab object that exposes this manager? or other appropriate object/calls?

Thanks!
Steve

1 Like

I think that any the python widget object will expose it’s state via the get_state method as defined here: https://github.com/jupyter-widgets/ipywidgets/blob/51322341d4f6d88449f9dbf6d538c60de6d23543/ipywidgets/widgets/widget.py#L463

Hi Ian, thanks.
I’m actually trying to work from the javascript side, where my goal is to be able to read widget state from the widget manager (or somewhere) so I can render it in a separate context.

To take advantage of the python calls you are pointing at, I would need to be calling those from within a juptyterlab extension?

I have found since posting my question that jupyterlab will continually append widget state to the notebook metadata, so it is possible to pickup the entire model from there as long as the “Automatically Save Widget State” option is selected by the user.

I was looking for a more targeted method to retrieve that info, for example from the model of a particular widget in memory.

Do you have access to the widget object on the javascript side? There’s an equivalent get_state method there: https://github.com/jupyter-widgets/ipywidgets/blob/51322341d4f6d88449f9dbf6d538c60de6d23543/packages/base/src/widget.ts#L273

So in the classic notebook, it is available on the global object:

Jupyter.WidgetManager._managers[0].get_state()
where the first manager in the array is jupyter.widgets

But I have yet to find an equivalent in lab as a direct call.

From the Javascript side, you can get this information from the widget model, which can in turn be retrieved from the view. For example:

const el = document.querySelector('<SELECT THE WIDGET'S DOM ELEMENT>');
const view = el.widget; // Get a reference to the widget's view
const model = view.model; // Get the model from the view
model.get('<KEY>'); // Retrieve the state based on the traitlet name

You might be able to get all the state at once from the model object, but you’d have to play around with it. I haven’t looked for a specific call for that before.

1 Like