How to subscribe to changes in cells metadata?

Hiya, good day everyone

I’m trying to write an extension that needs to subscribe to changes in cells me
disclaimer, I’m only starting to get a grip with jlab extensions…

So I wrote this

    notebookTracker.widgetAdded.connect((tracker, panel) => {
      const notebookModel = panel.content.model
      if (notebookModel === null) {
        return
      }
      notebookModel.cells.changed.connect((_, change) => {
        if (change.type !== 'add') {
          return
        }
        console.log('we have a new cell')
        const newCellModel = change.newValues[0]
        console.log(newCellModel.constructor.name, newCellModel)
        // newCellModel.metadataChanged.connect((...args: any) => {
        //     console.log('metadata changed', args)
        // })
      })
    })

  }

which seems to be the way to go; however when I uncomment the 3 last lines I’m getting this error

src/index.ts:244:22 - error TS2339: Property 'metadataChanged' does not exist on type 'ICellModel'.

244         newCellModel.metadataChanged.connect((...args: any) => {
                         ~~~~~~~~~~~~~~~

I had been reading in the jupyterlab repo the code for ICellModel in packages/cells/src/model.ts and that’s where I had spotted a metadataChanged signal

So after digging a little more I realized I was looking at jlab’s master branch, but was using 3.6.3; and plain enough, the metadataChanged thing is not in 3.6.3…

So I guess my questions are

  • do I get it right ? if so, in what version will the metadataChanged become available ?

  • there’s a “property inspector” available from the upper right corner of jlab;
    this does detect changes in the cell’s metadata, and it’s been around for a while, so how does it do its job ?
    is it not an extension maybe ?

thanks for any hint !

  • do I get it right ?

yes

if so, in what version will the metadataChanged become available ?

4.0 which is entering release candidate phase this week

  • there’s a “property inspector” available […] so how does it do its job ?

You should find an answer here for 4.0:

and here for 3.6.x branch:

1 Like

Thanks for the input !

I guess I did not make myself clear obviously, so let me try to rephrase

I am trying to write an extension that would

  • assist in managing some usage-specific tags
  • react to changes to these tags - specifically by keeping the cell widget classList in sync with the tags attached to that cell

So first off, when I mentioned a property inspector - I am afraid I have not picked the right name, but I was referring to this

because this area indeed reacts to changes done in the cell’s tags
where can I find the source code for that ? Specifically the places where, I expect, one subscribes to the changes in the tags - through, I assume, the metadata ?


Second, regarding 4.0.0

In https://github.com/jupyterlab/jupyterlab/issues/14438
you were saying that

Regarding t.currentWidget.context.model.metadata.has is not a function error: this is reproducible, and will probably require changes in jupytext to make it compatible with JupyterLab 4. In JupyterLab 4 NotebookModel.metadata is a read-only copy of the metdata implemented as a JSON object. I think that you can use standard in operator to check for key presence.

I’m curious to know how to replace in 4.0 the API that we had in 3.6, namely .get .has and .set

It’s not only jupytext’s code that will need changes in this respect, but my own custom extensions need the same kind of care and I can’t seem to find what to replace that with …

Also it’s puzzling, how do we get to modify the metadata if it’s exposed read-only ?


thanks if you can help me get back my grip :slight_smile:

1 Like

It’s not only jupytext’s code that will need changes in this respect, but my own custom extensions need the same kind of care and I can’t seem to find what to replace that with …

I am surprised this is not in extension migration guide; definitely something that should be in there, feel free to point it out in an issue.

So first off, when I mentioned a property inspector - I am afraid I have not picked the right name, but I was referring to this

Yes, these are mixed up often, I should have picked it up. In that case have a look at:

or

Also it’s puzzling, how do we get to modify the metadata if it’s exposed read-only ?

NotebookModel.setMetadata() on the same page as I linked before.

or maybe the migration guide for 4.0.0 is not yet available ?

https://jupyterlab.readthedocs.io/en/stable/extension/extension_migration.html

It is available here: Extension Migration Guide — JupyterLab 4.0.0b2 documentation you were looking at stable rather than latest version. 4.0 will become stable once released.

ow, right, sorry for not figuring that one out by myself :slight_smile:

https://github.com/jupyterlab/jupyterlab/issues/14445

1 Like

I’d love to ask one more question

when subscribing to changes done on a cellModel’s instance metadata, I’d need to somehow retrieve the widget(s) associated to that cell model - so I can actually change how they look like -
but I’m struggling to find out how to do that, can you please outline the general strategy to achieve that ?

thanks for your patience !

One option would be to match on model IDs, something like:

but I would go for .filter() method rather than find index to get the cell directly.

thank you so much for all the help, I have something working !

1 Like