import { JupyterFrontEnd, JupyterFrontEndPlugin } from '@jupyterlab/application'; import { ICommandPalette, IFrame, Dialog, showDialog } from '@jupyterlab/apputils'; import { PageConfig } from '@jupyterlab/coreutils'; import { ILauncher } from '@jupyterlab/launcher'; import { LabIcon } from '@jupyterlab/ui-components'; import React from 'react'; const nniIconSvg = ` `; const nniIcon = new LabIcon({ name: 'nni', svgstr: nniIconSvg }); const NNI_URL = PageConfig.getBaseUrl() + 'nni/index'; class NniWidget extends IFrame { constructor(url) { super({ sandbox: [ 'allow-same-origin', 'allow-scripts', ] }); this.url = url; this.id = 'nni'; this.title.label = 'NNI'; this.title.icon = nniIcon; 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', icon: (args) => (args.isPalette ? null : nniIcon), execute: () => { 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'); }); } }); palette.addItem({ command, category }); if (launcher) { launcher.add({ command, category }); } } const extension: JupyterFrontEndPlugin = { id: 'nni', autoStart: true, optional: [ILauncher], requires: [ICommandPalette], activate, }; export default extension;