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
354 lines (298 loc) · 14.2 KB
/
Copy pathvpProfiling.js
File metadata and controls
354 lines (298 loc) · 14.2 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
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_FILEPATH = 'vp-pf-filepath';
const VP_PF_FILENAME = 'vp-pf-filename';
const VP_PF_BUTTON_BOX = 'vp-pf-btn-box';
const VP_PF_BUTTON_CANCEL = 'vp-pf-btn-cancel';
const VP_PF_BUTTON_APPLY = 'vp-pf-btn-apply';
const PROFILE_TYPE = {
NONE: -1,
GENERATE: 1,
SHOW: 2,
SAVE: 3
}
/**
* @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"]
};
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();
}
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}"><i class="{1}"></i></div>'
, VP_PF_CLOSE, 'fa fa-close');
// 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('<i class="{0} {1} {2}" title="{2}"></i>', 'fa fa-refresh', 'vp-cursor', VP_PF_CHECK_BTN, 'Check if installed');
// 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('<i class="{0} {1}" title="{2}"></i>', VP_PF_DF_REFRESH, 'fa fa-refresh', "Refresh variable list");
page.appendLine('</div>');
page.appendFormatLine('<label for="{0}">{1}</label>', 'vp_pfReturn', 'Allocate to');
page.appendFormatLine('<input type="text" id="{0}" class="{1}" placeholder="{2}"/>', 'vp_pfReturn', 'vp-pf-input', 'variable name');
page.appendFormatLine('<label for="{0}">{1}</label>', 'vp_pfTitle', 'Report Title');
page.appendFormatLine('<input type="text" id="{0}" class="{1}" placeholder="{2}"/>', 'vp_pfTitle', 'vp-pf-input', 'title name');
page.appendLine('</div>');
// button box
page.appendLine('<div>');
// 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');
// 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
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('FrameEditor:', 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') {
if (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.unbindEvent = function() {
$(document).off(this.wrapSelector('*'));
$(document).off('click', this.wrapSelector('.' + VP_PF_CLOSE));
$(document).off('change', this.wrapSelector('#vp_pfVariable'));
$(document).off('click', this.wrapSelector('.vp-pf-df-refresh'));
$(document).off('click', this.wrapSelector('.' + VP_PF_BUTTON_CANCEL));
$(document).off('click', this.wrapSelector('.' + VP_PF_BUTTON_APPLY));
}
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), 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), async 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.appendFormat("{0} = ProfileReport({1}, title='{2}')", saveas, df, title);
break;
case PROFILE_TYPE.SHOW:
code.appendFormat("{0}.to_notebook_iframe()", saveas);
break;
case PROFILE_TYPE.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");
var { vp_init
, vp_bindEventFunctions } = fileNavigation;
fileNavigation.vp_init(that, "SAVE_SNIPPETS");
// fileNavigation.vp_init(that.getStateAll());
fileNavigation.vp_bindEventFunctions();
})
.appendTo("#site");
return;
}
vpCommon.cellExecute([{command: code.toString(), exec:true, type:'code'}]);
});
// save file complete event
$(document).on('snippetSaved.fileNavigation', this.wrapSelector('.' + VP_PF_FILEPATH), function(evt) {
var fileName = evt.file;
var path = evt.path;
var saveas = $(that.wrapSelector('#vp_pfReturn')).val();
if (saveas == '') {
saveas = '_vp_profile';
}
var code = new sb.StringBuilder();
code.appendFormat("{0}.to_file('{1}')", saveas, path);
vpCommon.cellExecute([{command: code.toString(), exec:true, type:'code'}]);
});
};
return Profiling;
});