forked from visualpython/visualpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvpProfiling.js
More file actions
491 lines (423 loc) · 20.5 KB
/
Copy pathvpProfiling.js
File metadata and controls
491 lines (423 loc) · 20.5 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
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
define([
'require'
, 'jquery'
, 'nbextensions/visualpython/src/common/constant'
, 'nbextensions/visualpython/src/common/StringBuilder'
, 'nbextensions/visualpython/src/common/vpCommon'
, 'nbextensions/visualpython/src/common/component/vpSuggestInputText'
, 'nbextensions/visualpython/src/pandas/common/pandasGenerator'
, 'nbextensions/visualpython/src/common/component/vpVarSelector'
, 'nbextensions/visualpython/src/common/kernelApi'
// file navigation
, 'nbextensions/visualpython/src/pandas/fileNavigation/index'
], function (requirejs, $
, vpConst, sb, vpCommon, vpSuggestInputText, pdGen, vpVarSelector, kernelApi
, fileNavigation) {
const VP_PF = 'vp-pf';
const VP_PF_CONTAINER = 'vp-pf-container';
const VP_PF_TITLE = 'vp-pf-title';
const VP_PF_CLOSE = 'vp-pf-close';
const VP_PF_BODY = 'vp-pf-body';
const VP_PF_GRID_BOX = 'vp-pf-grid-box';
const VP_PF_PREPARE_BOX = 'vp-pf-prepare-box';
const VP_PF_INSTALL_BTN = 'vp-pf-install-btn';
const VP_PF_CHECK_BTN = 'vp-pf-check-btn';
const VP_PF_IMPORT_BTN = 'vp-pf-import-btn';
const VP_PF_SHOW_BOX = 'vp-pf-show-box';
const VP_PF_DF_BOX = 'vp-pf-df-box';
const VP_PF_DF_REFRESH = 'vp-pf-df-refresh';
const VP_PF_MENU_ITEM = 'vp-pf-menu-item';
const VP_PF_LIST_BOX = 'vp-pf-list-box';
const VP_PF_LIST_HEADER = 'vp-pf-list-header';
const VP_PF_LIST_HEADER_ITEM = 'vp-pf-list-header-item';
const VP_PF_LIST_BODY = 'vp-pf-list-body';
const VP_PF_LIST_ITEM = 'vp-pf-list-item';
const VP_PF_LIST_BUTTON_BOX = 'vp-pf-list-button-box';
const VP_PF_LIST_MENU_ITEM = 'vp-pf-list-menu-item';
const VP_PF_FILEPATH = 'vp-pf-filepath';
const VP_PF_FILENAME = 'vp-pf-filename';
const PROFILE_TYPE = {
NONE: -1,
GENERATE: 1
}
const LIST_MENU_ITEM = {
SHOW: 'show',
DELETE: 'delete',
SAVE: 'save'
}
/**
* @class Profiling
* @param {object} pageThis
* @param {string} targetId
* @constructor
*/
var Profiling = function(pageThis, targetId) {
this.pageThis = pageThis;
this.targetId = targetId;
this.uuid = 'u' + vpCommon.getUUID();
// file navigation
this.state = {
paramData:{
encoding: "utf-8" // encoding
, delimiter: "," // seperater
},
returnVariable:"",
isReturnVariable: false,
fileExtension: ["html"],
visualpythonFileName: 'report',
selectedReport: ''
};
this.fileResultState = {
pathInputId : this.wrapSelector('.' + VP_PF_FILEPATH),
fileInputId : this.wrapSelector('.' + VP_PF_FILENAME)
};
}
Profiling.prototype.wrapSelector = function(query = '') {
return vpCommon.formatString('.{0}.{1} {2}', VP_PF, this.uuid, query);
}
Profiling.prototype.open = function() {
this.init();
$(this.wrapSelector()).show();
}
Profiling.prototype.close = function() {
this.unbindEvent();
$(this.wrapSelector()).remove();
}
Profiling.prototype.init = function() {
// state
vpCommon.loadCss(Jupyter.notebook.base_url + vpConst.BASE_PATH + vpConst.STYLE_PATH + "common/profiling.css");
this.render();
this.checkInstalled();
this.bindEvent();
this.loadVariableList();
this.loadReportList();
}
Profiling.prototype.render = function() {
var page = new sb.StringBuilder();
page.appendFormatLine('<div class="{0} {1}">', VP_PF, this.uuid);
page.appendFormatLine('<div class="{0}">', VP_PF_CONTAINER);
// title
page.appendFormat('<div class="{0}">{1}</div>'
, VP_PF_TITLE
, 'Pandas Profiling');
// close button
page.appendFormatLine('<div class="{0}"><img src="{1}"/></div>'
, VP_PF_CLOSE, '/nbextensions/visualpython/resource/close_big.svg');
// body start
page.appendFormatLine('<div class="{0}">', VP_PF_BODY);
page.appendFormatLine('<div class="{0}">', VP_PF_GRID_BOX);
// prepare box
page.appendFormatLine('<div class="{0}">', VP_PF_PREPARE_BOX);
page.appendFormatLine('<label>{0} <a href="{1}" target="_blank"><i class="{2} {3}" title="{4}"></i></a></label>'
, 'Prepare to use Pandas Profiling', 'https://github.com/pandas-profiling/pandas-profiling', 'fa fa-link', 'vp-pf-link', 'Go to pandas-profiling github page');
page.appendLine('<div>');
page.appendFormatLine('<button class="{0} {1}">{2}</button>', 'vp-button activated', VP_PF_INSTALL_BTN, 'Install');
page.appendFormatLine('<div class="{0} {1}" title="{2}"><img src="{3}"/></div>', 'vp-cursor', VP_PF_CHECK_BTN, 'Check if installed', '/nbextensions/visualpython/resource/refresh.svg');
// page.appendLine('<div class="vp-vertical-line"></div>');
page.appendFormatLine('<button class="{0} {1}">{2}</button>', 'vp-button', VP_PF_IMPORT_BTN, 'Import');
page.appendLine('</div>');
page.appendLine('</div>');
// page.appendLine('<hr style="margin:0px"/>');
// show box
page.appendFormatLine('<div class="{0}">', VP_PF_SHOW_BOX);
// Select DataFrame
page.appendFormatLine('<div class="{0}">', VP_PF_DF_BOX);
page.appendFormatLine('<label for="{0}" class="{1}">{2}</label>', 'vp_pfVariable', 'vp-orange-text', 'DataFrame');
page.appendLine('<div>');
page.appendFormatLine('<select id="{0}"></select>', 'vp_pfVariable');
page.appendFormatLine('<div class="{0}" title="{1}"><img src="{2}"/></div>', VP_PF_DF_REFRESH, "Refresh variable list", '/nbextensions/visualpython/resource/refresh.svg');
page.appendLine('</div>');
page.appendFormatLine('<label for="{0}" class="{1}">{2}</label>', 'vp_pfReturn', 'vp-orange-text', 'Allocate to');
page.appendFormatLine('<input type="text" id="{0}" class="{1}" placeholder="{2}"/>', 'vp_pfReturn', 'vp-pf-input', 'New variable name');
page.appendFormatLine('<label for="{0}">{1}</label>', 'vp_pfTitle', 'Report Title');
page.appendLine('<div>');
page.appendFormatLine('<input type="text" id="{0}" class="{1}" placeholder="{2}"/>', 'vp_pfTitle', 'vp-pf-input', 'Title name');
// Generate Report
page.appendFormatLine('<button class="{0} {1}" data-type="{2}">{3}</button>'
, 'vp-button activated', VP_PF_MENU_ITEM, PROFILE_TYPE.GENERATE, 'Generate Report');
page.appendLine('</div>');
// hidden inputs
page.appendFormatLine('<input type="hidden" class="{0}"/>', VP_PF_FILENAME);
page.appendFormatLine('<input type="hidden" class="{0}"/>', VP_PF_FILEPATH);
page.appendLine('</div>'); // VP_PF_DF_BOX
// // button box
// page.appendLine('<div>');
// // Show Report
// page.appendFormatLine('<button class="{0} {1}" data-type="{2}">{3}</button>'
// , 'vp-button', VP_PF_MENU_ITEM, PROFILE_TYPE.SHOW, 'Show Report');
// // Save Report
// page.appendFormatLine('<button class="{0} {1}" data-type="{2}">{3}</button>'
// , 'vp-button', VP_PF_MENU_ITEM, PROFILE_TYPE.SAVE, 'Save Report');
// page.appendLine('</div>');
page.appendLine('</div>'); // VP_PF_SHOW_BOX
// list box
page.appendLine(this.renderReportList());
page.appendLine('</div>'); // VP_PF_GRID_BOX
page.appendLine('</div>'); // VP_PF_BODY
page.appendLine('</div>'); // VP_PF_CONTAINER
page.appendLine('</div>'); // VP_PF
$('#vp-wrapper').append(page.toString());
$(this.wrapSelector()).hide();
}
Profiling.prototype.loadVariableList = function() {
var that = this;
// load using kernel
var dataTypes = ['DataFrame'];
kernelApi.searchVarList(dataTypes, function(result) {
try {
var varList = JSON.parse(result);
// render variable list
// replace
$(that.wrapSelector('#vp_pfVariable')).replaceWith(function() {
return that.renderVariableList(varList);
});
$(that.wrapSelector('#vp_pfVariable')).trigger('change');
} catch (ex) {
console.log('Profiling:', result);
// console.log(ex);
}
});
}
Profiling.prototype.renderVariableList = function(varList) {
var tag = new sb.StringBuilder();
var beforeValue = $(this.wrapSelector('#vp_pfVariable')).val();
tag.appendFormatLine('<select id="{0}">', 'vp_pfVariable');
varList.forEach(vObj => {
// varName, varType
var label = vObj.varName;
tag.appendFormatLine('<option value="{0}" data-type="{1}" {2}>{3}</option>'
, vObj.varName, vObj.varType
, beforeValue == vObj.varName?'selected':''
, label);
});
tag.appendLine('</select>'); // VP_VS_VARIABLES
return tag.toString();
}
Profiling.prototype.checkInstalled = function() {
var that = this;
// set state as 'Checking'
$(this.wrapSelector('.' + VP_PF_INSTALL_BTN)).text('Checking...');
// set disabled
if (!$(that.wrapSelector('.' + VP_PF_INSTALL_BTN)).hasClass('disabled')) {
$(that.wrapSelector('.' + VP_PF_INSTALL_BTN)).addClass('disabled');
}
var checking = true;
// check installed
Jupyter.notebook.kernel.execute(
'!pip show pandas-profiling',
{
iopub: {
output: function(msg) {
if (!checking) {
return;
}
if (msg.content['name'] == 'stderr' || msg.content['text'].includes('not found')) {
$(that.wrapSelector('.' + VP_PF_INSTALL_BTN)).text('Install');
// set enabled
if ($(that.wrapSelector('.' + VP_PF_INSTALL_BTN)).hasClass('disabled')) {
$(that.wrapSelector('.' + VP_PF_INSTALL_BTN)).removeClass('disabled');
}
} else {
$(that.wrapSelector('.' + VP_PF_INSTALL_BTN)).text('Installed');
// set disabled
if (!$(that.wrapSelector('.' + VP_PF_INSTALL_BTN)).hasClass('disabled')) {
$(that.wrapSelector('.' + VP_PF_INSTALL_BTN)).addClass('disabled');
}
}
}
}
}
);
}
Profiling.prototype.loadReportList = function() {
var that = this;
// load using kernel
kernelApi.getProfilingList(function(result) {
try {
var varList = JSON.parse(result);
// render variable list
// replace
$(that.wrapSelector('.' + VP_PF_LIST_BOX)).replaceWith(function() {
return that.renderReportList(varList);
});
} catch (ex) {
console.log('Profiling:', result);
// console.log(ex);
}
});
}
Profiling.prototype.renderReportList = function(reportList=[]) {
var page = new sb.StringBuilder();
page.appendFormatLine('<div class="{0}">', VP_PF_LIST_BOX);
page.appendFormatLine('<div class="{0}">', VP_PF_LIST_HEADER);
page.appendFormatLine('<div><label class="{0}">{1}</label></div>', VP_PF_LIST_HEADER_ITEM, 'Allocated to');
page.appendFormatLine('<div><label class="{0}">{1}</label></div>', VP_PF_LIST_HEADER_ITEM, 'Report Title');
page.appendFormatLine('<div><label class="{0}">{1}</label></div>', VP_PF_LIST_HEADER_ITEM, '');
page.appendLine('</div>');
page.appendFormatLine('<div class="{0}">', 'vp-apiblock-scrollbar');
page.appendFormatLine('<div class="{0} {1}">', VP_PF_LIST_BODY, 'vp-apiblock-scrollbar');
reportList.forEach((report, idx) => {
var { varName, title } = report;
page.appendFormatLine('<div class="{0}" data-name="{1}" data-title="{2}">', VP_PF_LIST_ITEM, varName, title);
page.appendFormatLine('<div>{0}</div>', varName);
page.appendFormatLine('<div>{0}</div>', title);
// button box
page.appendFormatLine('<div class="{0}">', VP_PF_LIST_BUTTON_BOX);
page.appendFormatLine('<div class="{0}" data-menu="{1}" title="{2}"><img src="{3}"/></div>'
, VP_PF_LIST_MENU_ITEM, LIST_MENU_ITEM.SHOW, 'Show report', '/nbextensions/visualpython/resource/snippets/run.svg');
page.appendFormatLine('<div class="{0}" data-menu="{1}" title="{2}"><img src="{3}"/></div>'
, VP_PF_LIST_MENU_ITEM, LIST_MENU_ITEM.DELETE, 'Delete report', '/nbextensions/visualpython/resource/delete.svg');
page.appendFormatLine('<div class="{0}" data-menu="{1}" title="{2}"><img src="{3}"/></div>'
, VP_PF_LIST_MENU_ITEM, LIST_MENU_ITEM.SAVE, 'Save report', '/nbextensions/visualpython/resource/snippets/export.svg');
page.appendLine('</div>');
page.appendLine('</div>');
});
page.appendLine('</div>'); // VP_PF_LIST_BODY
page.appendLine('</div>');
page.appendLine('</div>'); // VP_PF_LIST_BOX
return page.toString();
}
Profiling.prototype.unbindEvent = function() {
$(document).off(this.wrapSelector('*'));
$(document).off('click', this.wrapSelector('.' + VP_PF_CLOSE));
$(document).off('click', this.wrapSelector('.' + VP_PF_INSTALL_BTN));
$(document).off('click', this.wrapSelector('.' + VP_PF_CHECK_BTN));
$(document).off('click', this.wrapSelector('.' + VP_PF_IMPORT_BTN));
$(document).off('click', this.wrapSelector('.vp-pf-df-refresh'));
$(document).off('click', this.wrapSelector('.' + VP_PF_MENU_ITEM));
$(document).off('click', this.wrapSelector('.' + VP_PF_LIST_MENU_ITEM));
$(document).off('snippetSaved.fileNavigation', this.wrapSelector('.' + VP_PF_FILEPATH));
$(document).off('keydown.' + this.uuid);
$(document).off('keyup.' + this.uuid);
}
Profiling.prototype.bindEvent = function() {
var that = this;
// close popup
$(document).on('click', this.wrapSelector('.' + VP_PF_CLOSE), function(event) {
that.close();
// vpCommon.removeHeadScript("vpSubsetEditor");
});
// click install
$(document).on('click', this.wrapSelector('.' + VP_PF_INSTALL_BTN + ':not(.disabled)'), function(event) {
vpCommon.cellExecute([{command: '!pip install pandas-profiling', exec:true, type:'code'}]);
});
// click check installed
$(document).on('click', this.wrapSelector('.' + VP_PF_CHECK_BTN), function() {
that.checkInstalled();
});
// click import
$(document).on('click', this.wrapSelector('.' + VP_PF_IMPORT_BTN), function(event) {
vpCommon.cellExecute([{command: 'from pandas_profiling import ProfileReport', exec:true, type:'code'}]);
});
// refresh df
$(document).on('click', this.wrapSelector('.vp-pf-df-refresh'), function() {
that.loadVariableList();
});
// click menu
$(document).on('click', this.wrapSelector('.' + VP_PF_MENU_ITEM), function() {
var type = $(this).data('type');
var df = $(that.wrapSelector('#vp_pfVariable')).val();
var saveas = $(that.wrapSelector('#vp_pfReturn')).val();
if (saveas == '') {
saveas = '_vp_profile';
}
var title = $(that.wrapSelector('#vp_pfTitle')).val();
var code = new sb.StringBuilder();
switch(parseInt(type)) {
case PROFILE_TYPE.GENERATE:
code.appendFormatLine("{0} = ProfileReport({1}, title='{2}')", saveas, df, title);
code.append(saveas);
break;
}
vpCommon.cellExecute([{command: code.toString(), exec:true, type:'code'}]);
that.loadReportList();
});
// click list item menu
$(document).on('click', this.wrapSelector('.' + VP_PF_LIST_MENU_ITEM), async function(evt) {
var menu = $(this).data('menu');
var itemTag = $(this).closest('.' + VP_PF_LIST_ITEM);
var varName = $(itemTag).data('name');
var title = $(itemTag).data('title');
var code = new sb.StringBuilder();
switch(menu) {
case LIST_MENU_ITEM.SHOW:
code.appendFormat("{0}.to_notebook_iframe()", varName);
break;
case LIST_MENU_ITEM.DELETE:
code.appendFormat("del {0}", varName);
break;
case LIST_MENU_ITEM.SAVE:
var loadURLstyle = Jupyter.notebook.base_url + vpConst.BASE_PATH + vpConst.STYLE_PATH;
var loadURLhtml = Jupyter.notebook.base_url + vpConst.BASE_PATH + vpConst.SOURCE_PATH + "component/fileNavigation/index.html";
vpCommon.loadCss( loadURLstyle + "component/fileNavigation.css");
await $(`<div id="vp_fileNavigation"></div>`)
.load(loadURLhtml, () => {
$('#vp_fileNavigation').removeClass("hide");
$('#vp_fileNavigation').addClass("show");
that.state.selectedReport = varName;
var { vp_init
, vp_bindEventFunctions } = fileNavigation;
that.state.visualpythonFileName = title;
fileNavigation.vp_init(that, "SAVE_SNIPPETS");
// fileNavigation.vp_init(that.getStateAll());
fileNavigation.vp_bindEventFunctions();
})
.appendTo("#site");
return;
default:
return;
}
vpCommon.cellExecute([{command: code.toString(), exec:true, type:'code'}]);
that.loadReportList();
});
// save file complete event
$(document).on('snippetSaved.fileNavigation', this.wrapSelector('.' + VP_PF_FILEPATH), function(evt) {
var fileName = evt.file;
var path = evt.path;
var varName = that.state.selectedReport;
if (varName == '') {
varName = '_vp_profile';
}
var code = new sb.StringBuilder();
code.appendFormat("{0}.to_file('{1}')", varName, path);
vpCommon.cellExecute([{command: code.toString(), exec:true, type:'code'}]);
that.selectedReport = '';
});
this.keyboardManager = {
keyCode : {
ctrlKey: 17,
cmdKey: 91,
shiftKey: 16,
altKey: 18,
enter: 13,
escKey: 27
},
keyCheck : {
ctrlKey: false,
shiftKey: false
}
}
$(document).on('keydown.' + this.uuid, function(e) {
var keyCode = that.keyboardManager.keyCode;
if (e.keyCode == keyCode.ctrlKey || e.keyCode == keyCode.cmdKey) {
that.keyboardManager.keyCheck.ctrlKey = true;
}
if (e.keyCode == keyCode.shiftKey) {
that.keyboardManager.keyCheck.shiftKey = true;
}
}).on('keyup.' + this.uuid, function(e) {
var keyCode = that.keyboardManager.keyCode;
if (e.keyCode == keyCode.ctrlKey || e.keyCode == keyCode.cmdKey) {
that.keyboardManager.keyCheck.ctrlKey = false;
}
if (e.keyCode == keyCode.shiftKey) {
that.keyboardManager.keyCheck.shiftKey = false;
}
if (e.keyCode == keyCode.escKey) {
// close on esc
that.close();
}
});
};
return Profiling;
});