-
-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathImport.js
More file actions
150 lines (131 loc) · 5.83 KB
/
Copy pathImport.js
File metadata and controls
150 lines (131 loc) · 5.83 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
/*
* Project Name : Visual Python
* Description : GUI-based Python code generator
* File Name : Import.js
* Author : Black Logic
* Note : Apps > Import
* License : GNU GPLv3 with Visual Python special exception
* Date : 2021. 11. 18
* Change Date :
*/
//============================================================================
// [CLASS] Import
//============================================================================
define([
'vp_base/js/com/com_util',
'vp_base/js/com/com_Const',
'vp_base/js/com/com_String',
'vp_base/js/com/component/PopupComponent'
], function(com_util, com_Const, com_String, PopupComponent) {
/**
* Import
*/
class Import extends PopupComponent {
_init() {
super._init();
/** Write codes executed before rendering */
this.config.dataview = false;
this.config.sizeLevel = 1;
this.packageList = [
{ library: 'numpy', alias:'np'}
, { library: 'pandas', alias:'pd'}
, {
library: 'matplotlib.pyplot', alias:'plt'
, include: [
'%matplotlib inline'
]
}
, { library: 'seaborn', alias:'sns'}
];
this.state = {
importMeta: vpConfig.getDataSimple('', 'vpimport'),
...this.state
}
if (!this.state.importMeta || this.state.importMeta.length <= 0) {
this.state.importMeta = this.packageList;
}
}
_bindEvent() {
super._bindEvent();
/** Implement binding events */
let that = this;
// delete lib
$(document).on("click", this.wrapSelector('.vp-remove-option'), function() {
// X 아이콘과 동일한 행 삭제
$(this).closest('tr').remove();
});
// add lib
$(this.wrapSelector('#vp_addLibrary')).click(function() {
var libsLength = $(that.wrapSelector("#vp_tblImport tbody tr")).length;
var tagTr = $(that.templateForLibrary(libsLength, '', ''));
$(that.wrapSelector("#vp_tblImport tr:last")).after(tagTr);
});
}
templateForBody() {
/** Implement generating template */
var page = new com_String();
page.appendFormatLine('<input type="hidden" id="vp_importMeta" value="{0}"/>', '');
page.appendLine('<table id="vp_tblImport" class="vp-tbl-basic w90">');
page.appendLine('<colgroup><col width="40%"/><col width="40%"/><col width="*"/></colgroup>');
page.appendLine('<thead><tr>');
page.appendLine('<th>Library Name</th><th>Alias</th><th></th>');
page.appendLine('</tr></thead>');
page.appendLine('<tbody>');
let that = this;
this.state.importMeta.forEach((lib, idx) => {
page.appendLine(that.templateForLibrary(idx, lib.library, lib.alias));
});
page.appendLine('</tbody>');
page.appendLine('</table>');
page.appendLine('<input type="button" id="vp_addLibrary" value="+ Library" class="vp-button"/>');
return page.toString();
}
templateForLibrary(idx, libraryName, aliasName) {
var tag = new com_String();
tag.append('<tr>');
tag.appendFormat('<td><input id="{0}" type="text" class="{1}" placeholder="{2}" required value="{3}"/></td>'
, 'vp_library' + idx, 'vp-input m vp-add-library', 'Type library name', libraryName);
tag.appendFormat('<td><input id="{0}" type="text" class="{1}" placeholder="{2}" value="{3}"/></td>'
, 'vp_alias' + idx, 'vp-input m vp-add-alias', 'Type alias', aliasName);
tag.appendFormat('<td class="{0}"><img src="{1}"/></td>', 'vp-remove-option w100 vp-cursor', '/nbextensions/visualpython/img/close_small.svg');
tag.append('</tr>');
return tag.toString();
}
generateCode() {
var sbCode = new com_String();
// code generate with library list
var importMeta = [];
var libraryList = $(this.wrapSelector("#vp_tblImport tbody tr"));
for (var idx = 0; idx < libraryList.length; idx++) {
var pacName = $(libraryList[idx]).find('.vp-add-library').val();
var pacAlias = $(libraryList[idx]).find('.vp-add-alias').val().trim();
if (pacName == "") {
continue;
}
if (sbCode.toString().trim().length > 0) {
sbCode.appendLine();
}
sbCode.appendFormat("import {0}{1}", pacName, ((pacAlias === undefined || pacAlias === "") ? "" : (" as " + pacAlias)));
this.packageList.forEach(pack => {
if (pack.library == pacName) {
// if include code exists?
if (pack.include != undefined) {
pack.include.forEach(code => {
sbCode.appendLine();
sbCode.append(code);
});
}
}
})
importMeta.push({ library: pacName, alias: pacAlias });
}
this.state.importMeta = importMeta;
// save import packages
vpConfig.setData(importMeta, 'vpimport');
// TODO: 전체에게 해당 함수 리턴 요청
this.generatedCode = sbCode.toString();
return sbCode.toString();
}
}
return Import;
});