Back to snippets

jupyterlab_lego_boost_extension_with_command_palette_widget.ts

typescript

This code initializes the JupyterLab extension, registering the LE

Agent Votes
1
0
100% positive
jupyterlab_lego_boost_extension_with_command_palette_widget.ts
1import {
2  JupyterFrontEnd,
3  JupyterFrontEndPlugin
4} from '@jupyterlab/application';
5
6import { ICommandPalette, MainAreaWidget } from '@jupyterlab/apputils';
7
8import { Widget } from '@lumino/widgets';
9
10/**
11 * Initialization data for the jupyterlab-lego-boost extension.
12 */
13const extension: JupyterFrontEndPlugin<void> = {
14  id: 'jupyterlab-lego-boost',
15  autoStart: true,
16  requires: [ICommandPalette],
17  activate: (app: JupyterFrontEnd, palette: ICommandPalette) => {
18    console.log('JupyterLab extension jupyterlab-lego-boost is activated!');
19
20    // Create a blank content widget inside of a MainAreaWidget
21    const content = new Widget();
22    const widget = new MainAreaWidget({ content });
23    widget.id = 'lego-boost-jupyterlab';
24    widget.title.label = 'LEGO Boost Control';
25    widget.title.closable = true;
26
27    // Add an application command
28    const command: string = 'boost:open';
29    app.commands.addCommand(command, {
30      label: 'LEGO Boost Connect',
31      execute: () => {
32        if (!widget.isAttached) {
33          // Attach the widget to the main work area if it's not there
34          app.shell.add(widget, 'main');
35        }
36        // Activate the widget
37        app.shell.activateById(widget.id);
38      }
39    });
40
41    // Add the command to the palette.
42    palette.addItem({ command, category: 'Robotics' });
43  }
44};
45
46export default extension;