|
13 | 13 | // [CLASS] Add data
|
14 | 14 | //============================================================================
|
15 | 15 | define([
|
| 16 | + 'text!vp_base/html/m_ml/addData.html!strip', |
16 | 17 | 'vp_base/js/com/com_util',
|
17 | 18 | 'vp_base/js/com/com_Const',
|
18 | 19 | 'vp_base/js/com/com_String',
|
19 | 20 | 'vp_base/js/com/component/PopupComponent'
|
20 |
| -], function(com_util, com_Const, com_String, PopupComponent) { |
| 21 | +], function(addDataHtml, com_util, com_Const, com_String, PopupComponent) { |
21 | 22 |
|
22 | 23 | /**
|
23 | 24 | * Add data
|
24 | 25 | */
|
25 | 26 | class AddData extends PopupComponent {
|
26 | 27 | _init() {
|
27 | 28 | super._init();
|
28 |
| - /** Write codes executed before rendering */ |
| 29 | + |
| 30 | + this.state = { |
| 31 | + targetVariable: '', |
| 32 | + predictData: 'pred', |
| 33 | + colName: 'pred_result', |
| 34 | + allocateTo: '_vp', |
| 35 | + ...this.state |
| 36 | + } |
29 | 37 | }
|
30 | 38 |
|
31 | 39 | _bindEvent() {
|
32 | 40 | super._bindEvent();
|
33 |
| - /** Implement binding events */ |
34 | 41 | var that = this;
|
35 |
| - this.$target.on('click', function(evt) { |
36 |
| - var target = evt.target; |
37 |
| - if ($(that.wrapSelector()).find(target).length > 0) { |
38 |
| - // Sample : getDataList from Kernel |
39 |
| - vpKernel.getDataList().then(function(resultObj) { |
40 |
| - vpLog.display(VP_LOG_TYPE.DEVELOP, resultObj); |
41 |
| - }).catch(function(err) { |
42 |
| - vpLog.display(VP_LOG_TYPE.DEVELOP, err); |
43 |
| - }); |
| 42 | + |
| 43 | + } |
| 44 | + |
| 45 | + templateForDataView() { |
| 46 | + ; |
| 47 | + } |
| 48 | + |
| 49 | + renderDataView() { |
| 50 | + super.renderDataView(); |
| 51 | + |
| 52 | + this.loadDataPage(); |
| 53 | + $(this.wrapSelector('.vp-popup-dataview-box')).css('height', '300px'); |
| 54 | + } |
| 55 | + |
| 56 | + renderDataPage(renderedText, isHtml = true) { |
| 57 | + var tag = new com_String(); |
| 58 | + tag.appendFormatLine('<div class="{0} vp-close-on-blur vp-scrollbar">', 'rendered_html'); // 'rendered_html' style from jupyter output area |
| 59 | + if (isHtml) { |
| 60 | + tag.appendLine(renderedText); |
| 61 | + } else { |
| 62 | + tag.appendFormatLine('<pre>{0}</pre>', renderedText); |
| 63 | + } |
| 64 | + tag.appendLine('</div>'); |
| 65 | + $(this.wrapSelector('.vp-popup-dataview-box')).html(tag.toString()); |
| 66 | + } |
| 67 | + |
| 68 | + loadDataPage() { |
| 69 | + var that = this; |
| 70 | + var code = this.generateCode(); |
| 71 | + // if not, get output of all data in selected pandasObject |
| 72 | + vpKernel.execute(code).then(function(resultObj) { |
| 73 | + let { msg } = resultObj; |
| 74 | + if (msg.content.data) { |
| 75 | + var htmlText = String(msg.content.data["text/html"]); |
| 76 | + var codeText = String(msg.content.data["text/plain"]); |
| 77 | + if (htmlText != 'undefined') { |
| 78 | + that.renderDataPage(htmlText); |
| 79 | + } else if (codeText != 'undefined') { |
| 80 | + // plain text as code |
| 81 | + that.renderDataPage(codeText, false); |
| 82 | + } else { |
| 83 | + that.renderDataPage(''); |
| 84 | + } |
| 85 | + } else { |
| 86 | + var errorContent = new com_String(); |
| 87 | + if (msg.content.ename) { |
| 88 | + errorContent.appendFormatLine('<div class="{0}">', 'vp-popup-data-error-box'); |
| 89 | + errorContent.appendLine('<i class="fa fa-exclamation-triangle"></i>'); |
| 90 | + errorContent.appendFormatLine('<label class="{0}">{1}</label>', |
| 91 | + 'vp-popup-data-error-box-title', msg.content.ename); |
| 92 | + if (msg.content.evalue) { |
| 93 | + // errorContent.appendLine('<br/>'); |
| 94 | + errorContent.appendFormatLine('<pre>{0}</pre>', msg.content.evalue.split('\\n').join('<br/>')); |
| 95 | + } |
| 96 | + errorContent.appendLine('</div>'); |
| 97 | + } |
| 98 | + that.renderDataPage(errorContent); |
44 | 99 | }
|
| 100 | + }).catch(function(resultObj) { |
| 101 | + let { msg } = resultObj; |
| 102 | + var errorContent = new com_String(); |
| 103 | + if (msg.content.ename) { |
| 104 | + errorContent.appendFormatLine('<div class="{0}">', 'vp-popup-data-error-box'); |
| 105 | + errorContent.appendLine('<i class="fa fa-exclamation-triangle"></i>'); |
| 106 | + errorContent.appendFormatLine('<label class="{0}">{1}</label>', |
| 107 | + 'vp-popup-data-error-box-title', msg.content.ename); |
| 108 | + if (msg.content.evalue) { |
| 109 | + // errorContent.appendLine('<br/>'); |
| 110 | + errorContent.appendFormatLine('<pre>{0}</pre>', msg.content.evalue.split('\\n').join('<br/>')); |
| 111 | + } |
| 112 | + errorContent.appendLine('</div>'); |
| 113 | + } |
| 114 | + that.renderDataPage(errorContent); |
45 | 115 | });
|
46 | 116 | }
|
47 | 117 |
|
48 | 118 | templateForBody() {
|
49 |
| - /** Implement generating template */ |
50 |
| - return 'This is sample.'; |
| 119 | + let page = $(addDataHtml); |
| 120 | + |
| 121 | + |
| 122 | + return page; |
51 | 123 | }
|
52 | 124 |
|
53 | 125 | generateCode() {
|
54 |
| - return "print('sample code')"; |
| 126 | + let { targetVariable, predictData, colName, allocateTo } = this.state; |
| 127 | + let code = new com_String(); |
| 128 | + |
| 129 | + if (targetVariable != '' && targetVariable != allocateTo) { |
| 130 | + code.appendFormatLine('{0} = {1}.copy()', allocateTo, targetVariable); |
| 131 | + } |
| 132 | + code.appendFormat("{0}['{1}'] = {2}", allocateTo, colName, predictData); |
| 133 | + if (allocateTo && allocateTo != '') { |
| 134 | + code.appendLine(); |
| 135 | + code.append(allocateTo); |
| 136 | + } |
| 137 | + return code.toString(); |
55 | 138 | }
|
56 | 139 |
|
57 | 140 | }
|
|
0 commit comments