Extension that is notified on notebook open event

I would like to have an extension listen to all “notebook open events”. So far I have looked at the NotebookTracker class in packages/notebook/src/tracker.ts which seems to be the place to listen. But how would I go about subscribing to this event in an extension?


What kind of extension are you thinking of? See here for background:

And maybe you could rephrase the title? Activating an extension is something else than receiving events in an extension that is already active.

Thanks for the quick reply. I amended the title to reflect what specifically I’m trying to achieve. I have already made the extension and have registered a command that works fine when the user explicitly calls the command. The case here is that I would like the notebook to automatically run this command if certain prerequisites in the notebook are fulfilled. Specifically, if certain text strings appear in the code cells of the notebook, the command should be triggered.
And just to be sure that I clarified your question in the end of your comment: the extension is already activated, and I would like to receive events in the extension.

Thanks again

1 Like

Hi @manniche, you can listen to the widgetAdded signal on the notebook tracker, which is documented here. You can register a callback with that function which calls your command.

Hi Ian,

thanks for the suggestion. I managed to hook up a callback to the widgetAdded signal, and I do get an event when opening the notebook.
However, when I try to retrieve the editor element from the NotebookPanel that gets passed to my callback, there are no lines available in it. My suspicion is that the element has not been rendered when the signal is fired, and I therefore cannot see the code lines from the notebook.

Feels like I’m almost there, just need to get the notebook contents.

Thanks again for taking the time to read and answer

Just an update. I found the solution to my problem. Instead of the widgetAdded, I attached to the INotebookTracker.currentChanged and checked for a currentWidget in the callback. If that is present, it means that the notebook has rendered and I can get to the code lines in the notebook.

Again thanks for the help and answers.

1 Like