How to retrieve the notebook directory in developing jupyterlab extension?

I am now working on a JupyterLab extension and struggling to get the notebook directory from the app front end.

For example, the user runs the jupyter lab with option --notebook-dir, like

jupyter lab --notebook-dir dir1

In the activate input of a JupyterFrontEndPlugin (in index.ts), i.e.

const extension: JupyterFrontEndPlugin<void> = {
  id: PLUGIN_ID,
  autoStart: true,
  requires: [IFileBrowserFactory],
  activate: activate
};

I tried to locate a property in JupyterFrontEnd app object for the notebook directory

function activate(
  app: JupyterFrontEnd,
  ...
) {
  ...
}

but no luck.

If I look into the app object in runtime, it seems the property app.paths.directories.serverRoot is only found in the runtime.

Is there any other static property I can rely on?

Have you found a solution for this? Wondering the same.

Unfortunately, I had to resort to access the property dangerously.

The solution you use may not work in certain contexts (potentially JupyterLite/jupyverse). Did you try:

import { PageConfig } from '@jupyterlab/coreutils';
PageConfig.getOption('serverRoot');

In future, searching JupyterLab codebase might give you a quick answer :slight_smile:

1 Like