Unable to access cell metadata

I am trying to build an extension for Jupyter Lab 3.x. I am trying to pick cells based on the value of the activelearning key (which is inputted while creating the notebook). Here is my code

const notebook = app.shell.currentWidget as NotebookPanel;
const cellList = notebookPanel.content.model?.cells;
for (let i = 0; i < cellList.length; i++) {
	let cell = cellList?.get(i);
	let metadata = cell.metadata;
	let activelearning = metadata?.activelearning;
	console.log(`cell ${i}: ${activelearning}`);

However activelearning is undefined even though, it is visible in the _map attribute of metadata.

I have already tried

  1. metadata.toJSON() but I got the following error:
No constituent of type 'PartialJSONValue' is callable.
  1. let activelearning = cell.getMetadata('activelearning');
    And I got a similar error.

Any insights would be really helpful!

cell.metadata.get("activelearning") works for me on 3.x:

import { JupyterFrontEnd, JupyterFrontEndPlugin } from '@jupyterlab/application';
import { INotebookTracker } from '@jupyterlab/notebook';

const plugin: JupyterFrontEndPlugin<void> = {
  id: 'metadata:plugin',
  autoStart: true,
  requires: [INotebookTracker],
  activate: (app: JupyterFrontEnd, tracker: INotebookTracker) => {
    app.shell.currentChanged.connect((_emitter, change) => {
      const notebook = tracker.currentWidget;
      if (notebook !== change.newValue) {
        return;
      }
      const cellList = notebook.content.model?.cells;
      const firstCell = cellList?.get(0);
      console.log(
        'Value:',
        firstCell.metadata.get("activelearning")
      );
    });

  },
};

export default plugin;

Tested with GitHub - jupyterlab/jupyterlab-plugin-playground: A dynamic extension loader for JupyterLab

Thank you for your reply, however, I am still encountering an error.

const plugin: JupyterFrontEndPlugin<void> = {
	id: 'activelearning:plugin',
	autoStart: true,
	requires: [INotebookTracker],
	activate: async (
		app: JupyterFrontEnd,
		notebooktracker: INotebookTracker
	) => {
		notebooktracker.widgetAdded.connect(async (_, notebookPanel: NotebookPanel) => {
			await notebookPanel.revealed;
			const notebook = app.shell.currentWidget as NotebookPanel;
			const cellList = notebookPanel.content.model?.cells;
			if (cellList !== undefined) {
				for (let i = 0; i < cellList.length; i++) {
					let cell = cellList?.get(i);
					let activelearning = cell.metadata.get('activelearning');
					console.log(`cell ${i}: ${activelearning}`);
				}
			};
		}
		)
	}
};

export default plugin;

I am getting the error:

"This expression is not callable.
No constituent of type 'PartialJSONValue' is callable.",

Any idea as to why I am encountering this error? Does it have anything to do with my node modules or config files?

What exact version of JupyterLab are you testing against?

Also, is this a runtime error, or an error in your editor?

cell.metadata.get("activelearning") also works for me on 3.x. I think the JupyterLab version installed might not be correct, or maybe the npm package version and the Python package version are not consistent.

1 Like