outputs icon indicating copy to clipboard operation
outputs copied to clipboard

Saving widget state in ipynb

Open miduncan opened this issue 4 years ago • 4 comments

Jupyterlab and Jupyter-classic both support a way to save the notebook such that it persists the widget data in the ipynb (see here). This can be used so that exporting a notebooks will show the widgets with the correct value. It is done by writing to the metadata portion of the ipynb, not by changing the output value saved in the ipynb. My question is

  • Are we open to supporting this in nteract?
  • If yes, how would we want to do it? Since widgets are written as an output, writing to metadata from within an output might be considered a bit of an anti-pattern

miduncan avatar Apr 07 '21 20:04 miduncan

@miduncan What's the payload that is written to the notebook when using the "Save Notebook Widget State" item?

captainsafia avatar Apr 08 '21 22:04 captainsafia

@captainsafia It looks something like this (irrelevant fields omitted)

{
 "cells": [
  {
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e628e4cd267741c2a45818fc11314d4b",
       "version_major": 2,
       "version_minor": 0
      },
     },
    }
   ],
  }
 ],
 "metadata": {
    ...
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {
     "e628e4cd267741c2a45818fc11314d4b": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "IntSliderModel",
      "state": {
       "layout": "IPY_MODEL_4e99f641bc7146acb26556cfe7e69521",
       "style": "IPY_MODEL_ea421168b7084ac397e9829f9dc7a77b",
       "value": 50
      }
     },
    },
    "version_major": 2,
    "version_minor": 0
   }
  }
 }
}


miduncan avatar Apr 09 '21 23:04 miduncan

As far as not having outputs writing that in a notebook, we could possible store a reference to the Widget Manager somewhere in a redux store. Then we could make the get_state call (seen here) from wherever we want and add that to the notebook. Thoughts?

miduncan avatar Apr 09 '21 23:04 miduncan

Yeah, I'm not opposed to the idea on principle but I think the design needs to be fleshed out a little bit more especially because it does require some bidrectional interactions between the parent app and an individual output.

we could possible store a reference to the Widget Manager somewhere in a redux store.

Yeah, maybe we can store a reference to the Widget manager in the transient state of the notebook. It looks like get state will serialize all the widgets in the notebook? How do we make sure that we write the correct serialized version to the output on disk?

captainsafia avatar Apr 14 '21 21:04 captainsafia