How to load sample file into JupyterLab

Hello

I am trying to develop an extension that on command will launch a jupyter notebook file with sample code.

I have the notebook contents in a json file contained in the extension. My problem is that I’m not sure how to import/load this file to the jupyterlab file browser to then be launched.

Any help would be appreciated.

One approach would be to first make an untitled notebook, then update the model:

import { NotebookPanel } from '@jupyterlab/notebook';

async function newCustomFromString(nbName: string, cwd?: string, kernelName?: string): NotebookPanel {
  const nbText = await import("!!raw-loader!./custom-notebook.ipynb");
  const nbPanel: NotebookPanel = await app.commands.execute(
    'notebook:create-new', 
    {cwd, kernelName}
  );
  await nbPanel.context.ready;
  await nbPanel.context.rename(nbName);
  nbPanel.context.model.fromString(nbText);
  return nbPanel;
}

3 Likes

Looks like that does the trick. Thank you!

Just noticed that when I use a similar method to this everything works except the kernel does not update to the new name of the notebook.

So it stays as the default ‘Untitled.ipynb’ for the kernel instead of the notebook name. Is there a way to refresh or change the name of the kernel after renaming?

How to import webpack raw-loader in a jupyterlab extension? I didn’t find webpack.config.js in my directory.

@zymmatt Not sure about raw loader, but typescript will allow importing of JSON files directly. So you can just import the notebook in JSON format and use that.