Skip to content

Add example of toolbar button in text editor (+for particular file extension) #188

@ktaletsk

Description

@ktaletsk

Problem

There is an excellent example on how to add a button to the notebook toolbar, which boils down to

app.docRegistry.addWidgetExtension('Notebook', new ButtonExtension());

Now, I would like to do the same with other editors available in JupyterLab, in particular tailored for the specific file extension (think .py or .md or .tex). I played around with the example and reached the desired result. I thought the answer might be useful for someone else searching for the same functionality.

The first part (adding button to all text editors) is straightforward: change the name of the widget factory widgetName in the addWidgetExtension call from 'Notebook' to 'Editor'. The second part I achieved by checking the path ending:

// Limit the toolbar button for .tex files only
if (context.path.endsWith('.tex')) {
  panel.toolbar.insertItem(10, 'clearOutputs', button);
}

I am not sure this is the best (or even valid) way to do this, but it gave the desired result. The full example is available in my fork: https://github.com/ktaletsk/extension-examples/blob/toolbar-button-editor/toolbar-button/src/index.ts

To summarize, I would like to:

  1. Ask community if the proposed solution is the way to go for buttons in specific file extension editors
  2. Add the accepted solution (either mine or better one from community) as part of toolbar-button example or posible a different example in this repo. I would be happy to create PR.

Proposed Solution

Add example of the toolbar button enabled only for specific file extension

Additional context

Proposed example in action:
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions