Requesting help with how to remove menu items from the Jupyterlab Context menu

Need to remove a couple of menu items from the JuypterLab context menu – the context menu that displays when right mouse clicking on a Jupyter Notebook in the left document tree. I have searched the documentation and I am not able to figure that out.

It is easy to add menu items. It is much harder to take them away (in general, extensions shouldn’t be able to modify other extensions by taking away their features).

That said, it may make sense to have an application-wide control for a context menu. The context menu is an attribute of the application object, so you could define your own application object with its own context menu instance, which could do whatever filtering it wanted. If you want to explore this more, please open an issue in JupyterLab.

I STRICTLY DISSAPROVE THAT BUT
You can try to remove commands. For example:

import { CommandRegistry } from '@lumino/commands';

interface IMenuCommands {
    _commands: { [index: string]: unknown }
}

const extension: JupyterFrontEndPlugin<void> = {
    id: 'test:plugin',
    autoStart: true,
    activate: (
        app: JupyterFrontEnd,
    ): void => {
        app.restored.then(() => {
            const itemsToRemove = [ 'filebrowser:cut', 'filebrowser:paste', 'filebrowser:copy' ]
            const commands = <IMenuCommands><CommandRegistry | IMenuCommands>app.commands;
            app.commands;
            itemsToRemove.forEach(item => {
                delete commands._commands[item]
            })

        })
    }
}
export default extension;

1

I’ll also add that this approach accesses private implementation variables, so is risky and fragile and may break suddenly or have other unforeseen consequences.

1 Like