Lab Extension: Open Notebook in New Tab, from MainAreaWidget

I have an extension which contains a MainAreaWidget with HTML+JavaScript. Is it possible to have a clickable button or link that, when clicked, causes a Notebook to be opened in a new tab?

It looks like you can create a NotebookPanel, and then add it to the current “view” using IShell which is part of JupyterFrontEnd . Interested in whether this is the recommended way to do it?

@ew23 there is also the docmanager:open command that can be used to open notebook files in the main area:

commands.execute('docmanager:open', {
  path: 'example.ipynb',
  options: {
	mode: 'split-right'
  }
});

That ended up being useful to me @jtp. Is there a place where I can view the args and their values that can be passed to docmanager:open as part of commands.execute? I haven’t been ale to locate a reference.
Thanks!

A first step would be to have a look at where the docmanager:open command is defined in the JupyterLab source code:

As an aside, we need to make sure that the application is ready in order for docmanager:open to work.

Hi @adpatter I am new to the extension development, could you provide some hint on how to make sure the application is ready, right now I am using a timer to delay the docmanager.open operation but it’s not reliable.

1 Like

Does the code below make sense? I’ve found that if I await these two promises, things seem to generally be in order. However, I am in no way authoritative regarding this subject - just trial and error.

notebookTracker.widgetAdded.connect(async (sender: INotebookTracker, notebookPanel: NotebookPanel) => {

        await notebookPanel.revealed;
        await notebookPanel.sessionContext.ready;
})

Please let me know if I can provide further assistance.

This is an example of the pattern that I use in context: etc_jupyterlab_cell_properties/index.ts at 318ba822c8878186ef2ab888ce44ca0a73b0929a · educational-technology-collective/etc_jupyterlab_cell_properties · GitHub

That code is just for explanatory purposes - it’s not supposed to actually do anything practical.

Hi @adpatter Thanks for your quick response, however, I guess this assumes I will open a notebook, right? Is there a way to determine whether the jupyterlab app is ready (before I load any notebook)? Not sure if there is a promise or event for making sure the doc manager is ready to run docmanager.open.

Search this page for promise: https://jupyterlab.github.io/jupyterlab/classes/_application_src_index_.jupyterfrontend.html

I’ll bet that “restored” or one of the other promises might be the one you’re are looking for.

Please let me know if I can assist further.

For clarification, that class is referring to this thing: etc_jupyterlab_cell_properties/index.ts at 318ba822c8878186ef2ab888ce44ca0a73b0929a · educational-technology-collective/etc_jupyterlab_cell_properties · GitHub

Great! It seems to be working now! Would it make more sense to use started instead of restored?

I think that is a great question and it would make a nice addition to the docs if it’s not in there already.

The way it generally works at present is that you have to grep the code base for the relevant code or class (or find it some other way) and determine how to proceed. You can look to see what happens after those promises resolve.

I would clone the repo and grep it for that class definition. You may be able to view it using the facilities of your IDE, depending on how your environment is set up.

1 Like