forked from jupyter/notebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
232 lines (201 loc) · 5.9 KB
/
index.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';
import {
IToolbarWidgetRegistry,
createToolbarFactory,
setToolbar
} from '@jupyterlab/apputils';
import {
IFileBrowserFactory,
FileBrowser,
Uploader
} from '@jupyterlab/filebrowser';
import { ISettingRegistry } from '@jupyterlab/settingregistry';
import { IRunningSessionManagers, RunningSessions } from '@jupyterlab/running';
import { ISettingEditorTracker } from '@jupyterlab/settingeditor';
import { ITranslator } from '@jupyterlab/translation';
import {
caretDownIcon,
folderIcon,
runningIcon
} from '@jupyterlab/ui-components';
import { Menu, MenuBar } from '@lumino/widgets';
import { NotebookTreeWidget, INotebookTree } from '@jupyter-notebook/tree';
/**
* The file browser factory.
*/
const FILE_BROWSER_FACTORY = 'FileBrowser';
/**
* The file browser plugin id.
*/
const FILE_BROWSER_PLUGIN_ID = '@jupyterlab/filebrowser-extension:browser';
/**
* The namespace for command IDs.
*/
namespace CommandIDs {
// The command to activate the filebrowser widget in tree view.
export const activate = 'filebrowser:activate';
}
/**
* Plugin to add extra commands to the file browser to create
* new notebooks, files, consoles and terminals
*/
const createNew: JupyterFrontEndPlugin<void> = {
id: '@jupyter-notebook/tree-extension:new',
requires: [ITranslator],
optional: [IToolbarWidgetRegistry],
autoStart: true,
activate: (
app: JupyterFrontEnd,
translator: ITranslator,
toolbarRegistry: IToolbarWidgetRegistry | null
) => {
const { commands } = app;
const trans = translator.load('notebook');
const menubar = new MenuBar();
const newMenu = new Menu({ commands });
newMenu.title.label = trans.__('New');
newMenu.title.icon = caretDownIcon;
menubar.addMenu(newMenu);
const newCommands = [
'notebook:create-new',
'terminal:create-new',
'console:create',
'filebrowser:create-new-file',
'filebrowser:create-new-directory'
];
newCommands.forEach(command => {
newMenu.addItem({ command });
});
if (toolbarRegistry) {
toolbarRegistry.addFactory(
FILE_BROWSER_FACTORY,
'new-dropdown',
(browser: FileBrowser) => {
const menubar = new MenuBar();
menubar.addMenu(newMenu);
menubar.addClass('jp-DropdownMenu');
return menubar;
}
);
}
}
};
/**
* A plugin to add file browser commands for the tree view.
*/
const openFileBrowser: JupyterFrontEndPlugin<void> = {
id: '@jupyter-notebook/tree-extension:open-file-browser',
requires: [INotebookTree, IFileBrowserFactory],
autoStart: true,
activate: (
app: JupyterFrontEnd,
notebookTree: INotebookTree,
factory: IFileBrowserFactory
) => {
const { commands } = app;
commands.addCommand(CommandIDs.activate, {
execute: () => {
const { defaultBrowser: browser } = factory;
notebookTree.currentWidget = browser;
}
});
}
};
/**
* A plugin to add the file browser widget to an INotebookShell
*/
const notebookTreeWidget: JupyterFrontEndPlugin<INotebookTree> = {
id: '@jupyter-notebook/tree-extension:widget',
requires: [
IFileBrowserFactory,
ITranslator,
ISettingRegistry,
IToolbarWidgetRegistry
],
optional: [IRunningSessionManagers, ISettingEditorTracker],
autoStart: true,
provides: INotebookTree,
activate: (
app: JupyterFrontEnd,
factory: IFileBrowserFactory,
translator: ITranslator,
settingRegistry: ISettingRegistry,
toolbarRegistry: IToolbarWidgetRegistry,
manager: IRunningSessionManagers | null,
settingEditorTracker: ISettingEditorTracker | null
): INotebookTree => {
const nbTreeWidget = new NotebookTreeWidget();
const trans = translator.load('notebook');
const { defaultBrowser: browser } = factory;
browser.title.label = trans.__('Files');
browser.node.setAttribute('role', 'region');
browser.node.setAttribute('aria-label', trans.__('File Browser Section'));
browser.title.icon = folderIcon;
nbTreeWidget.addWidget(browser);
nbTreeWidget.tabBar.addTab(browser.title);
// Toolbar
toolbarRegistry.addFactory(
FILE_BROWSER_FACTORY,
'uploader',
(browser: FileBrowser) =>
new Uploader({
model: browser.model,
translator,
label: trans.__('Upload')
})
);
setToolbar(
browser,
createToolbarFactory(
toolbarRegistry,
settingRegistry,
FILE_BROWSER_FACTORY,
notebookTreeWidget.id,
translator
)
);
if (manager) {
const running = new RunningSessions(manager, translator);
running.id = 'jp-running-sessions';
running.title.label = trans.__('Running');
running.title.icon = runningIcon;
nbTreeWidget.addWidget(running);
nbTreeWidget.tabBar.addTab(running.title);
}
// show checkboxes by default if there is no user setting override
const settings = settingRegistry.load(FILE_BROWSER_PLUGIN_ID);
Promise.all([settings, app.restored])
.then(([settings]) => {
if (settings.user.showFileCheckboxes !== undefined) {
return;
}
void settings.set('showFileCheckboxes', true);
})
.catch((reason: Error) => {
console.error(reason.message);
});
app.shell.add(nbTreeWidget, 'main', { rank: 100 });
if (settingEditorTracker) {
settingEditorTracker.widgetAdded.connect((_, editor) => {
nbTreeWidget.addWidget(editor);
nbTreeWidget.tabBar.addTab(editor.title);
nbTreeWidget.currentWidget = editor;
});
}
return nbTreeWidget;
}
};
/**
* Export the plugins as default.
*/
const plugins: JupyterFrontEndPlugin<any>[] = [
createNew,
openFileBrowser,
notebookTreeWidget
];
export default plugins;