Skip to content

Commit 5b0e305

Browse files
author
minjk-bl
committed
add default snippets
1 parent 28212e3 commit 5b0e305

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

src/file_io/udf.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,44 @@ define([
203203
} else if (menu == 'default-snippets') {
204204
// import default snippets
205205
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+
]
207242
}
243+
208244
var timestamp = new Date().getTime();
209245

210246
var keys = Object.keys(defaultSnippets);
@@ -218,7 +254,8 @@ define([
218254
importKey = key + '_imported' + importNo;
219255
importNo += 1;
220256
}
221-
var newSnippet = { [importKey]: { code: defaultSnippets[key], timestamp: timestamp } };
257+
var code = defaultSnippets[key].join('\n');
258+
var newSnippet = { [importKey]: { code: code, timestamp: timestamp } };
222259
vpSetting.saveUserDefinedCode(newSnippet);
223260

224261
importKeys.push(importKey);

0 commit comments

Comments
 (0)