|
| 1 | +# Toolbar Item |
| 2 | + |
| 3 | +This example shows how to add a button to the notebook toolbar. |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +In this particular example, the button will clear all cell outputs |
| 8 | + |
| 9 | +To use it first you need to import the following packages: |
| 10 | + |
| 11 | +```ts |
| 12 | +// src/index.ts#L1-L16 |
| 13 | + |
| 14 | +import { IDisposable, DisposableDelegate } from '@lumino/disposable'; |
| 15 | + |
| 16 | +import { |
| 17 | + JupyterFrontEnd, |
| 18 | + JupyterFrontEndPlugin |
| 19 | +} from '@jupyterlab/application'; |
| 20 | + |
| 21 | +import { ToolbarButton } from '@jupyterlab/apputils'; |
| 22 | + |
| 23 | +import { DocumentRegistry } from '@jupyterlab/docregistry'; |
| 24 | + |
| 25 | +import { |
| 26 | + NotebookActions, |
| 27 | + NotebookPanel, |
| 28 | + INotebookModel |
| 29 | +} from '@jupyterlab/notebook'; |
| 30 | +``` |
| 31 | + |
| 32 | +Firstly you have to register the plugin information. For that you have to pass a activate **function** and the plugin **id**. |
| 33 | + |
| 34 | +```ts |
| 35 | +// src/index.ts#L21-L25 |
| 36 | + |
| 37 | +const plugin: JupyterFrontEndPlugin<void> = { |
| 38 | + activate, |
| 39 | + id: 'toolbar-button', |
| 40 | + autoStart: true |
| 41 | +}; |
| 42 | +``` |
| 43 | + |
| 44 | +New widgets can be added to a document widget by implementing the interface [DocumentRegistry.IWidgetExtension](https://jupyterlab.readthedocs.io/en/latest/api/interfaces/docregistry.documentregistry.iwidgetextension.html). In particular, you need to add your widget in the `createNew` method that is called when creating a new |
| 45 | +document widget; in this case a notebook panel. |
| 46 | + |
| 47 | +```ts |
| 48 | +// src/index.ts#L30-L54 |
| 49 | + |
| 50 | +export class ButtonExtension |
| 51 | + implements DocumentRegistry.IWidgetExtension<NotebookPanel, INotebookModel> { |
| 52 | + /** |
| 53 | + * Create a new extension for the notebook panel widget. |
| 54 | + */ |
| 55 | + createNew( |
| 56 | + panel: NotebookPanel, |
| 57 | + context: DocumentRegistry.IContext<INotebookModel> |
| 58 | + ): IDisposable { |
| 59 | + const clearOutput = () => { |
| 60 | + NotebookActions.clearAllOutputs(panel.content); |
| 61 | + }; |
| 62 | + const button = new ToolbarButton({ |
| 63 | + className: 'clear-output-button', |
| 64 | + label: 'Clear All Outputs', |
| 65 | + onClick: clearOutput, |
| 66 | + tooltip: 'Clear All Outputs' |
| 67 | + }); |
| 68 | + |
| 69 | + panel.toolbar.insertItem(10, 'clearOutputs', button); |
| 70 | + return new DisposableDelegate(() => { |
| 71 | + button.dispose(); |
| 72 | + }); |
| 73 | + } |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +Finally you need to tell the document registry about your widget extension: |
| 78 | + |
| 79 | +```ts |
| 80 | +// src/index.ts#L59-L61 |
| 81 | + |
| 82 | +function activate(app: JupyterFrontEnd) { |
| 83 | + app.docRegistry.addWidgetExtension('Notebook', new ButtonExtension()); |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +## Where to Go Next |
| 88 | + |
| 89 | +This example uses a command to display the widget. Have a look a the |
| 90 | +[commands example](../commands/README.md) for more information about it. |
0 commit comments