# Create new notebook from extension

**URL:** <https://discourse.jupyter.org/t/create-new-notebook-from-extension/16888>\
**Category:** JupyterLab\
**Tags:** how-to, help-wanted\
**Created:** [November 20, 2022, 1:52pm UTC](https://discourse.jupyter.org/t/create-new-notebook-from-extension/16888 "2022-11-20T13:52:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rakesh\_K](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/rakesh_k/32/7108_2.png) [@Rakesh\_K](https://discourse.jupyter.org/u/Rakesh_K)\
**Post date:** [November 20, 2022, 1:52pm UTC](https://discourse.jupyter.org/t/create-new-notebook-from-extension/16888/1 "2022-11-20T13:52:31Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![krassowski](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/krassowski/32/1685_2.png) [@krassowski](https://discourse.jupyter.org/u/krassowski)\
**Post date:** [November 20, 2022, 4:09pm UTC](https://discourse.jupyter.org/t/create-new-notebook-from-extension/16888/2 "2022-11-20T16:09:48Z")

</div>

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:

```typescript
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](https://github.com/jupyterlab/jupyterlab-plugin-playground) on Binder.

---

<div class="post-metadata">

**Author:** ![Rakesh\_K](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/rakesh_k/32/7108_2.png) [@Rakesh\_K](https://discourse.jupyter.org/u/Rakesh_K)\
**Post date:** [November 21, 2022, 6:04am UTC](https://discourse.jupyter.org/t/create-new-notebook-from-extension/16888/3 "2022-11-21T06:04:15Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![krassowski](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/krassowski/32/1685_2.png) [@krassowski](https://discourse.jupyter.org/u/krassowski)\
**Post date:** [November 21, 2022, 9:11am UTC](https://discourse.jupyter.org/t/create-new-notebook-from-extension/16888/4 "2022-11-21T09:11:34Z")

</div>

> [@Rakesh\_K](#):
>
> Is this something that I should be concerned with?

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](https://github.com/jupyterlab/jupyterlab/pull/13308)), 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.

---

<div class="post-metadata">

**Author:** ![Rakesh\_K](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/rakesh_k/32/7108_2.png) [@Rakesh\_K](https://discourse.jupyter.org/u/Rakesh_K)\
**Post date:** [November 21, 2022, 11:54am UTC](https://discourse.jupyter.org/t/create-new-notebook-from-extension/16888/5 "2022-11-21T11:54:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bollwyvl](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/bollwyvl/32/904_2.png) [@bollwyvl](https://discourse.jupyter.org/u/bollwyvl)\
**Post date:** [November 21, 2022, 2:11pm UTC](https://discourse.jupyter.org/t/create-new-notebook-from-extension/16888/6 "2022-11-21T14:11:14Z")

</div>

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.
