Create new notebook from extension

I would like to create a new notebook upon some user action in my extension. I am using this command to create a new notebook:

const result = await this._commandCenter.executeCommand('docmanager:new-untitled', {path: cwd, type: 'notebook',})
    
const widget = await this._commandCenter.executeCommand('docmanager:open', {path: result.path, factory: 'Notebook'});

But this brings up a dialog box to select the kernel. Is there anyway to set a default kernel while creating the notebook using the above command and prevent showing the kernel selection dialog?

Thanks,
Rakesh.

Yes, you need to pass third argument to docmanager:open: kernel: { name: kernelName }, where kernelName corresponds to one of the identifiers of kernels form jupyter kernelspec list. You can get this programatically from app.serviceManager.kernelspecs.specs?.kernelspecs. A full example:

import { JupyterFrontEnd, JupyterFrontEndPlugin } from '@jupyterlab/application';


const plugin: JupyterFrontEndPlugin<void> = {
  id: 'notebook-opener:plugin',
  autoStart: true,
  activate: async (app: JupyterFrontEnd) => {
    const services = app.serviceManager
    await services.ready;
    const kernelspecs = services.kernelspecs;
    // Note: add null handling - should not use ! in production
    const kernels = Object.keys(kernelspecs.specs!.kernelspecs!);
    const result = await app.commands.execute(
      'docmanager:new-untitled',
      {path: '.', type: 'notebook'}
    );
    await app.commands.execute(
      'docmanager:open',
      {
        path: result.path,
        factory: 'Notebook',
        kernel: {
          name: kernels[0]
        }
      }
    );
  },
};

export default plugin;

You can try the above example in jupyterlab-plugin-playground on Binder.

Hi @krassowski Thank you for the update. I was able to create a new notebook as per your suggestion. However, I see an error in my console log occassionally:

react_devtools_backend.js:4026 TypeError: Cannot set properties of undefined (setting 'text')
    at f._setValue (jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2:234326)
    at set source [as source] (jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2:231978)
    at s._update (jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2:860737)
    at s.onActiveCellChanged (jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2:860615)
    at s.processMessage (jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2:857076)
    at b (jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2:1546954)
    at Object.t [as sendMessage] (jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2:1545525)
    at jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2:855959
    at c (jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2:1344531)
    at L._onActiveCellChanged (jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2:855918)
overrideMethod @ react_devtools_backend.js:4026
m @ jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2
b @ jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2
t @ jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2
(anonymous) @ jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2
c @ jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2
_onActiveCellChanged @ jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2
m @ jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2
l @ jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2
e.emit @ jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2
onCurrentChanged @ jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2
(anonymous) @ jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2
m @ jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2
l @ jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2
e.emit @ jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2
set current @ jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2
add @ jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2
await in add (async)
add @ jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2
(anonymous) @ jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2
m @ jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2
l @ jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2
e.emit @ jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2
createNew @ jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2
createWidget @ jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2
_createOrOpenDocument @ jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2
open @ jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2
openOrReveal @ jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2
(anonymous) @ jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2
Promise.then (async)
execute @ jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2
e.execute @ jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2
executeCommand @ jlcommandcenter.js:44

Is this something that I should be concerned with? The new notebook works just fine though.

Thanks,
Rakesh.

This is already fixed on 3.6 branch (if I remember correctly by Updates JSONEditor's source only when there is an active cell or an active notebook panel by hbcarlos · Pull Request #13308 · jupyterlab/jupyterlab · GitHub), so you should not need to worry. If this is causing any nuisance we can backport the fix. The original plan was to release 3.6 soon after 3.5 - I think this is the only reason this was not already backported in a patch release.

Thank you @krassowski it’s not causing any issues so far. I am on version 3.5, I think it’d go away once I update to 3.6.

3.6 contains a number of RTC-related changes which should be considered breaking for some use cases. Until we have an idea of the broader impact of these changes, getting low-hanging fruit bugfixes into 3.5.x would be useful.

1 Like