From 73cba5d8f9aec6ec28306079ec1720ca67a33e00 Mon Sep 17 00:00:00 2001 From: Jeremy Tuloup Date: Mon, 12 Feb 2024 16:36:57 +0000 Subject: [PATCH] Create a new notebook with a specific kernel from the new dropdown --- packages/docmanager-extension/src/index.ts | 8 ++++- packages/tree-extension/src/index.ts | 37 ++++++++++++++++------ 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/packages/docmanager-extension/src/index.ts b/packages/docmanager-extension/src/index.ts index e23a9911e1..d3bfff9a91 100644 --- a/packages/docmanager-extension/src/index.ts +++ b/packages/docmanager-extension/src/index.ts @@ -40,7 +40,10 @@ const opener: JupyterFrontEndPlugin = { const pathOpener = notebookPathOpener ?? defaultNotebookPathOpener; let id = 0; return new (class { - open(widget: IDocumentWidget, options?: DocumentRegistry.IOpenOptions) { + async open( + widget: IDocumentWidget, + options?: DocumentRegistry.IOpenOptions + ) { const widgetName = options?.type ?? ''; const ref = options?.ref; // check if there is an setting override and if it would add the widget in the main area @@ -54,6 +57,9 @@ const opener: JupyterFrontEndPlugin = { (widgetName === 'default' && ext === '.ipynb') || widgetName.includes('Notebook') ) { + // make sure to save the notebook before opening it in a new tab + // so the kernel info is saved + await widget.context.save(); route = 'notebooks'; } // append ?factory only if it's not the default diff --git a/packages/tree-extension/src/index.ts b/packages/tree-extension/src/index.ts index 4d9ba65e7a..4f61cacdfa 100644 --- a/packages/tree-extension/src/index.ts +++ b/packages/tree-extension/src/index.ts @@ -87,7 +87,7 @@ const createNew: JupyterFrontEndPlugin = { translator: ITranslator, toolbarRegistry: IToolbarWidgetRegistry | null ) => { - const { commands } = app; + const { commands, serviceManager } = app; const trans = translator.load('notebook'); const overflowOptions = { @@ -99,18 +99,35 @@ const createNew: JupyterFrontEndPlugin = { newMenu.title.icon = caretDownIcon; menubar.addMenu(newMenu); - const newCommands = [ - 'notebook:create-new', - 'terminal:create-new', - 'console:create', - 'filebrowser:create-new-file', - 'filebrowser:create-new-directory', - ]; + const populateNewMenu = () => { + // create an entry per kernel spec for creating a new notebook + const specs = serviceManager.kernelspecs?.specs?.kernelspecs; + for (const name in specs) { + newMenu.addItem({ + args: { kernelName: name, isLauncher: true }, + command: 'notebook:create-new', + }); + } + + const baseCommands = [ + 'terminal:create-new', + 'console:create', + 'filebrowser:create-new-file', + 'filebrowser:create-new-directory', + ]; - newCommands.forEach((command) => { - newMenu.addItem({ command }); + baseCommands.forEach((command) => { + newMenu.addItem({ command }); + }); + }; + + serviceManager.kernelspecs?.specsChanged.connect(() => { + newMenu.clearItems(); + populateNewMenu(); }); + populateNewMenu(); + if (toolbarRegistry) { toolbarRegistry.addFactory( FILE_BROWSER_FACTORY,