extension-examples icon indicating copy to clipboard operation
extension-examples copied to clipboard

How do fetch notebook or current file path for a jupyterlab extension the way the `Rename` widget does?

Open luis-chaves-visa opened this issue 2 years ago • 1 comments

The Rename action that appears when a user right-clicks a notebook name on the tab section

image

displays the path of the current file:

image

I was wondering how it does that, I'd like to replicate that functionality for my extension, I've browsed through jupyterlab's source code but wasn't able to find an answer.

luis-chaves-visa avatar Aug 10 '22 15:08 luis-chaves-visa

@luis-chaves-visa usually an user interaction is linked to triggering a command (like when a user click a menu entry, press shortcut keys, click on simple toolbar buttons,...). That said it may not be easy to find which one.

A good place to look for is the latest version of the documentation that list all available commands: https://jupyterlab.readthedocs.io/en/latest/user/commands.html

In the case of the rename action, the command is docmanager:rename. And its implementation is at https://github.com/jupyterlab/jupyterlab/blob/7b57518f284fea3ceeb0479352897c720779500d/packages/docmanager-extension/src/index.tsx#L918

The code logic is:

  • Get the context of the widget that is linked to the context menu
  • From the context you can get the path, see for example the code of the rename dialog: https://github.com/jupyterlab/jupyterlab/blob/912aceb5a0a08fda578375567b6b2fee936d86c4/packages/docmanager/src/dialogs.ts#L40

fcollonval avatar Aug 10 '22 16:08 fcollonval