JL 4 extension conversion: Notebook to JSON

I am working on converting an extension we had for pre-JL 4 that serialized a notebook to JSON and then sent the JSON to a service.

In the old method we could get a hold of the notebook and serialize by executing:
Jupyter.notebook.toJSON();

I am struggling to figure out where to find the documentation that describes how to get a hold of the notebook for this purpose.

I have a plugin registered correctly and a custom command ready to go:

const plugin: JupyterFrontEndPlugin<void> = {
  id: '@jupyterlab-examples/toolbar-button:plugin',
  description:
    'A JupyterLab extension adding a button to the Notebook toolbar.',
  autoStart: true,
  activate: (app: JupyterFrontEnd, doc: DocumentRegistry) => {
    // Nothing is needed
    const { commands } = app;

    const command = 'jlab-examples:command';

    // Add a command
    commands.addCommand(command, {
      label: 'Submit',
      caption: 'Send your notebook to be graded',
      execute: (args: any) => {
        console.log(`jlab-examples:command has been called.`);
        **// how to get notebook model for serialization?**
      }
    });
  }
};

The object model is difficult to parse through. I am assuming I need to get a hold of the notebook widget and from there extract the model in order to then serialize. But I could wrong here as well.

doc.getWidgetFactory("Notebook") ?

Thanks for the help.

The key search term for other discussions around this breaking change is sharedModel.

The relevant API docs:

Thank you. This helps explain where I was partially missing the boat.

I am still confused about how to navigate the object model. How do I get the notebook model from the DocumentRegistry or somewhere else?

doc.getWidgetFactory or doc.getModelFactory have generic properties but I don’t see how to get the sharedModel from here.

Thanks again. Any other links explaining how to navigate the object model would be great.

I appreciate your help,

Sean

See one of the links under sharedModel:

Use INotebookTracker.currrentWidget.content

Thanks again just to put it out there. This is how I was able to get a hold of the notebook for serialization:

const plugin: JupyterFrontEndPlugin<void> = {
  id: '@jupyterlab-examples/toolbar-button:plugin',
  description:
    'A JupyterLab extension adding a button to the Notebook toolbar.',
  autoStart: true,
  requires: [INotebookTracker],
  activate: (app: JupyterFrontEnd, notebookTracker: INotebookTracker) => {
    // Nothing is needed
    const { commands } = app;

    const command = 'jlab-examples:command';
    var nbpanel : any;
    notebookTracker.currentChanged.connect((tracker, panel) => {
      nbpanel = panel
    })
    // Add a command
    commands.addCommand(command, {
      label: 'Submit for Grading',
      caption: 'Send your notebook to be graded',
      execute: (args: any) => {
        var nb = nbpanel?.context.model.toJSON();
    });
  }