-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathcom_interface.js
More file actions
322 lines (297 loc) · 13.7 KB
/
com_interface.js
File metadata and controls
322 lines (297 loc) · 13.7 KB
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
/*
* Project Name : Visual Python
* Description : GUI-based Python code generator
* File Name : com_interface.js
* Author : Black Logic
* Note : Interface for jupyter
* License : GNU GPLv3 with Visual Python special exception
* Date : 2021. 09. 16
* Change Date :
*/
define([
'./com_util',
'./com_String'
], function(com_util, com_String) {
var getSelectedCell = function() {
if (vpConfig.extensionType === 'notebook') {
return Jupyter.notebook.get_selected_index();
} else if (vpConfig.extensionType === 'colab') {
if (colab.global.notebook.focusedCell) {
return colab.global.notebook.focusedCell.cellId;
} else {
return '';
}
}
}
/**
*
* @param {String} type code / markdown
* @param {String} command
* @param {boolean} exec true(default) / false
* @param {int} sigNum
*/
var insertCell = function(type, command, exec=true, sigText='') {
// Add signature
if (type == 'code') {
if (sigText !== '') {
command = com_util.formatString('# Visual Python: {0}\n', sigText) + command;
} else {
command = '# Visual Python\n' + command;
}
}
if (vpConfig.extensionType === 'notebook') {
var selectedIndex = getSelectedCell();
var targetCell = Jupyter.notebook.insert_cell_below(type, selectedIndex);
targetCell.set_text(command);
Jupyter.notebook.select_next();
if (exec) {
switch (type) {
case "markdown":
targetCell.render();
break;
case "code":
default:
targetCell.execute();
}
}
// move to executed cell
Jupyter.notebook.scroll_to_cell(Jupyter.notebook.get_selected_index());
} else if (vpConfig.extensionType === 'colab') {
// CHROME: use colab api to add cell
let cell = null;
// get focused index
let lastFocusedCellId = '';
let lastFocusedCellIdx = 0;
try {
// get focused cell id
lastFocusedCellId = colab.global.notebook.focusedCell? colab.global.notebook.focusedCell.getId() : null;
// get focused cell index
lastFocusedCellIdx = colab.global.notebook.cells.findIndex(cell => cell.getId() === lastFocusedCellId);
} catch (err) {
vpLog.display(VP_LOG_TYPE.ERROR, err);
}
if (exec) {
switch (type) {
case "markdown":
// add text
// create cell to execute
colab.global.notebook.insertCell('text', {after:true, index:lastFocusedCellIdx});
setTimeout(() => {
// cell = colab.global.notebook.focusedCell;
cell = colab.global.notebook.cells[lastFocusedCellIdx + 1];
cell.setText(command);
// colab text cell's focus out is same as running it
cell.setFocused(false);
}, 300);
break;
case "code":
default:
// add code cell
// colab.global.notebook.notebookToolbar.toolbarButtons.get("insert-cell-below").click();
// create cell to execute
colab.global.notebook.insertCell('code', {after:true, index:lastFocusedCellIdx});
setTimeout(() => {
// cell = colab.global.notebook.focusedCell;
cell = colab.global.notebook.cells[lastFocusedCellIdx + 1];
cell.setText(command);
cell.runButton.click();
}, 300);
}
}
// move to executed cell
// CHROME: TODO:
} else if (vpConfig.extensionType === 'lab') {
var { NotebookActions } = require('@jupyterlab/notebook');
var notebookPanel = vpKernel.getLabNotebookPanel();
if (notebookPanel && notebookPanel.sessionContext){
var sessionContext = notebookPanel.sessionContext;
let sessionType = sessionContext.type;
if (sessionType === 'notebook') {
var notebook = notebookPanel.content;
var notebookModel = notebook.model;
var cellModel = notebookModel.contentFactory.createCell(type, {});
cellModel.value.text = command;
const newCellIndex = notebook.activeCellIndex + 1;
// 셀 추가
notebookModel.cells.insert(newCellIndex, cellModel);
notebook.activeCellIndex = newCellIndex;
var cell = notebook.activeCell;
if (exec == true) {
try{
NotebookActions.run(notebook, sessionContext);
} catch(err){
vpLog.display(VP_LOG_TYPE.ERROR, err);
}
}
// move to executed cell
let activeCell = notebookPanel.content.activeCell;
let activeCellTop = $(activeCell.node).position().top;
// scroll to current cell top
$(notebookPanel.layout.widgets[2].node).animate({scrollTop: activeCellTop},"fast");
} else if (sessionType === 'console') {
var labConsole = notebookPanel.content;
var widget = labConsole.widgets[0];
// execute or not
if (exec == true) {
// console allow only code cell
var cellModel = widget.createCodeCell();
cellModel.model.value.text = command;
widget.addCell(cellModel);
widget._execute(cellModel);
} else {
widget.promptCell.model.value.text = command;
}
widget.promptCell.inputArea.editor.focus();
}
} else {
// No session found
com_util.renderAlertModal('Visual Python only supports Notebook and Console type. Please use appropriate type of file to use it.');
}
}
com_util.renderSuccessMessage('Your code is successfully generated.');
}
/**
* Insert multiple cells
* @param {String} type
* @param {Array} commands
* @param {boolean} exec
* @param {int} sigNum
*/
var insertCells = function(type, commands, exec=true, sigText='') {
if (vpConfig.extensionType === 'lab') {
var { NotebookActions } = require('@jupyterlab/notebook');
var notebookPanel = vpKernel.getLabNotebookPanel();
}
commands && commands.forEach((command, idx) => {
// Add signature
if (type == 'code') {
if (sigText !== '') {
command = com_util.formatString('# Visual Python: {0}\n', sigText) + command;
} else {
command = com_util.formatString('# Visual Python') + command;
}
}
if (vpConfig.extensionType === 'notebook') {
var selectedIndex = getSelectedCell();
var targetCell = Jupyter.notebook.insert_cell_below(type, selectedIndex);
targetCell.set_text(command);
Jupyter.notebook.select_next();
if (exec) {
switch (type) {
case "markdown":
targetCell.render();
break;
case "code":
default:
targetCell.execute();
}
}
} else if (vpConfig.extensionType === 'colab') {
// CHROME: use colab api to add cell
let cell = null;
// get focused index
let lastFocusedCellId = '';
let lastFocusedCellIdx = 0;
try {
// get focused cell id
lastFocusedCellId = colab.global.notebook.focusedCell? colab.global.notebook.focusedCell.getId() : colab.global.notebook.lastFocusedCellId;
// get focused cell index
lastFocusedCellIdx = colab.global.notebook.cells.findIndex(cell => cell.getId() === lastFocusedCellId);
} catch (err) {
vpLog.display(VP_LOG_TYPE.ERROR, err);
}
if (exec) {
switch (type) {
case "markdown":
// add text
// create cell to execute
colab.global.notebook.insertCell('text', {after:true, index:lastFocusedCellIdx});
setTimeout(() => {
// cell = colab.global.notebook.focusedCell;
cell = colab.global.notebook.cells[lastFocusedCellIdx + 1];
cell.setText(command);
// colab text cell's focus out is same as running it
cell.setFocused(false);
}, 300);
break;
case "code":
default:
colab.global.notebook.insertCell('code', {after:true, index:lastFocusedCellIdx});
setTimeout(() => {
// cell = colab.global.notebook.focusedCell;
cell = colab.global.notebook.cells[lastFocusedCellIdx + 1];
cell.setText(command);
cell.runButton.click();
}, 300);
}
}
} else if (vpConfig.extensionType === 'lab') {
if (notebookPanel && notebookPanel.sessionContext){
var sessionContext = notebookPanel.sessionContext;
let sessionType = sessionContext.type;
if (sessionType === 'notebook') {
var notebook = notebookPanel.content;
var notebookModel = notebook.model;
var options = { };
var cellModel = notebookModel.contentFactory.createCell(type, options);
cellModel.value.text = command;
const newCellIndex = notebook.activeCellIndex + 1;
// 셀 추가
notebookModel.cells.insert(newCellIndex, cellModel);
notebook.activeCellIndex = newCellIndex;
var cell = notebook.activeCell;
if (exec == true) {
try{
NotebookActions.run(notebook, sessionContext);
} catch(err){
vpLog.display(VP_LOG_TYPE.ERROR, err);
}
}
} else if (sessionType === 'console') {
var console = notebookPanel.content;
var cellModel = console.contentFactory.createCell(type, {});
cellModel.value.text = command;
}
} else {
// No session found
com_util.renderAlertModal('Visual Python only supports Notebook and Console type. Please use appropriate type of file to use it.');
}
}
});
// move to executed cell
if (vpConfig.extensionType === 'notebook') {
Jupyter.notebook.scroll_to_cell(Jupyter.notebook.get_selected_index());
} else if (vpConfig.extensionType === 'colab') {
// CHROME: TODO:
} else if (vpConfig.extensionType === 'lab') {
// LAB: TODO:
let activeCell = notebookPanel.content.activeCell;
let activeCellTop = $(activeCell.node).position().top;
// scroll to current cell top
$(notebookPanel.layout.widgets[2].node).animate({scrollTop: activeCellTop},"fast");
}
com_util.renderSuccessMessage('Your code is successfully generated.');
}
var enableOtherShortcut = function() {
vpLog.display(VP_LOG_TYPE.DEVELOP, 'enable short cut');
if (vpConfig.extensionType == 'notebook') {
Jupyter.notebook.keyboard_manager.enable();
} else if (vpConfig.extensionType == 'colab') {
;
}
}
var disableOtherShortcut = function() {
vpLog.display(VP_LOG_TYPE.DEVELOP, 'disable short cut');
if (vpConfig.extensionType == 'notebook') {
Jupyter.notebook.keyboard_manager.disable();
} else if (vpConfig.extensionType == 'colab') {
;
}
}
return {
insertCell: insertCell,
insertCells: insertCells,
enableOtherShortcut: enableOtherShortcut,
disableOtherShortcut: disableOtherShortcut,
};
});