Refactoring extension from JupyterLab 3.6 to 4.x

I’ve been working on refactoring an extension that was originally written for JupyterLab 3.6, with the goal of making it compatible with JupyterLab 4. However, I’ve run into some issues with certain methods that were previously used in JupyterLab 3.6. Specifically, I’m struggling to find analogous methods for the following operations in JupyterLab 4:

  1. notebookPanel.content.model.initialize();
  2. notebookPanel.content.model.cells.insert(0, cell);
  3. notebookPanel.content.model.cells.removeRange(1, notebookPanel.content.model.cells.length);

I’ve searched through the changelog and the JupyterLab 4 documentation, but I couldn’t find clear replacements or alternatives for these methods.
Has anyone else encountered similar issues while upgrading extensions to JupyterLab 4? If so, could you please share your insights and solutions? Any guidance, code examples, or documentation links would be greatly appreciated.

2 Likes

In place of .model you can use .model.sharedModel (ISharedNotebook). This is because in JupyterLab 4.0 the INotebookModel was adapted to Real Time Collaboration.

For example, notebookPanel.content.model.cells.insert(0, cell); would become:

notebookPanel.content.model.sharedModel.cells.insertCell(0, cell);

Thank you for asking, this was indeed missed from extension migration guide; if you would like to contribute please do open a pull request updating it.

2 Likes

Also, since NotebookPanel has direct access to the model, the above can be simplified to:

notebookPanel.model.sharedModel.cells.insertCell(0, cell);