-
Notifications
You must be signed in to change notification settings - Fork 331
/
index.tsx
643 lines (566 loc) · 21.2 KB
/
index.tsx
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
/*
* Copyright 2018-2020 IBM Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {JupyterFrontEnd, JupyterFrontEndPlugin, ILayoutRestorer} from '@jupyterlab/application';
import {ICommandPalette, showDialog, Dialog, ReactWidget, WidgetTracker} from '@jupyterlab/apputils';
import {DocumentRegistry, ABCWidgetFactory, DocumentWidget} from '@jupyterlab/docregistry';
import {IFileBrowserFactory} from '@jupyterlab/filebrowser';
import {ILauncher} from '@jupyterlab/launcher';
import {IMainMenu} from '@jupyterlab/mainmenu';
import {NotebookPanel} from "@jupyterlab/notebook";
import {IconRegistry, IIconRegistry} from '@jupyterlab/ui-components';
import {toArray} from '@phosphor/algorithm';
import {IDragEvent} from '@phosphor/dragdrop';
import {Widget, PanelLayout} from '@phosphor/widgets';
import {CommonCanvas, CanvasController, CommonProperties} from '@elyra/canvas';
import '@elyra/canvas/dist/common-canvas.min.css';
import {NotebookParser, SubmissionHandler} from "@elyra/application";
import 'carbon-components/css/carbon-components.min.css';
import '../style/index.css';
import * as palette from './palette.json';
import * as properties from './properties.json';
import * as i18nData from "./en.json";
import * as React from 'react';
import { IntlProvider } from "react-intl";
const PIPELINE_ICON_CLASS = 'jp-MaterialIcon elyra-PipelineIcon';
const PIPELINE_CLASS = 'elyra-PipelineEditor';
const PIPELINE_FACTORY = 'Pipeline Editor';
const PIPELINE = 'pipeline';
const PIPELINE_EDITOR_NAMESPACE = 'elyra-pipeline-editor-extension';
const commandIDs = {
openPipelineEditor : 'pipeline-editor:open',
openDocManager : 'docmanager:open',
newDocManager : 'docmanager:new-untitled'
};
/**
* Class for dialog that pops up for pipeline submission
*/
class PipelineDialog extends Widget implements Dialog.IBodyWidget<any> {
constructor(props: any) {
super(props);
let layout = (this.layout = new PanelLayout());
let htmlContent = this.getHtml(props);
// Set default runtime to kfp, since list is dynamically generated
(htmlContent.getElementsByClassName("elyra-form-runtime-config")[0] as HTMLSelectElement).value = "kfp";
layout.addWidget(new Widget( {node: htmlContent} ));
}
getValue() {
return {
'pipeline_name': (document.getElementById('pipeline_name') as HTMLInputElement).value,
'runtime_config': (document.getElementById('runtime_config') as HTMLInputElement).value
};
}
getHtml(props: any) {
let htmlContent = document.createElement('div');
let br = '<br/>';
let runtime_options = '';
let runtimes = props['runtimes'];
for (let key in runtimes) {
runtime_options = runtime_options + `<option value="${runtimes[key]['name']}">${runtimes[key]['display_name']}</option>`;
}
let content = ''
+ '<label for="pipeline_name">Pipeline Name:</label>'
+ br
+ '<input type="text" id="pipeline_name" name="pipeline_name" placeholder="Pipeline Name"/>'
+ br + br
+ '<label for="runtime_config">Runtime Config:</label>'
+ br
+ '<select id="runtime_config" name="runtime_config" class="elyra-form-runtime-config">'
+ runtime_options
+ '</select>';
htmlContent.innerHTML = content;
return htmlContent;
}
}
/**
* Class for Common Canvas React Component
*/
class Canvas extends ReactWidget {
app: JupyterFrontEnd;
browserFactory: IFileBrowserFactory;
context: DocumentRegistry.Context;
iconRegistry: IconRegistry;
constructor(props: any) {
super(props);
this.app = props.app;
this.browserFactory = props.browserFactory;
this.context = props.context;
this.iconRegistry = props.iconRegistry;
}
render() {
return <Pipeline
app={this.app}
browserFactory={this.browserFactory}
iconRegistry={this.iconRegistry}
widgetContext={this.context}
/>
}
}
/**
* A namespace for Pipeline.
*/
namespace Pipeline {
/**
* The props for Pipeline.
*/
export interface Props {
app: JupyterFrontEnd;
browserFactory: IFileBrowserFactory;
iconRegistry: IIconRegistry;
widgetContext: DocumentRegistry.Context;
}
/**
* The props for Pipeline.
*/
export interface State {
/**
* Whether the properties dialog is visible.
*/
showPropertiesDialog: boolean;
/**
* The form contents of the properties dialog.
*/
propertiesInfo: any;
}
}
class Pipeline extends React.Component<Pipeline.Props, Pipeline.State> {
app: JupyterFrontEnd;
browserFactory: IFileBrowserFactory;
iconRegistry: IconRegistry;
canvasController: any;
widgetContext: DocumentRegistry.Context;
position: number = 10;
node: React.RefObject<HTMLDivElement>;
constructor(props: any) {
super(props);
this.app = props.app;
this.browserFactory = props.browserFactory;
this.iconRegistry = props.iconRegistry;
this.canvasController = new CanvasController();
this.canvasController.setPipelineFlowPalette(palette);
this.widgetContext = props.widgetContext;
this.widgetContext.ready.then( () => {
this.canvasController.setPipelineFlow(this.widgetContext.model.toJSON());
});
this.toolbarMenuActionHandler = this.toolbarMenuActionHandler.bind(this);
this.contextMenuHandler = this.contextMenuHandler.bind(this);
this.contextMenuActionHandler = this.contextMenuActionHandler.bind(this);
this.editActionHandler = this.editActionHandler.bind(this);
this.state = {showPropertiesDialog: false, propertiesInfo: {}};
this.applyPropertyChanges = this.applyPropertyChanges.bind(this);
this.closePropertiesDialog = this.closePropertiesDialog.bind(this);
this.openPropertiesDialog = this.openPropertiesDialog.bind(this);
this.node = React.createRef();
this.handleEvent = this.handleEvent.bind(this);
}
render() {
const style = { height: '100%' };
const canvasConfig = {
enableInternalObjectModel: true,
enablePaletteLayout: 'Modal',
paletteInitialState: false
};
const toolbarConfig = [
{ action: 'add', label: 'Add Notebook to Pipeline', enable: true, iconEnabled: '/'},
{ divider: true },
{ action: 'run', label: 'Run Pipeline', enable: true },
{ divider: true },
{ action: 'save', label: 'Save Pipeline', enable: true },
{ divider: true },
// { action: 'open', label: 'Open Pipeline', enable: true },
// { divider: true },
{ action: 'new', label: 'New Pipeline', enable: true },
{ divider: true },
{ action: 'clear', label: 'Clear Pipeline', enable: true },
{ divider: true },
{ action: 'undo', label: 'Undo', enable: true },
{ action: 'redo', label: 'Redo', enable: true },
{ action: 'cut', label: 'Cut', enable: false },
{ action: 'copy', label: 'Copy', enable: false },
{ action: 'paste', label: 'Paste', enable: false },
{ action: 'addComment', label: 'Add Comment', enable: true },
{ action: 'delete', label: 'Delete', enable: true },
{ action: 'arrangeHorizontally', label: 'Arrange Horizontally', enable: true },
{ action: 'arrangeVertically', label: 'Arrange Vertically', enable: true } ];
const propertiesCallbacks = {
applyPropertyChanges: this.applyPropertyChanges,
closePropertiesDialog: this.closePropertiesDialog
};
const commProps = this.state.showPropertiesDialog ? <IntlProvider key='IntlProvider2' locale={ 'en' } messages={ i18nData.messages }>
<CommonProperties
propertiesInfo={this.propertiesInfo}
propertiesConfig={{ }}
callbacks={propertiesCallbacks}
/>
</IntlProvider> : null;
return (
<div style={style} ref={this.node}>
<CommonCanvas
canvasController={this.canvasController}
toolbarMenuActionHandler={this.toolbarMenuActionHandler}
contextMenuHandler={this.contextMenuHandler}
contextMenuActionHandler={this.contextMenuActionHandler}
editActionHandler={this.editActionHandler}
toolbarConfig={toolbarConfig}
config={canvasConfig}
/>
{commProps}
</div>
);
}
propertiesInfo = { 'parameterDef': properties, 'appData': { 'id': '' } };
openPropertiesDialog(source: any) {
console.log('Opening properties dialog');
let node_id = source.targetObject.id;
let app_data = this.canvasController.getNode(node_id).app_data;
let node_props = this.propertiesInfo;
node_props.appData.id = node_id;
node_props.parameterDef.current_parameters.image = app_data.image;
node_props.parameterDef.current_parameters.outputs = app_data.outputs;
node_props.parameterDef.current_parameters.vars = app_data.vars;
node_props.parameterDef.current_parameters.dependencies = app_data.dependencies;
node_props.parameterDef.current_parameters.recursive_dependencies = app_data.recursive_dependencies;
this.setState({showPropertiesDialog: true, propertiesInfo: node_props});
}
applyPropertyChanges(propertySet: any, appData: any) {
console.log('Applying changes to properties');
let app_data = this.canvasController.getNode(appData.id).app_data;
app_data.image = propertySet.image;
app_data.outputs = propertySet.outputs;
app_data.vars = propertySet.vars;
app_data.dependencies = propertySet.dependencies;
app_data.recursive_dependencies = propertySet.recursive_dependencies;
};
closePropertiesDialog() {
console.log('Closing properties dialog');
this.setState({showPropertiesDialog: false, propertiesInfo: {}});
};
contextMenuHandler(source: any, defaultMenu: any) {
let customMenu = defaultMenu;
if (source.type === 'node') {
if (source.selectedObjectIds.length > 1) {
customMenu = customMenu.concat({ action: 'openNotebook', label: 'Open Notebooks'});
} else {
customMenu = customMenu.concat({ action: 'openNotebook', label: 'Open Notebook'});
}
customMenu = customMenu.concat({ action: 'properties', label: 'Properties'});
}
return customMenu;
}
contextMenuActionHandler( action: any, source: any ) {
if (action === 'openNotebook' && source.type === 'node') {
let nodes = source.selectedObjectIds;
for (let i = 0; i < nodes.length; i++) {
let path = this.canvasController.getNode(nodes[i]).app_data.artifact;
this.app.commands.execute(commandIDs.openDocManager, {path});
}
} else if (action === 'properties' && source.type === 'node') {
if (this.state.showPropertiesDialog) {
this.closePropertiesDialog();
} else {
this.openPropertiesDialog(source);
}
}
}
/*
* Handles creating new nodes in the canvas
*/
editActionHandler(data: any) {
this.widgetContext.model.fromJSON(this.canvasController.getPipelineFlow());
}
handleAdd(x?: number, y?: number) {
let failedAdd = 0;
let position = 0;
let missingXY = !(x && y);
// if either x or y is undefined use the default coordinates
if (missingXY) {
position = this.position;
x = 75;
y = 85;
}
let fileBrowser = this.browserFactory.defaultBrowser;
toArray(fileBrowser.selectedItems()).map(
item => {
// if the selected item is a file
if (item.type == 'notebook') {
//add each selected notebook
console.log('Adding ==> ' + item.path );
const nodeTemplate = this.canvasController.getPaletteNode('execute-notebook-node');
if (nodeTemplate) {
const data = {
'editType': 'createNode',
'offsetX': x + position,
'offsetY': y + position,
'nodeTemplate': this.canvasController.convertNodeTemplate(nodeTemplate)
};
// create a notebook widget to get a string with the node content then dispose of it
let notebookWidget = fileBrowser.model.manager.open(item.path);
let notebookStr = (notebookWidget as NotebookPanel).content.model.toString();
notebookWidget.dispose();
let vars = NotebookParser.getEnvVars(notebookStr).map(str => str + '=');
data.nodeTemplate.label = item.path.replace(/^.*[\\\/]/, '');
data.nodeTemplate.label = data.nodeTemplate.label.replace(/\.[^/.]+$/, '');
data.nodeTemplate.image = 'data:image/svg+xml;utf8,' + encodeURIComponent(this.iconRegistry.svg('notebook'));
data.nodeTemplate.app_data['artifact'] = item.path;
data.nodeTemplate.app_data['image'] = this.propertiesInfo.parameterDef.current_parameters.image;
data.nodeTemplate.app_data['vars'] = vars;
this.canvasController.editActionHandler(data);
position += 20;
}
} else {
failedAdd++;
}
}
);
// update position if the default coordinates were used
if (missingXY) {
this.position = position;
}
if (failedAdd) {
return showDialog({
title: 'Unsupported File(s)',
body: 'Currently only notebook files can be added to a pipeline',
buttons: [Dialog.okButton()]
});
}
}
handleRun() {
SubmissionHandler.makeGetRequest('api/metadata/runtimes', 'pipeline', (response: any) =>
showDialog({
title: 'Run pipeline',
body: new PipelineDialog({"runtimes": response.runtimes}),
buttons: [Dialog.cancelButton(), Dialog.okButton()],
focusNodeSelector: '#pipeline_name'
}).then( result => {
if( result.value == null) {
// When Cancel is clicked on the dialog, just return
return;
}
// prepare pipeline submission details
let pipelineFlow = this.canvasController.getPipelineFlow();
pipelineFlow.pipelines[0]['app_data']['title'] = result.value.pipeline_name;
// TODO: Be more flexible and remove hardcoded runtime type
pipelineFlow.pipelines[0]['app_data']['runtime'] = 'kfp';
pipelineFlow.pipelines[0]['app_data']['runtime-config'] = result.value.runtime_config;
SubmissionHandler.submitPipeline(pipelineFlow, result.value.runtime_config, 'pipeline');
})
);
}
handleSave() {
this.widgetContext.model.fromJSON(this.canvasController.getPipelineFlow());
this.widgetContext.save();
}
handleOpen() {
toArray(this.browserFactory.defaultBrowser.selectedItems()).map(
item => {
// if the selected item is a file
if (item.type != 'directory') {
console.log('Opening ==> ' + item.path );
this.app.commands.execute(commandIDs.openDocManager, { path: item.path });
}
}
)
}
handleNew() {
// Clears the canvas, then creates a new file and sets the pipeline_name field to the new name.
this.app.commands.execute(commandIDs.openPipelineEditor);
}
handleClear() {
return showDialog({
title: 'Clear Pipeline?',
body: 'Are you sure you want to clear? You can not undo this.',
buttons: [Dialog.cancelButton(), Dialog.okButton({ label: 'Clear' })]
}).then( result => {
if (result.button.accept) {
this.canvasController.clearPipelineFlow();
this.widgetContext.model.fromJSON(this.canvasController.getPipelineFlow());
this.position = 10;
}
});
}
/**
* Handles submitting pipeline runs
*/
toolbarMenuActionHandler(action: any, source: any) {
console.log('Handling action: ' + action);
if(action == 'add') { // When adding a new node to the pipeline
this.handleAdd();
} else if (action == 'run') { // When executing the pipeline
this.handleRun();
} else if (action == 'save') {
this.handleSave();
} else if (action == 'open') {
this.handleOpen();
} else if (action == 'new') {
this.handleNew();
} else if (action == 'clear') {
this.handleClear();
}
}
componentDidMount(): void {
let node = this.node.current!;
node.addEventListener('dragenter', this.handleEvent);
node.addEventListener('dragover', this.handleEvent);
node.addEventListener('p-dragover', this.handleEvent);
node.addEventListener('p-drop', this.handleEvent);
}
componentWillUnmount(): void {
let node = this.node.current!;
node.removeEventListener('p-drop', this.handleEvent);
node.removeEventListener('p-dragover', this.handleEvent);
node.removeEventListener('dragover', this.handleEvent);
node.removeEventListener('dragenter', this.handleEvent);
}
/**
* Handle the DOM events.
*
* @param event - The DOM event.
*/
handleEvent(event: Event): void {
switch (event.type) {
case 'dragenter':
event.preventDefault();
break;
case 'dragover':
event.preventDefault();
break;
case 'p-dragover':
event.preventDefault();
event.stopPropagation();
(event as IDragEvent).dropAction = (event as IDragEvent).proposedAction;
break;
case 'p-drop':
event.preventDefault();
event.stopPropagation();
this.handleAdd((event as IDragEvent).offsetX, (event as IDragEvent).offsetY);
break;
default:
break;
}
}
}
class PipelineEditorFactory extends ABCWidgetFactory<DocumentWidget> {
app: JupyterFrontEnd;
browserFactory: IFileBrowserFactory;
iconRegistry: IconRegistry;
constructor(options: any) {
super(options);
this.app = options.app;
this.browserFactory = options.browserFactory;
this.iconRegistry = options.iconRegistry;
}
protected createNewWidget(
context: DocumentRegistry.Context
): DocumentWidget {
// Creates a blank widget with a DocumentWidget wrapper
let props = {
app: this.app,
browserFactory: this.browserFactory,
iconRegistry: this.iconRegistry,
context: context
};
const content = new Canvas(props);
const widget = new DocumentWidget({ content, context, node: document.createElement('div') });
widget.addClass(PIPELINE_CLASS);
widget.title.iconClass = PIPELINE_ICON_CLASS;
return widget;
}
}
/**
* Initialization data for the pipeline-editor-extension extension.
*/
const extension: JupyterFrontEndPlugin<void> = {
id: PIPELINE,
autoStart: true,
requires: [ICommandPalette, ILauncher, IFileBrowserFactory, ILayoutRestorer, IMainMenu, IIconRegistry],
activate: (
app: JupyterFrontEnd,
palette: ICommandPalette,
launcher: ILauncher,
browserFactory: IFileBrowserFactory,
restorer: ILayoutRestorer,
menu: IMainMenu,
iconRegistry: IIconRegistry
) => {
console.log('Elyra - pipeline-editor extension is activated!');
// Set up new widget Factory for .pipeline files
const pipelineEditorFactory = new PipelineEditorFactory({
name: PIPELINE_FACTORY,
fileTypes: [PIPELINE],
defaultFor: [PIPELINE],
app: app,
browserFactory: browserFactory,
iconRegistry: iconRegistry
});
// Add the default behavior of opening the widget for .pipeline files
app.docRegistry.addFileType({ name: PIPELINE, extensions: ['.pipeline'], iconClass: PIPELINE_ICON_CLASS});
app.docRegistry.addWidgetFactory(pipelineEditorFactory);
const tracker = new WidgetTracker<DocumentWidget>({
namespace: PIPELINE_EDITOR_NAMESPACE
});
pipelineEditorFactory.widgetCreated.connect((sender, widget) => {
void tracker.add(widget);
// Notify the widget tracker if restore data needs to update
widget.context.pathChanged.connect(() => {
void tracker.save(widget);
});
});
// Handle state restoration
void restorer.restore(tracker,
{
command: commandIDs.openDocManager,
args: widget => ({
path: widget.context.path,
factory: PIPELINE_FACTORY
}),
name: widget => widget.context.path
});
// Add an application command
const openPipelineEditorCommand: string = commandIDs.openPipelineEditor;
app.commands.addCommand(openPipelineEditorCommand, {
label: args => (args['isPalette'] ? 'New Pipeline Editor' : 'Pipeline Editor'),
iconClass: args => (args['isPalette'] ? '' : PIPELINE_ICON_CLASS),
execute: () => {
// Creates blank file, then opens it in a new window
app.commands.execute(commandIDs.newDocManager, {
type: 'file',
path: browserFactory.defaultBrowser.model.path,
ext: '.pipeline'
})
.then(model => {
return app.commands.execute(commandIDs.openDocManager, {
path: model.path,
factory: PIPELINE_FACTORY
});
});
}
});
// Add the command to the palette.
palette.addItem({command: openPipelineEditorCommand, args: { isPalette: true }, category: 'Extensions'});
if (launcher) {
launcher.add({
command: openPipelineEditorCommand,
category: 'Other',
rank: 3
});
}
// Add new pipeline to the file menu
menu.fileMenu.newMenu.addGroup(
[{ command: openPipelineEditorCommand }],
30
);
}
};
export default extension;