forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snippets.ts
55 lines (52 loc) · 1.48 KB
/
snippets.ts
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
/*-----------------------------------------------------------------------------
| Copyright (c) Jupyter Development Team.
| Distributed under the terms of the Modified BSD License.
|----------------------------------------------------------------------------*/
import {
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';
import {
IConverterRegistry,
snippetViewerConverter,
fileSnippetConverter,
URLSnippetConverter
} from '@jupyterlab/databus';
import { INotebookTracker } from '@jupyterlab/notebook';
export default {
activate,
id: '@jupyterlab/databus-extension:snippets',
requires: [IConverterRegistry, INotebookTracker],
autoStart: true
} as JupyterFrontEndPlugin<void>;
function activate(
app: JupyterFrontEnd,
converters: IConverterRegistry,
notebookTracker: INotebookTracker
) {
converters.register(
snippetViewerConverter(async (snippet: string) => {
notebookTracker.activeCell.model.value.insert(0, snippet);
})
);
converters.register(
fileSnippetConverter({
mimeType: 'text/csv',
label: 'Snippet',
createSnippet: (path: string) =>
`import pandas as pd\n\ndf = pd.read_csv(${JSON.stringify(
'.' + path
)})\ndf`
})
);
converters.register(
URLSnippetConverter({
mimeType: 'text/csv',
label: 'Snippet',
createSnippet: (url: string | URL) =>
`import pandas as pd\n\ndf = pd.read_csv(${JSON.stringify(
url.toString()
)})\ndf`
})
);
}