-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathcom_interface.js
More file actions
117 lines (104 loc) · 3.7 KB
/
com_interface.js
File metadata and controls
117 lines (104 loc) · 3.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
/*
* 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() {
return Jupyter.notebook.get_selected_index();
}
/**
*
* @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='') {
var selectedIndex = getSelectedCell();
var targetCell = Jupyter.notebook.insert_cell_below(type, selectedIndex);
// Add signature
if (type == 'code') {
if (sigText !== '') {
command = com_util.formatString('# Visual Python: {0}\n', sigText) + command;
} else {
command = '# Visual Python\n' + command;
}
}
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());
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='') {
var selectedIndex = getSelectedCell();
var targetCell = Jupyter.notebook.insert_cell_below(type, selectedIndex);
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;
}
}
targetCell.set_text(command);
Jupyter.notebook.select_next();
if (exec) {
switch (type) {
case "markdown":
targetCell.render();
break;
case "code":
default:
targetCell.execute();
}
}
selectedIndex = getSelectedCell();
targetCell = Jupyter.notebook.insert_cell_below(type, selectedIndex);
});
// move to executed cell
Jupyter.notebook.scroll_to_cell(Jupyter.notebook.get_selected_index());
com_util.renderSuccessMessage('Your code is successfully generated.');
}
var enableOtherShortcut = function() {
vpLog.display(VP_LOG_TYPE.DEVELOP, 'enable short cut');
Jupyter.notebook.keyboard_manager.enable();
}
var disableOtherShortcut = function() {
vpLog.display(VP_LOG_TYPE.DEVELOP, 'disable short cut');
Jupyter.notebook.keyboard_manager.disable();
}
return {
insertCell: insertCell,
insertCells: insertCells,
enableOtherShortcut: enableOtherShortcut,
disableOtherShortcut: disableOtherShortcut,
};
});