-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathcom_Kernel.js
More file actions
324 lines (297 loc) · 12.4 KB
/
com_Kernel.js
File metadata and controls
324 lines (297 loc) · 12.4 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
323
324
/*
* Project Name : Visual Python
* Description : GUI-based Python code generator
* File Name : com_Kernel.js
* Author : Black Logic
* Note : Interface between vp and jupyter
* License : GNU GPLv3 with Visual Python special exception
* Date : 2021. 09. 16
* Change Date :
*/
//============================================================================
// [CLASS] Kernel
//============================================================================
define([
'./com_util',
'./com_String'
], function(com_util, com_String) {
/**
* Kernel interface class
*/
class Kernel {
/** constructor */
constructor() {
this.data = [
{ varName: 'df1', varType: 'DataFrame' },
{ varName: 'df2', varType: 'DataFrame' },
{ varName: 'df3', varType: 'DataFrame' },
{ varName: 's1', varType: 'Series' },
{ varName: 's2', varType: 'Series' },
{ varName: 's3', varType: 'Series' },
{ varName: 'l1', varType: 'List' },
{ varName: 'l2', varType: 'List' },
{ varName: 'l3', varType: 'List' }
]
this.config = {
'vpcfg': {
},
'vpudf': {
}
}
}
//====================================================================
// Executing command api
//====================================================================
execute(command, isSilent = false) {
return new Promise(function(resolve, reject) {
Jupyter.notebook.kernel.execute(
command,
{
iopub: {
output: function (msg) {
var result = '';
var type = '';
if (msg.msg_type == 'error') {
reject({result: msg.content, type: 'error', msg: msg});
} else {
if (msg.content) {
try {
if (msg.content['name'] == 'stderr') {
reject(msg);
} else {
if (msg.content['text']) {
result = String(msg.content['text']);
type = 'text';
} else if (msg.content.data) {
if (msg.content.data['text/plain']) {
result = String(msg.content.data['text/plain']);
type = 'text/plain';
} else if (msg.content.data['text/html']) {
result = String(msg.content.data['text/html']);
type = 'text/html';
} else if (msg.content.data['image/png']) {
result = String(msg.content.data['image/png']);
type = 'image/png';
}
}
resolve({result: result, type: type, msg: msg});
}
} catch(ex) {
reject(ex);
}
} else {
resolve({result: result, type: type, msg: msg});
}
}
}
}
},
{ silent: isSilent }
);
});
}
/**
* Check if module/function is loaded already
* @param {*} moduleList
*/
checkModule(moduleList) {
var that = this;
return new Promise(function(resolve, reject) {
that.execute(com_util.formatString('_vp_print(_vp_check_module_loaded({0}))', JSON.stringify(moduleList))).then(function(resultObj) {
// resolve
resolve(resultObj);
}).catch(function(err) {
// reject
reject(err);
})
});
}
getDataList(dataTypeList=[], excludeList=[]) {
// use function command to get variable list of selected data types
var cmdSB = '_vp_print(_vp_get_variables_list(None))';
if (!dataTypeList || dataTypeList.length <= 0) {
dataTypeList = [];
}
cmdSB = com_util.formatString('_vp_print(_vp_get_variables_list({0}, {1}))', JSON.stringify(dataTypeList), JSON.stringify(excludeList));
var that = this;
return new Promise(function(resolve, reject) {
that.execute(cmdSB.toString()).then(function(resultObj) {
// resolve
resolve(resultObj);
}).catch(function(err) {
// reject
reject(err);
})
})
}
getColumnList(dataframe) {
var that = this;
return new Promise(function(resolve, reject) {
that.execute(com_util.formatString('_vp_print(_vp_get_columns_list({0}))', dataframe))
.then(function(resultObj) {
resolve(resultObj);
}).catch(function(err) {
// reject
reject(err);
})
});
}
getCommonColumnList(dataframeList) {
var that = this;
return new Promise(function(resolve, reject) {
that.execute(com_util.formatString('_vp_print(_vp_get_multi_columns_list([{0}]))', dataframeList.join(',')))
.then(function(resultObj) {
resolve(resultObj);
}).catch(function(err) {
// reject
reject(err);
})
});
}
getColumnCategory(dataframe, columnName) {
var that = this;
return new Promise(function(resolve, reject) {
that.execute(com_util.formatString('_vp_print(_vp_get_column_category({0}, {1}))', dataframe, columnName))
.then(function(resultObj) {
resolve(resultObj);
}).catch(function(err) {
// reject
reject(err);
})
});
}
/**
* Get rows list
* @param {*} dataframe
* @returns
*/
getRowList(dataframe) {
var that = this;
return new Promise(function(resolve, reject) {
that.execute(com_util.formatString('_vp_print(_vp_get_rows_list({0}))', dataframe))
.then(function(resultObj) {
resolve(resultObj);
}).catch(function(err) {
// reject
reject(err);
})
});
}
getProfilingList() {
var that = this;
return new Promise(function(resolve, reject) {
that.execute('_vp_print(_vp_get_profiling_list())')
.then(function(resultObj) {
resolve(resultObj);
}).catch(function(err) {
// reject
reject(err);
})
});
}
//====================================================================
// FileNavigation
//====================================================================
getCurrentDirectory() {
var that = this;
return new Promise(function(resolve, reject) {
that.execute('%pwd')
.then(function(resultObj) {
resolve(resultObj.result);
}).catch(function(err) {
// reject
reject(err);
})
});
}
getFileList(path, useFunction=false) {
var that = this;
if (path === '') {
path = '.';
}
if (!useFunction) {
path = "'" + path + "'";
}
var cmd = com_util.formatString('_vp_print(_vp_search_path({0}))', path);
return new Promise(function(resolve, reject) {
that.execute(cmd)
.then(function(resultObj) {
resolve(resultObj.result);
}).catch(function(err) {
// reject
reject(err);
})
});
}
//====================================================================
// Image data
//====================================================================
getImageFile(path) {
var that = this;
let code = com_util.formatString("_vp_get_image_by_path('{0}')", path);
return new Promise(function(resolve, reject) {
that.execute(code)
.then(function(resultObj) {
resolve(resultObj);
}).catch(function(err) {
// reject
reject(err);
})
});
}
//====================================================================
// Write File
//====================================================================
saveFile(fileName, filePath, saveData) {
let that = this;
fetch(filePath).then(function(data) {
// overwrite confirmation
if (data.status == 200) {
if (!confirm(com_util.formatString("{0} already exists.\nDo you want to replace it?", fileName))) {
return false;
}
}
// write file
var sbfileSaveCmd = new com_String();
sbfileSaveCmd.appendFormatLine('%%writefile "{0}"', filePath);
sbfileSaveCmd.appendLine(saveData);
that.execute(sbfileSaveCmd.toString()).then(function(resultObj) {
com_util.renderSuccessMessage('Successfully saved file. (' + fileName + ')');
}).catch(function(err) {
com_util.renderAlertModal("Couldn't save file. "+err.message);
vpLog.display(VP_LOG_TYPE.ERROR, err);
});
});
}
//====================================================================
// Machine Learning
//====================================================================
getModelList(modelCategory='') {
// use function command to get variable list of selected data types
var cmdSB = `_vp_print(_vp_get_variables_list(${JSON.stringify(vpConfig.getMLDataTypes())}))`;
if (modelCategory != '') {
cmdSB = `_vp_print(_vp_get_variables_list(${JSON.stringify(vpConfig.getMLDataDict(modelCategory))}))`;
}
var that = this;
return new Promise(function(resolve, reject) {
that.execute(cmdSB).then(function(resultObj) {
// resolve
resolve(resultObj);
}).catch(function(err) {
// reject
reject(err);
})
})
}
//====================================================================
// Configuration api
//====================================================================
loadConfig() {
}
getConfig() {
}
setConfig() {
}
}
return Kernel;
});