@@ -203,8 +203,44 @@ define([
203
203
} else if ( menu == 'default-snippets' ) {
204
204
// import default snippets
205
205
var defaultSnippets = {
206
- 'default import' : 'import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport seaborn as sns'
206
+ 'default import' : [
207
+ 'import numpy as np' ,
208
+ 'import pandas as pd' ,
209
+ 'import matplotlib.pyplot as plt' ,
210
+ '%matplotlib inline' ,
211
+ 'import seaborn as sns' ,
212
+ 'import plotly.express as px'
213
+ ] ,
214
+ 'as_float' : [
215
+ 'def as_float(x):' ,
216
+ ' """' ,
217
+ " usage: df['col'] = df['col'].apply(as_float)" ,
218
+ ' """' ,
219
+ ' if not isinstance(x, str):' ,
220
+ ' return 0.0' ,
221
+ ' else:' ,
222
+ ' try:' ,
223
+ ' result = float(x)' ,
224
+ ' return result' ,
225
+ ' except ValueError:' ,
226
+ ' return 0.0'
227
+ ] ,
228
+ 'as_int' : [
229
+ 'def as_int(x):' ,
230
+ ' """' ,
231
+ " usage: df['col'] = df['col'].apply(as_int)" ,
232
+ ' """' ,
233
+ ' if not isinstance(x, str):' ,
234
+ ' return 0' ,
235
+ ' else:' ,
236
+ ' try:' ,
237
+ ' result = int(x)' ,
238
+ ' return result' ,
239
+ ' except ValueError:' ,
240
+ ' return 0.0'
241
+ ]
207
242
}
243
+
208
244
var timestamp = new Date ( ) . getTime ( ) ;
209
245
210
246
var keys = Object . keys ( defaultSnippets ) ;
@@ -218,7 +254,8 @@ define([
218
254
importKey = key + '_imported' + importNo ;
219
255
importNo += 1 ;
220
256
}
221
- var newSnippet = { [ importKey ] : { code : defaultSnippets [ key ] , timestamp : timestamp } } ;
257
+ var code = defaultSnippets [ key ] . join ( '\n' ) ;
258
+ var newSnippet = { [ importKey ] : { code : code , timestamp : timestamp } } ;
222
259
vpSetting . saveUserDefinedCode ( newSnippet ) ;
223
260
224
261
importKeys . push ( importKey ) ;
0 commit comments