-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.ts
251 lines (223 loc) · 6.64 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import {
ILayoutRestorer,
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';
import {
ICommandPalette,
MainAreaWidget,
WidgetTracker
} from '@jupyterlab/apputils';
import { IMainMenu } from '@jupyterlab/mainmenu';
import { ISettingRegistry } from '@jupyterlab/settingregistry';
import {
CondaEnvironments,
CondaEnvWidget,
condaIcon,
CONDA_WIDGET_CLASS,
IEnvironmentManager
} from '@mamba-org/gator-common';
import { INotification } from 'jupyterlab_toastify';
import { managerTour } from './tour';
import {
companionID,
CompanionValidator,
ICompanionValidator
} from './validator';
const CONDAENVID = '@mamba-org/gator-lab:plugin';
const TOUR_DELAY = 1000;
const TOUR_TIMEOUT = 5 * TOUR_DELAY + 1;
async function activateCondaEnv(
app: JupyterFrontEnd,
settingsRegistry: ISettingRegistry | null,
palette: ICommandPalette | null,
menu: IMainMenu | null,
restorer: ILayoutRestorer | null
): Promise<IEnvironmentManager> {
let tour: any;
const { commands, shell } = app;
const pluginNamespace = 'conda-env';
const command = 'jupyter_conda:open-ui';
const settings = await settingsRegistry?.load(CONDAENVID);
const model = new CondaEnvironments(settings);
// Request listing available package as quickly as possible
if (settings?.get('backgroundCaching').composite ?? true) {
Private.loadPackages(model);
}
// Track and restore the widget state
const tracker = new WidgetTracker<MainAreaWidget<CondaEnvWidget>>({
namespace: pluginNamespace
});
let condaWidget: MainAreaWidget<CondaEnvWidget>;
commands.addCommand(command, {
label: 'Conda Packages Manager',
execute: () => {
app.restored.then(() => {
let timeout = 0;
const delayTour = (): void => {
setTimeout(() => {
timeout += TOUR_DELAY;
if (condaWidget?.isVisible && tour) {
commands.execute('jupyterlab-tour:launch', {
id: tour.id,
force: false
});
} else if (timeout < TOUR_TIMEOUT) {
delayTour();
}
}, 1000);
};
if (commands.hasCommand('jupyterlab-tour:add')) {
if (!tour) {
commands
.execute('jupyterlab-tour:add', {
tour: managerTour as any
})
.then(result => {
tour = result;
});
}
delayTour();
}
});
if (!condaWidget || condaWidget.isDisposed) {
condaWidget = new MainAreaWidget({
content: new CondaEnvWidget(model)
});
condaWidget.addClass(CONDA_WIDGET_CLASS);
condaWidget.id = pluginNamespace;
condaWidget.title.label = 'Packages';
condaWidget.title.caption = 'Conda Packages Manager';
condaWidget.title.icon = condaIcon;
}
if (!tracker.has(condaWidget)) {
// Track the state of the widget for later restoration
tracker.add(condaWidget);
}
if (!condaWidget.isAttached) {
// Attach the widget to the main work area if it's not there
shell.add(condaWidget, 'main');
}
shell.activateById(condaWidget.id);
}
});
// Add command to command palette
if (palette) {
palette.addItem({ command, category: 'Settings' });
}
// Handle state restoration.
if (restorer) {
restorer.restore(tracker, {
command,
name: () => pluginNamespace
});
}
// Add command to settings menu
if (menu) {
menu.settingsMenu.addGroup([{ command: command }], 999);
}
return model;
}
async function activateCompanions(
app: JupyterFrontEnd,
envManager: IEnvironmentManager,
settingsRegistry: ISettingRegistry,
palette: ICommandPalette | null
): Promise<ICompanionValidator> {
const { commands, serviceManager } = app;
const command = 'jupyter_conda:companions';
const settings = await settingsRegistry.load(CONDAENVID);
const validator = new CompanionValidator(
serviceManager.kernelspecs,
envManager,
settings
);
commands.addCommand(command, {
label: 'Validate kernels compatibility',
execute: () => {
validator.validate(serviceManager.kernelspecs.specs);
}
});
// Add command to command palette
if (palette) {
palette.addItem({ command, category: 'Troubleshooting' });
}
return validator;
}
/**
* Initialization data for the @mamba-org/gator-lab extension.
*/
const condaManager: JupyterFrontEndPlugin<IEnvironmentManager> = {
id: CONDAENVID,
autoStart: true,
activate: activateCondaEnv,
optional: [ISettingRegistry, ICommandPalette, IMainMenu, ILayoutRestorer],
provides: IEnvironmentManager
};
/**
* Initialization data for the jupyterlab_kernel_companions extension.
*/
const companions: JupyterFrontEndPlugin<ICompanionValidator> = {
id: companionID,
autoStart: true,
activate: activateCompanions,
requires: [IEnvironmentManager, ISettingRegistry],
optional: [ICommandPalette],
provides: ICompanionValidator
};
const extensions = [condaManager, companions];
export default extensions;
/* eslint-disable no-inner-declarations */
namespace Private {
export function loadPackages(model: CondaEnvironments): void {
let packageFound = false;
let toastId: React.ReactText;
const messages = [
'I know you want to give up, but wait a bit longer...',
'Why is conda so popular, still loading that gigantic packages list...',
'Take a break, available packages list are still loading...',
'Available packages list still loading...'
];
function displayMessage(message: React.ReactNode): void {
setTimeout(() => {
if (!packageFound) {
INotification.update({
message,
toastId
});
if (messages.length > 0) {
displayMessage(messages.pop());
}
}
}, 60000);
}
model
.getPackageManager()
.refreshAvailablePackages(false)
.then(() => {
packageFound = true;
if (toastId) {
INotification.dismiss(toastId);
}
})
.catch((reason: Error) => {
console.debug('Fail to cache available packages list.', reason);
if (toastId) {
INotification.dismiss(toastId);
}
});
// Tell the user after a minute than the extension if still trying to get
// the available packages list
setTimeout(() => {
if (!packageFound) {
INotification.inProgress(
'Loading the available packages list in background...'
).then(id => {
toastId = id;
});
displayMessage(messages.pop());
}
}, 60000);
}
}
/* eslint-enable no-inner-declarations */