forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
143 lines (129 loc) · 3.87 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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
ILayoutRestorer,
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';
import {
IRunningSessions,
IRunningSessionManagers,
RunningSessionManagers,
RunningSessions
} from '@jupyterlab/running';
import { Session } from '@jupyterlab/services';
import { PathExt } from '@jupyterlab/coreutils';
import { toArray } from '@phosphor/algorithm';
/**
* The class name added to a notebook icon.
*/
const NOTEBOOK_ICON_CLASS = 'jp-mod-notebook';
/**
* The class name added to a console icon.
*/
const CONSOLE_ICON_CLASS = 'jp-mod-console';
/**
* The class name added to a file icon.
*/
const FILE_ICON_CLASS = 'jp-mod-file';
/**
* The default running sessions extension.
*/
const plugin: JupyterFrontEndPlugin<IRunningSessionManagers> = {
activate,
id: '@jupyterlab/running-extension:plugin',
provides: IRunningSessionManagers,
optional: [ILayoutRestorer],
autoStart: true
};
/**
* Export the plugin as default.
*/
export default plugin;
/**
* Activate the running plugin.
*/
function activate(
app: JupyterFrontEnd,
restorer: ILayoutRestorer | null
): IRunningSessionManagers {
let runningSessionManagers = new RunningSessionManagers();
let running = new RunningSessions(runningSessionManagers);
running.id = 'jp-running-sessions';
running.title.iconClass = 'jp-RunningIcon jp-SideBar-tabIcon';
running.title.caption = 'Running Terminals and Kernels';
// Let the application restorer track the running panel for restoration of
// application state (e.g. setting the running panel as the current side bar
// widget).
if (restorer) {
restorer.add(running, 'running-sessions');
}
addKernelRunningSessionManager(runningSessionManagers, app);
// Rank has been chosen somewhat arbitrarily to give priority to the running
// sessions widget in the sidebar.
app.shell.add(running, 'left', { rank: 200 });
return runningSessionManagers;
}
/**
* Add the running kernel manager (notebooks & consoles) to the running panel.
*/
function addKernelRunningSessionManager(
managers: IRunningSessionManagers,
app: JupyterFrontEnd
) {
let manager = app.serviceManager.sessions;
function filterSessions(m: Session.IModel) {
return !!(
(m.name || PathExt.basename(m.path)).indexOf('.') !== -1 || m.name
);
}
managers.add({
name: 'Kernel',
running: () => {
return toArray(manager.running())
.filter(filterSessions)
.map(model => new RunningKernel(model));
},
shutdownAll: () => manager.shutdownAll(),
refreshRunning: () => manager.refreshRunning(),
runningChanged: manager.runningChanged
});
class RunningKernel implements IRunningSessions.IRunningItem {
constructor(model: Session.IModel) {
this._model = model;
}
open() {
let { path, type } = this._model;
if (type.toLowerCase() === 'console') {
void app.commands.execute('console:open', { path });
} else {
void app.commands.execute('docmanager:open', { path });
}
}
shutdown() {
return manager.shutdown(this._model.id);
}
iconClass() {
let { name, path, type } = this._model;
if ((name || PathExt.basename(path)).indexOf('.ipynb') !== -1) {
return NOTEBOOK_ICON_CLASS;
} else if (type.toLowerCase() === 'console') {
return CONSOLE_ICON_CLASS;
}
return FILE_ICON_CLASS;
}
label() {
return this._model.name || PathExt.basename(this._model.path);
}
labelTitle() {
let { kernel, path } = this._model;
let kernelName = kernel.name;
if (manager.specs) {
const spec = manager.specs.kernelspecs[kernelName];
kernelName = spec ? spec.display_name : 'unknown';
}
return `Path: ${path}\nKernel: ${kernelName}`;
}
private _model: Session.IModel;
}
}