Skip to content

Commit

Permalink
Create a new notebook with a specific kernel from the new dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
jtpio committed Feb 12, 2024
1 parent e49f77e commit 73cba5d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
8 changes: 7 additions & 1 deletion packages/docmanager-extension/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ const opener: JupyterFrontEndPlugin<IDocumentWidgetOpener> = {
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
Expand All @@ -54,6 +57,9 @@ const opener: JupyterFrontEndPlugin<IDocumentWidgetOpener> = {
(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
Expand Down
37 changes: 27 additions & 10 deletions packages/tree-extension/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const createNew: JupyterFrontEndPlugin<void> = {
translator: ITranslator,
toolbarRegistry: IToolbarWidgetRegistry | null
) => {
const { commands } = app;
const { commands, serviceManager } = app;
const trans = translator.load('notebook');

const overflowOptions = {
Expand All @@ -99,18 +99,35 @@ const createNew: JupyterFrontEndPlugin<void> = {
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,
Expand Down

0 comments on commit 73cba5d

Please sign in to comment.