index.ts 2.6 KB
Newer Older
liuzhe-lz's avatar
liuzhe-lz committed
1
import { JupyterFrontEnd, JupyterFrontEndPlugin } from '@jupyterlab/application';
2
import { ICommandPalette, IFrame, Dialog, showDialog } from '@jupyterlab/apputils';
liuzhe-lz's avatar
liuzhe-lz committed
3
4
import { PageConfig } from '@jupyterlab/coreutils';
import { ILauncher } from '@jupyterlab/launcher';
liuzhe-lz's avatar
liuzhe-lz committed
5
import { LabIcon } from '@jupyterlab/ui-components';
6
import React from 'react';
liuzhe-lz's avatar
liuzhe-lz committed
7
8
9
10
11
12
13
14
15
16

const nniIconSvg = `
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 162 84">
  <polygon fill="#0b6bac" points="0,84 12,84 34,18 56,84 68,84 87,27 75,27 62,66 40,0 28,0 0,84"/>
  <polygon fill="#0b6bac" points="94,84 106,84 125,27 113,27 100,66 90,36 84,54 94,84"/>
  <polygon fill="#0b6bac" points="122,0 128,18 140,18 134,0 122,0"/>
  <polygon fill="#0b6bac" points="131,27 150,84 162,84 143,27 131,27"/>
</svg>
`;
const nniIcon = new LabIcon({ name: 'nni', svgstr: nniIconSvg });
liuzhe-lz's avatar
liuzhe-lz committed
17

18
const NNI_URL = PageConfig.getBaseUrl() + 'nni/index';
liuzhe-lz's avatar
liuzhe-lz committed
19
class NniWidget extends IFrame {
20
    constructor(url) {
liuzhe-lz's avatar
liuzhe-lz committed
21
22
23
24
25
26
        super({
            sandbox: [
                'allow-same-origin',
                'allow-scripts',
            ]
        });
27
        this.url = url;
liuzhe-lz's avatar
liuzhe-lz committed
28
29
        this.id = 'nni';
        this.title.label = 'NNI';
liuzhe-lz's avatar
liuzhe-lz committed
30
        this.title.icon = nniIcon;
liuzhe-lz's avatar
liuzhe-lz committed
31
32
33
34
35
36
37
38
39
40
41
42
43
        this.title.closable = true;
    }
}

async function activate(app: JupyterFrontEnd, palette: ICommandPalette, launcher: ILauncher | null) {
    console.log('nni extension is activated');
    const { commands, shell } = app;
    const command = 'nni';
    const category = 'Other';

    commands.addCommand(command, {
        label: 'NNI',
        caption: 'NNI',
liuzhe-lz's avatar
liuzhe-lz committed
44
        icon: (args) => (args.isPalette ? null : nniIcon),
liuzhe-lz's avatar
liuzhe-lz committed
45
        execute: () => {
46
47
48
49
50
51
52
53
54
55
56
57
58
            fetch(NNI_URL).then(async (resp) => {
                if (resp.status !== 200) {
                    showDialog({
                        title: 'NNI-HPO Launcher Error',
                        body: React.createElement("div", null,
                            "please run command:",
                            React.createElement("div", { style: { color: 'blue', fontSize: "14px", lineHeight: "28px" } }, "nnictl create --config experiment.yml")),
                        buttons: [Dialog.warnButton({ label: 'OK' })]
                    });
                    return;
                }
                shell.add(new NniWidget(NNI_URL), 'main');
            });
liuzhe-lz's avatar
liuzhe-lz committed
59
60
61
62
63
64
        }
    });

    palette.addItem({ command, category });

    if (launcher) {
liuzhe-lz's avatar
liuzhe-lz committed
65
        launcher.add({ command, category });
liuzhe-lz's avatar
liuzhe-lz committed
66
67
68
69
70
71
72
73
74
75
76
77
    }
}

const extension: JupyterFrontEndPlugin<void> = {
    id: 'nni',
    autoStart: true,
    optional: [ILauncher],
    requires: [ICommandPalette],
    activate,
};

export default extension;