@jupyterlab/console 4.0.0-alpha.19 → 4.0.0-alpha.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/panel.ts ADDED
@@ -0,0 +1,329 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ import {
5
+ ISessionContext,
6
+ MainAreaWidget,
7
+ SessionContext,
8
+ sessionContextDialogs
9
+ } from '@jupyterlab/apputils';
10
+ import { IEditorMimeTypeService } from '@jupyterlab/codeeditor';
11
+ import { PathExt, Time, URLExt } from '@jupyterlab/coreutils';
12
+ import {
13
+ IRenderMimeRegistry,
14
+ RenderMimeRegistry
15
+ } from '@jupyterlab/rendermime';
16
+ import { ServiceManager } from '@jupyterlab/services';
17
+ import { ITranslator, nullTranslator } from '@jupyterlab/translation';
18
+ import { consoleIcon } from '@jupyterlab/ui-components';
19
+ import { Token, UUID } from '@lumino/coreutils';
20
+ import { IDisposable } from '@lumino/disposable';
21
+ import { Message } from '@lumino/messaging';
22
+ import { Panel } from '@lumino/widgets';
23
+ import { CodeConsole } from './widget';
24
+
25
+ /**
26
+ * The class name added to console panels.
27
+ */
28
+ const PANEL_CLASS = 'jp-ConsolePanel';
29
+
30
+ /**
31
+ * A panel which contains a console and the ability to add other children.
32
+ */
33
+ export class ConsolePanel extends MainAreaWidget<Panel> {
34
+ /**
35
+ * Construct a console panel.
36
+ */
37
+ constructor(options: ConsolePanel.IOptions) {
38
+ super({ content: new Panel() });
39
+ this.addClass(PANEL_CLASS);
40
+ let {
41
+ rendermime,
42
+ mimeTypeService,
43
+ path,
44
+ basePath,
45
+ name,
46
+ manager,
47
+ modelFactory,
48
+ sessionContext,
49
+ translator
50
+ } = options;
51
+ this.translator = translator || nullTranslator;
52
+ const trans = this.translator.load('jupyterlab');
53
+
54
+ const contentFactory = (this.contentFactory = options.contentFactory);
55
+ const count = Private.count++;
56
+ if (!path) {
57
+ path = URLExt.join(basePath || '', `console-${count}-${UUID.uuid4()}`);
58
+ }
59
+
60
+ sessionContext = this._sessionContext =
61
+ sessionContext ||
62
+ new SessionContext({
63
+ sessionManager: manager.sessions,
64
+ specsManager: manager.kernelspecs,
65
+ path,
66
+ name: name || trans.__('Console %1', count),
67
+ type: 'console',
68
+ kernelPreference: options.kernelPreference,
69
+ setBusy: options.setBusy
70
+ });
71
+
72
+ const resolver = new RenderMimeRegistry.UrlResolver({
73
+ session: sessionContext,
74
+ contents: manager.contents
75
+ });
76
+ rendermime = rendermime.clone({ resolver });
77
+
78
+ this.console = contentFactory.createConsole({
79
+ rendermime,
80
+ sessionContext: sessionContext,
81
+ mimeTypeService,
82
+ contentFactory,
83
+ modelFactory,
84
+ translator
85
+ });
86
+ this.content.addWidget(this.console);
87
+
88
+ void sessionContext.initialize().then(async value => {
89
+ if (value) {
90
+ await sessionContextDialogs.selectKernel(sessionContext!);
91
+ }
92
+ this._connected = new Date();
93
+ this._updateTitlePanel();
94
+ });
95
+
96
+ this.console.executed.connect(this._onExecuted, this);
97
+ this._updateTitlePanel();
98
+ sessionContext.kernelChanged.connect(this._updateTitlePanel, this);
99
+ sessionContext.propertyChanged.connect(this._updateTitlePanel, this);
100
+
101
+ this.title.icon = consoleIcon;
102
+ this.title.closable = true;
103
+ this.id = `console-${count}`;
104
+ }
105
+
106
+ /**
107
+ * The content factory used by the console panel.
108
+ */
109
+ readonly contentFactory: ConsolePanel.IContentFactory;
110
+
111
+ /**
112
+ * The console widget used by the panel.
113
+ */
114
+ console: CodeConsole;
115
+
116
+ /**
117
+ * The session used by the panel.
118
+ */
119
+ get sessionContext(): ISessionContext {
120
+ return this._sessionContext;
121
+ }
122
+
123
+ /**
124
+ * Dispose of the resources held by the widget.
125
+ */
126
+ dispose(): void {
127
+ this.sessionContext.dispose();
128
+ this.console.dispose();
129
+ super.dispose();
130
+ }
131
+
132
+ /**
133
+ * Handle `'activate-request'` messages.
134
+ */
135
+ protected onActivateRequest(msg: Message): void {
136
+ const prompt = this.console.promptCell;
137
+ if (prompt) {
138
+ prompt.editor!.focus();
139
+ }
140
+ }
141
+
142
+ /**
143
+ * Handle `'close-request'` messages.
144
+ */
145
+ protected onCloseRequest(msg: Message): void {
146
+ super.onCloseRequest(msg);
147
+ this.dispose();
148
+ }
149
+
150
+ /**
151
+ * Handle a console execution.
152
+ */
153
+ private _onExecuted(sender: CodeConsole, args: Date) {
154
+ this._executed = args;
155
+ this._updateTitlePanel();
156
+ }
157
+
158
+ /**
159
+ * Update the console panel title.
160
+ */
161
+ private _updateTitlePanel(): void {
162
+ Private.updateTitle(this, this._connected, this._executed, this.translator);
163
+ }
164
+
165
+ translator: ITranslator;
166
+ private _executed: Date | null = null;
167
+ private _connected: Date | null = null;
168
+ private _sessionContext: ISessionContext;
169
+ }
170
+
171
+ /**
172
+ * A namespace for ConsolePanel statics.
173
+ */
174
+ export namespace ConsolePanel {
175
+ /**
176
+ * The initialization options for a console panel.
177
+ */
178
+ export interface IOptions {
179
+ /**
180
+ * The rendermime instance used by the panel.
181
+ */
182
+ rendermime: IRenderMimeRegistry;
183
+
184
+ /**
185
+ * The content factory for the panel.
186
+ */
187
+ contentFactory: IContentFactory;
188
+
189
+ /**
190
+ * The service manager used by the panel.
191
+ */
192
+ manager: ServiceManager.IManager;
193
+
194
+ /**
195
+ * The path of an existing console.
196
+ */
197
+ path?: string;
198
+
199
+ /**
200
+ * The base path for a new console.
201
+ */
202
+ basePath?: string;
203
+
204
+ /**
205
+ * The name of the console.
206
+ */
207
+ name?: string;
208
+
209
+ /**
210
+ * A kernel preference.
211
+ */
212
+ kernelPreference?: ISessionContext.IKernelPreference;
213
+
214
+ /**
215
+ * An existing session context to use.
216
+ */
217
+ sessionContext?: ISessionContext;
218
+
219
+ /**
220
+ * The model factory for the console widget.
221
+ */
222
+ modelFactory?: CodeConsole.IModelFactory;
223
+
224
+ /**
225
+ * The service used to look up mime types.
226
+ */
227
+ mimeTypeService: IEditorMimeTypeService;
228
+
229
+ /**
230
+ * The application language translator.
231
+ */
232
+ translator?: ITranslator;
233
+
234
+ /**
235
+ * A function to call when the kernel is busy.
236
+ */
237
+ setBusy?: () => IDisposable;
238
+ }
239
+
240
+ /**
241
+ * The console panel renderer.
242
+ */
243
+ export interface IContentFactory extends CodeConsole.IContentFactory {
244
+ /**
245
+ * Create a new console panel.
246
+ */
247
+ createConsole(options: CodeConsole.IOptions): CodeConsole;
248
+ }
249
+
250
+ /**
251
+ * Default implementation of `IContentFactory`.
252
+ */
253
+ export class ContentFactory
254
+ extends CodeConsole.ContentFactory
255
+ implements IContentFactory
256
+ {
257
+ /**
258
+ * Create a new console panel.
259
+ */
260
+ createConsole(options: CodeConsole.IOptions): CodeConsole {
261
+ return new CodeConsole(options);
262
+ }
263
+ }
264
+
265
+ /**
266
+ * A namespace for the console panel content factory.
267
+ */
268
+ export namespace ContentFactory {
269
+ /**
270
+ * Options for the code console content factory.
271
+ */
272
+ export interface IOptions extends CodeConsole.ContentFactory.IOptions {}
273
+ }
274
+
275
+ /**
276
+ * The console renderer token.
277
+ */
278
+ export const IContentFactory = new Token<IContentFactory>(
279
+ '@jupyterlab/console:IContentFactory'
280
+ );
281
+ }
282
+
283
+ /**
284
+ * A namespace for private data.
285
+ */
286
+ namespace Private {
287
+ /**
288
+ * The counter for new consoles.
289
+ */
290
+ export let count = 1;
291
+
292
+ /**
293
+ * Update the title of a console panel.
294
+ */
295
+ export function updateTitle(
296
+ panel: ConsolePanel,
297
+ connected: Date | null,
298
+ executed: Date | null,
299
+ translator?: ITranslator
300
+ ): void {
301
+ translator = translator || nullTranslator;
302
+ const trans = translator.load('jupyterlab');
303
+
304
+ const sessionContext = panel.console.sessionContext.session;
305
+ if (sessionContext) {
306
+ // FIXME:
307
+ let caption =
308
+ trans.__('Name: %1\n', sessionContext.name) +
309
+ trans.__('Directory: %1\n', PathExt.dirname(sessionContext.path)) +
310
+ trans.__('Kernel: %1', panel.console.sessionContext.kernelDisplayName);
311
+
312
+ if (connected) {
313
+ caption += trans.__(
314
+ '\nConnected: %1',
315
+ Time.format(connected.toISOString())
316
+ );
317
+ }
318
+
319
+ if (executed) {
320
+ caption += trans.__('\nLast Execution: %1');
321
+ }
322
+ panel.title.label = sessionContext.name;
323
+ panel.title.caption = caption;
324
+ } else {
325
+ panel.title.label = trans.__('Console');
326
+ panel.title.caption = '';
327
+ }
328
+ }
329
+ }
package/src/tokens.ts ADDED
@@ -0,0 +1,18 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ import { IWidgetTracker } from '@jupyterlab/apputils';
5
+ import { Token } from '@lumino/coreutils';
6
+ import { ConsolePanel } from './panel';
7
+
8
+ /**
9
+ * The console tracker token.
10
+ */
11
+ export const IConsoleTracker = new Token<IConsoleTracker>(
12
+ '@jupyterlab/console:IConsoleTracker'
13
+ );
14
+
15
+ /**
16
+ * A class that tracks console widgets.
17
+ */
18
+ export interface IConsoleTracker extends IWidgetTracker<ConsolePanel> {}