-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathindex.ts
125 lines (106 loc) · 3.17 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import {
JupyterFrontEnd,
JupyterFrontEndPlugin,
} from '@jupyterlab/application';
import { ICommandPalette, InputDialog } from '@jupyterlab/apputils';
import { ILauncher } from '@jupyterlab/launcher';
import { IMainMenu } from '@jupyterlab/mainmenu';
import { ITranslator } from '@jupyterlab/translation';
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
import { Menu } from '@lumino/widgets';
import { ExamplePanel } from './panel';
/**
* The command IDs used by the console plugin.
*/
namespace CommandIDs {
export const create = 'kernel-output:create';
export const execute = 'kernel-output:execute';
}
/**
* Initialization data for the extension.
*/
const extension: JupyterFrontEndPlugin<void> = {
id: 'kernel-output',
autoStart: true,
optional: [ILauncher],
requires: [ICommandPalette, IMainMenu, IRenderMimeRegistry, ITranslator],
activate: activate,
};
/**
* Activate the JupyterLab extension.
*
* @param app Jupyter Front End
* @param palette Jupyter Commands Palette
* @param mainMenu Jupyter Menu
* @param rendermime Jupyter Render Mime Registry
* @param translator Jupyter Translator
* @param launcher [optional] Jupyter Launcher
*/
function activate(
app: JupyterFrontEnd,
palette: ICommandPalette,
mainMenu: IMainMenu,
rendermime: IRenderMimeRegistry,
translator: ITranslator,
launcher: ILauncher | null
): void {
const manager = app.serviceManager;
const { commands, shell } = app;
const category = 'Extension Examples';
const trans = translator.load('jupyterlab');
let panel: ExamplePanel;
/**
* Creates a example panel.
*
* @returns The panel
*/
async function createPanel(): Promise<ExamplePanel> {
panel = new ExamplePanel(manager, rendermime, translator);
shell.add(panel, 'main');
return panel;
}
// add menu tab
const exampleMenu = new Menu({ commands });
exampleMenu.title.label = trans.__('Kernel Output');
mainMenu.addMenu(exampleMenu);
// add commands to registry
commands.addCommand(CommandIDs.create, {
label: trans.__('Open the Kernel Output Panel'),
caption: trans.__('Open the Kernel Output Panel'),
execute: createPanel,
});
commands.addCommand(CommandIDs.execute, {
label: trans.__('Contact Kernel and Execute Code'),
caption: trans.__('Contact Kernel and Execute Code'),
execute: async () => {
// Create the panel if it does not exist
if (!panel) {
await createPanel();
}
// Prompt the user about the statement to be executed
const input = await InputDialog.getText({
title: trans.__('Code to execute'),
okLabel: trans.__('Execute'),
placeholder: trans.__('Statement to execute'),
});
// Execute the statement
if (input.button.accept) {
const code = input.value;
panel.execute(code);
}
},
});
// add items in command palette and menu
[CommandIDs.create, CommandIDs.execute].forEach((command) => {
palette.addItem({ command, category });
exampleMenu.addItem({ command });
});
// Add launcher
if (launcher) {
launcher.add({
command: CommandIDs.create,
category: category,
});
}
}
export default extension;