13
13
// [CLASS] Import
14
14
//============================================================================
15
15
define ( [
16
+ 'css!vp_base/css/m_apps/import.css' ,
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 ( importCss , com_util , com_Const , com_String , PopupComponent ) {
22
+
23
+ const importTemplates = {
24
+ 'pre-processing' : [
25
+ { i0 : 'numpy' , i1 : 'np' , type : 'module' } ,
26
+ { i0 : 'pandas' , i1 : 'pd' , type : 'module' } ,
27
+ {
28
+ i0 : 'matplotlib.pyplot' , i1 : 'plt' , type : 'module'
29
+ , include : [
30
+ '%matplotlib inline'
31
+ ]
32
+ } ,
33
+ { i0 : 'seaborn' , i1 : 'sns' , type : 'module' }
34
+ ] ,
35
+ 'machine-learning' : [
36
+ { i0 : 'sklearn.model_selection' , i1 : 'train_test_split' , type : 'function' } ,
37
+ { i0 : 'sklearn' , i1 : 'metrics' , type : 'function' }
38
+ ]
39
+ }
21
40
22
41
/**
23
42
* Import
@@ -29,98 +48,205 @@ define([
29
48
this . config . dataview = false ;
30
49
this . config . sizeLevel = 1 ;
31
50
32
- this . packageList = [
33
- { library : 'numpy' , alias :'np' }
34
- , { library : 'pandas' , alias :'pd' }
35
- , {
36
- library : 'matplotlib.pyplot' , alias :'plt'
37
- , include : [
38
- '%matplotlib inline'
39
- ]
40
- }
41
- , { library : 'seaborn' , alias :'sns' }
42
- ] ;
51
+ let savedData = vpConfig . getDataSimple ( '' , 'vpimport' ) ;
52
+ // Reset abnormal data
53
+ if ( savedData . tabType == undefined ) {
54
+ savedData = { } ;
55
+ vpConfig . setData ( null , 'vpimport' ) ;
56
+ }
43
57
44
58
this . state = {
45
- importMeta : vpConfig . getDataSimple ( '' , 'vpimport' ) ,
59
+ tabType : 'pre-processing' ,
60
+ importMeta : [ ] ,
61
+ ...savedData ,
46
62
...this . state
47
63
}
48
64
49
65
if ( ! this . state . importMeta || this . state . importMeta . length <= 0 ) {
50
- this . state . importMeta = this . packageList ;
66
+ this . state . importMeta = JSON . parse ( JSON . stringify ( importTemplates [ this . state . tabType ] ) ) ;
51
67
}
52
68
}
53
69
54
70
_bindEvent ( ) {
55
71
super . _bindEvent ( ) ;
56
- /** Implement binding events */
57
72
let that = this ;
73
+
74
+ // select tab
75
+ $ ( this . wrapSelector ( '.vp-tab-button' ) ) . on ( 'click' , function ( ) {
76
+ let tabType = $ ( this ) . data ( 'tab' ) ;
77
+ // set button selected
78
+ that . state . tabType = tabType ;
79
+ $ ( that . wrapSelector ( '.vp-tab-button' ) ) . removeClass ( 'vp-tab-selected' ) ;
80
+ $ ( this ) . addClass ( 'vp-tab-selected' ) ;
81
+ // replace libraries
82
+ that . state . importMeta = importTemplates [ tabType ] ;
83
+ $ ( that . wrapSelector ( '#vp_tblImport' ) ) . replaceWith ( function ( ) {
84
+ return that . templateTable ( that . state . importMeta ) ;
85
+ } ) ;
86
+ } ) ;
87
+
58
88
// delete lib
59
- $ ( document ) . on ( "click" , this . wrapSelector ( '.vp-remove-option' ) , function ( ) {
89
+ $ ( this . wrapSelector ( ) ) . on ( "click" , '.vp-remove-option' , function ( ) {
60
90
$ ( this ) . closest ( 'tr' ) . remove ( ) ;
91
+
92
+ that . checkAll ( ) ;
93
+ } ) ;
94
+ // check/uncheck all
95
+ $ ( this . wrapSelector ( ) ) . on ( 'change' , '#vp_libraryCheckAll' , function ( ) {
96
+ var checked = $ ( this ) . prop ( 'checked' ) ;
97
+ $ ( that . wrapSelector ( '.vp-item-check' ) ) . prop ( 'checked' , checked ) ;
98
+ } ) ;
99
+ // check item
100
+ $ ( this . wrapSelector ( ) ) . on ( 'change' , '.vp-item-check' , function ( ) {
101
+ var checked = $ ( this ) . prop ( 'checked' ) ;
102
+ // if unchecked at least one item, uncheck check-all
103
+ if ( ! checked ) {
104
+ $ ( that . wrapSelector ( '.vp-check-all' ) ) . prop ( 'checked' , false ) ;
105
+ } else {
106
+ // if all checked, check check-all
107
+ that . checkAll ( ) ;
108
+ }
61
109
} ) ;
62
110
63
- // add lib
64
- $ ( this . wrapSelector ( '#vp_addLibrary' ) ) . click ( function ( ) {
111
+ // add module
112
+ $ ( this . wrapSelector ( '#vp_addModule' ) ) . click ( function ( ) {
113
+ var libsLength = $ ( that . wrapSelector ( "#vp_tblImport tbody tr" ) ) . length ;
114
+ var tagTr = $ ( that . templateForModule ( libsLength , '' , '' ) ) ;
115
+
116
+ $ ( that . wrapSelector ( "#vp_tblImport tr:last" ) ) . after ( tagTr ) ;
117
+ } ) ;
118
+ // add function
119
+ $ ( this . wrapSelector ( '#vp_addFunction' ) ) . click ( function ( ) {
65
120
var libsLength = $ ( that . wrapSelector ( "#vp_tblImport tbody tr" ) ) . length ;
66
- var tagTr = $ ( that . templateForLibrary ( libsLength , '' , '' ) ) ;
121
+ var tagTr = $ ( that . templateForFunction ( libsLength , '' , '' ) ) ;
67
122
68
123
$ ( that . wrapSelector ( "#vp_tblImport tr:last" ) ) . after ( tagTr ) ;
69
124
} ) ;
70
125
}
71
126
127
+ checkAll ( ) {
128
+ // check if all checked
129
+ // if all checked, check check-all
130
+ var allLength = $ ( this . wrapSelector ( '.vp-item-check' ) ) . length ;
131
+ var checkedLength = $ ( this . wrapSelector ( '.vp-item-check:checked' ) ) . length ;
132
+ if ( allLength == checkedLength ) {
133
+ $ ( this . wrapSelector ( '.vp-check-all' ) ) . prop ( 'checked' , true ) ;
134
+ } else {
135
+ $ ( this . wrapSelector ( '.vp-check-all' ) ) . prop ( 'checked' , false ) ;
136
+ }
137
+ }
138
+
72
139
templateForBody ( ) {
73
140
/** Implement generating template */
74
141
var page = new com_String ( ) ;
75
- page . appendFormatLine ( '<input type="hidden" id="vp_importMeta" value="{0}"/>' , '' ) ;
76
- page . appendLine ( '<table id="vp_tblImport" class="vp-tbl-basic w90">' ) ;
77
- page . appendLine ( '<colgroup><col width="40%"/><col width="40%"/><col width="*"/></colgroup>' ) ;
142
+ // tab buttons
143
+ page . appendLine ( '<div class="vp-tab-box">' ) ;
144
+ page . appendFormatLine ( '<div class="vp-tab-button {0}" data-tab="{1}">{2}</div>'
145
+ , this . state . tabType == 'pre-processing' ?'vp-tab-selected' :'' , 'pre-processing' , 'Pre-processing' ) ;
146
+ page . appendFormatLine ( '<div class="vp-tab-button {0}" data-tab="{1}">{2}</div>'
147
+ , this . state . tabType == 'machine-learning' ?'vp-tab-selected' :'' , 'machine-learning' , 'Machine Learning' ) ;
148
+ page . appendLine ( '</div>' ) ;
149
+ // import table
150
+ page . appendLine ( this . templateTable ( this . state . importMeta ) ) ;
151
+ page . appendLine ( '<input type="button" id="vp_addModule" value="+ Module" class="vp-button" title="import...as"/>' ) ;
152
+ page . appendLine ( '<input type="button" id="vp_addFunction" value="+ Function" class="vp-button" title="from...import"/>' ) ;
153
+ return page . toString ( ) ;
154
+ }
155
+
156
+ templateTable ( libraries ) {
157
+ var page = new com_String ( ) ;
158
+ page . appendLine ( '<table id="vp_tblImport" class="vp-tbl-basic w90 vp-tbl-gap5">' ) ;
159
+ page . appendLine ( '<colgroup><col width="10px"/><col width="10%"/><col width="30%"/><col width="10%"/><col width="30%"/><col width="*"/></colgroup>' ) ;
78
160
page . appendLine ( '<thead><tr>' ) ;
79
- page . appendLine ( '<th>Library Name</th><th>Alias</th><th></th>' ) ;
161
+ page . appendFormat ( '<th><input id="{0}" type="checkbox" class="vp-checkbox vp-check-all" checked/></th>' , 'vp_libraryCheckAll' ) ;
162
+ page . appendLine ( '<th></th><th></th><th></th><th></th><th></th>' ) ;
80
163
page . appendLine ( '</tr></thead>' ) ;
81
164
page . appendLine ( '<tbody>' ) ;
82
165
let that = this ;
83
- this . state . importMeta && this . state . importMeta . forEach ( ( lib , idx ) => {
84
- page . appendLine ( that . templateForLibrary ( idx , lib . library , lib . alias ) ) ;
166
+ libraries && libraries . forEach ( ( lib , idx ) => {
167
+ if ( lib . type == 'function' ) {
168
+ page . appendLine ( that . templateForFunction ( idx , lib . i0 , lib . i1 , lib . checked ) ) ;
169
+ } else {
170
+ page . appendLine ( that . templateForModule ( idx , lib . i0 , lib . i1 , lib . checked ) ) ;
171
+ }
85
172
} ) ;
86
173
page . appendLine ( '</tbody>' ) ;
87
174
page . appendLine ( '</table>' ) ;
88
- page . appendLine ( '<input type="button" id="vp_addLibrary" value="+ Library" class="vp-button"/>' ) ;
89
175
return page . toString ( ) ;
90
176
}
91
177
92
- templateForLibrary ( idx , libraryName , aliasName ) {
178
+ templateForModule ( idx , moduleName , aliasName , checked = true ) {
93
179
var tag = new com_String ( ) ;
94
- tag . append ( '<tr>' ) ;
180
+ tag . append ( '<tr data-type="module">' ) ;
181
+ // checkbox
182
+ tag . appendFormat ( '<td><input id="{0}" type="checkbox" class="vp-checkbox vp-item-check" {1}/></td>'
183
+ , 'vp_libraryCheck' + idx , checked ?'checked' :'' ) ;
184
+ // inputs
185
+ tag . appendFormat ( '<td style="{0}">import</td>' , 'text-align="center";' ) ;
95
186
tag . appendFormat ( '<td><input id="{0}" type="text" class="{1}" placeholder="{2}" required value="{3}"/></td>'
96
- , 'vp_library' + idx , 'vp-input m vp-add-library' , 'Type library name' , libraryName ) ;
187
+ , 'vp_i0_' + idx , 'vp-input m vp-add-i0' , 'Type module' , moduleName ) ;
188
+ tag . appendFormat ( '<td style="{0}">as</td>' , 'text-align="center";' ) ;
97
189
tag . appendFormat ( '<td><input id="{0}" type="text" class="{1}" placeholder="{2}" value="{3}"/></td>'
98
- , 'vp_alias ' + idx , 'vp-input m vp-add-alias ' , 'Type alias' , aliasName ) ;
190
+ , 'vp_i1_ ' + idx , 'vp-input m vp-add-i1 ' , 'Type alias' , aliasName ) ;
99
191
tag . appendFormat ( '<td class="{0}"><img src="{1}"/></td>' , 'vp-remove-option w100 vp-cursor' , '/nbextensions/visualpython/img/close_small.svg' ) ;
100
192
tag . append ( '</tr>' ) ;
101
193
return tag . toString ( ) ;
102
194
}
103
195
196
+ templateForFunction ( idx , moduleName , functionName , checked = true ) {
197
+ var tag = new com_String ( ) ;
198
+ tag . append ( '<tr data-type="function">' ) ;
199
+ // checkbox
200
+ tag . appendFormat ( '<td><input id="{0}" type="checkbox" class="vp-checkbox vp-item-check" {1}/></td>'
201
+ , 'vp_libraryCheck' + idx , checked ?'checked' :'' ) ;
202
+ // inputs
203
+ tag . appendFormat ( '<td style="{0}">from</td>' , 'text-align="center";' ) ;
204
+ tag . appendFormat ( '<td><input id="{0}" type="text" class="{1}" placeholder="{2}" required value="{3}"/></td>'
205
+ , 'vp_i0_' + idx , 'vp-input m vp-add-i0' , 'Type module' , moduleName ) ;
206
+ tag . appendFormat ( '<td style="{0}">import</td>' , 'text-align="center";' ) ;
207
+ tag . appendFormat ( '<td><input id="{0}" type="text" class="{1}" placeholder="{2}" value="{3}"/></td>'
208
+ , 'vp_i1_' + idx , 'vp-input m vp-add-i1' , 'Type function' , functionName ) ;
209
+ tag . appendFormat ( '<td class="{0}"><img src="{1}"/></td>' , 'vp-remove-option w100 vp-cursor' , '/nbextensions/visualpython/img/close_small.svg' ) ;
210
+ tag . append ( '</tr>' ) ;
211
+ return tag . toString ( ) ;
212
+ }
213
+
214
+ render ( ) {
215
+ super . render ( ) ;
216
+
217
+ this . checkAll ( ) ;
218
+ }
219
+
104
220
generateCode ( ) {
105
221
var sbCode = new com_String ( ) ;
106
222
107
223
// code generate with library list
108
224
var importMeta = [ ] ;
109
225
var libraryList = $ ( this . wrapSelector ( "#vp_tblImport tbody tr" ) ) ;
110
226
for ( var idx = 0 ; idx < libraryList . length ; idx ++ ) {
111
- var pacName = $ ( libraryList [ idx ] ) . find ( '.vp-add-library' ) . val ( ) ;
112
- var pacAlias = $ ( libraryList [ idx ] ) . find ( '.vp-add-alias' ) . val ( ) . trim ( ) ;
227
+ var pacType = $ ( libraryList [ idx ] ) . data ( 'type' ) ;
228
+ var pacI0 = $ ( libraryList [ idx ] ) . find ( '.vp-add-i0' ) . val ( ) ;
229
+ var pacI1 = $ ( libraryList [ idx ] ) . find ( '.vp-add-i1' ) . val ( ) . trim ( ) ;
230
+ var pacChecked = $ ( libraryList [ idx ] ) . find ( '.vp-item-check' ) . prop ( 'checked' ) ;
113
231
114
- if ( pacName == "" ) {
232
+ if ( pacI0 == "" ) {
115
233
continue ;
116
234
}
117
- if ( sbCode . toString ( ) . trim ( ) . length > 0 ) {
118
- sbCode . appendLine ( ) ;
235
+ if ( pacChecked ) {
236
+ if ( sbCode . toString ( ) . trim ( ) . length > 0 ) {
237
+ sbCode . appendLine ( ) ;
238
+ }
239
+ if ( pacType == 'function' ) {
240
+ // function
241
+ sbCode . appendFormat ( "from {0} import {1}" , pacI0 , pacI1 ) ;
242
+ } else {
243
+ // module
244
+ sbCode . appendFormat ( "import {0}{1}" , pacI0 , ( ( pacI1 === undefined || pacI1 === "" ) ? "" : ( " as " + pacI1 ) ) ) ;
245
+ }
119
246
}
120
- sbCode . appendFormat ( "import {0}{1}" , pacName , ( ( pacAlias === undefined || pacAlias === "" ) ? "" : ( " as " + pacAlias ) ) ) ;
121
247
122
- this . packageList . forEach ( pack => {
123
- if ( pack . library == pacName ) {
248
+ this . state . importMeta && this . state . importMeta . forEach ( pack => {
249
+ if ( pack . i0 == pacI0 ) {
124
250
// if include code exists?
125
251
if ( pack . include != undefined ) {
126
252
pack . include . forEach ( code => {
@@ -131,12 +257,12 @@ define([
131
257
}
132
258
} )
133
259
134
- importMeta . push ( { library : pacName , alias : pacAlias } ) ;
260
+ importMeta . push ( { i0 : pacI0 , i1 : pacI1 , type : pacType , checked : pacChecked } ) ;
135
261
}
136
262
this . state . importMeta = importMeta ;
137
263
138
264
// save import packages
139
- vpConfig . setData ( importMeta , 'vpimport' ) ;
265
+ vpConfig . setData ( { tabType : this . state . tabType , importMeta : importMeta } , 'vpimport' ) ;
140
266
141
267
this . generatedCode = sbCode . toString ( ) ;
142
268
return sbCode . toString ( ) ;
0 commit comments