0

How do I access:

  • a list of open notebooks;
  • a list of cells in a specific notebook

in JupyterLab 4?

I can generate a reference to the currently active cell in an active notebook:

const plugin: JupyterFrontEndPlugin<void> = {
  id: '@jupyterlab-examples/toolbar-button:plugin',
  description:
    'A JupyterLab extension adding a button to the Notebook toolbar.',
  requires: [INotebookTracker],
  autoStart: true,
  activate: (app: JupyterFrontEnd, notebookTracker: INotebookTracker) => {
    activeCell = notebookTracker.activeCell
    // Perform operations on active cell,
    // eg get/set metadata, set node class attributes
  })
};

export default plugin;

and then use the activeCell reference to get/set cell metadata (e.g. activeCell.model.getMetadata("tags") as string[]) and manipulate cell node class attributes in the rendered UI (e.g. activeCell.node.classList.add('myclass')).

I thought I might be able to capture "on-the-fly" references to notebooks as they are selected using:

notebookTracker.currentChanged.connect((tracker, panel) => {
})

but I can't find lists of cells on tracker or panel?

In pseudo code, what I would like to do is:

  • when a notebook is opened (or rendered, eg if Lab starts with opened notebooks in view):
    • iterate the cells;

Cell references should support getting/setting cell metadata and manipulation of cell node class attributes.

psychemedia
  • 5,690
  • 7
  • 52
  • 84
  • This answer on Juopyter Discourse forum seems to do the trick in terms of accessing notebooks: https://discourse.jupyter.org/t/jupyterlab-4-iterating-over-all-cells-in-a-notebook/20033/5?u=psychemedia – psychemedia Jun 22 '23 at 00:04
  • Blogged comments on a solution: https://blog.ouseful.info/2023/06/22/updating-the-empinken-extension-from-jupyterlab-3-x-to-4-x/ – psychemedia Jun 22 '23 at 13:53

0 Answers0