@@ -8,22 +8,23 @@ import { find } from '@lumino/algorithm';
8
8
import { CommandRegistry } from '@lumino/commands' ;
9
9
import { BoxPanel , Widget } from '@lumino/widgets' ;
10
10
import { IChangedArgs } from '@jupyterlab/coreutils' ;
11
- import { Cell , ICellModel , MarkdownCell } from '@jupyterlab/cells' ;
12
11
import { Contents , ServiceManager , Kernel as JupyterKernel , SessionManager } from '@jupyterlab/services' ;
13
12
import { DocumentRegistry , Context } from '@jupyterlab/docregistry' ;
14
- import { IRenderMime } from '@jupyterlab/rendermime-interfaces' ;
15
- import { standardRendererFactories , RenderMimeRegistry } from '@jupyterlab/rendermime' ;
16
- import { rendererFactory as jsonRendererFactory } from '@jupyterlab/json-extension' ;
17
- import { rendererFactory as javascriptRendererFactory } from '@jupyterlab/javascript-extension' ;
18
- import { Notebook , NotebookPanel , NotebookWidgetFactory , NotebookTracker , INotebookModel } from '@jupyterlab/notebook' ;
19
13
import { ybinding , CodeMirrorEditorFactory , CodeMirrorMimeTypeService , EditorLanguageRegistry , EditorExtensionRegistry , EditorThemeRegistry } from '@jupyterlab/codemirror' ;
20
14
import { IEditorServices } from '@jupyterlab/codeeditor' ;
15
+ import { standardRendererFactories , RenderMimeRegistry } from '@jupyterlab/rendermime' ;
16
+ import { IRenderMime } from '@jupyterlab/rendermime-interfaces' ;
17
+ import { ISharedAttachmentsCell , IYText } from '@jupyter/ydoc' ;
18
+ import { INotebookContent , CellType , IAttachments } from '@jupyterlab/nbformat' ;
21
19
import { Completer , CompleterModel , CompletionHandler , ProviderReconciliator , KernelCompleterProvider } from '@jupyterlab/completer' ;
20
+ import { Notebook , NotebookPanel , NotebookWidgetFactory , NotebookTracker , INotebookModel } from '@jupyterlab/notebook' ;
21
+ import { Cell , ICellModel , MarkdownCell } from '@jupyterlab/cells' ;
22
+ import { rendererFactory as jsonRendererFactory } from '@jupyterlab/json-extension' ;
23
+ import { rendererFactory as javascriptRendererFactory } from '@jupyterlab/javascript-extension' ;
22
24
import { MathJaxTypesetter } from '@jupyterlab/mathjax-extension' ;
23
- import { INotebookContent , CellType , IAttachments } from '@jupyterlab/nbformat' ;
24
- import { ISharedAttachmentsCell , IYText } from '@jupyter/ydoc' ;
25
25
import { WIDGET_MIMETYPE } from '@jupyter-widgets/html-manager/lib/output_renderers' ;
26
26
import { Lite , Kernel , WidgetManager , WidgetLabRenderer } from '../../jupyter' ;
27
+ import { OnKernelConnection } from '../../state' ;
27
28
import { ICellSidebarProps } from './cell/sidebar/CellSidebarWidget' ;
28
29
import { JupyterReactContentFactory } from './content/JupyterReactContentFactory' ;
29
30
import { JupyterReactNotebookModelFactory } from './model/JupyterReactNotebookModelFactory' ;
@@ -48,17 +49,22 @@ export class NotebookAdapter {
48
49
private _nbgrader : boolean ;
49
50
private _notebookModelFactory ?: JupyterReactNotebookModelFactory ;
50
51
private _notebookPanel ?: NotebookPanel ;
51
- private _url ?: string ;
52
+ private _onKernelConnection ?: OnKernelConnection ;
52
53
private _path ?: string ;
53
54
private _readonly : boolean ;
54
55
private _renderers : IRenderMime . IRendererFactory [ ] ;
55
56
private _rendermime ?: RenderMimeRegistry ;
56
57
private _serverless : boolean ;
57
58
private _serviceManager : ServiceManager . IManager ;
58
59
private _tracker ?: NotebookTracker ;
60
+ private _url ?: string ;
59
61
60
62
constructor ( props : INotebookProps ) {
63
+
64
+ console . log ( 'A new Notebook Adapter is being created...' ) ;
65
+
61
66
this . _CellSidebar = props . CellSidebar ;
67
+
62
68
this . _id = props . id ;
63
69
this . _kernel = props . kernel ;
64
70
this . _lite = props . lite ;
@@ -71,6 +77,8 @@ export class NotebookAdapter {
71
77
this . _serviceManager = props . serviceManager ! ;
72
78
this . _url = props . url ;
73
79
80
+ this . _onKernelConnection = props . onKernelConnection ;
81
+
74
82
this . _boxPanel = new BoxPanel ( ) ;
75
83
this . _boxPanel . addClass ( 'dla-Jupyter-Notebook' ) ;
76
84
this . _boxPanel . spacing = 0 ;
@@ -149,6 +157,8 @@ export class NotebookAdapter {
149
157
150
158
initializeContext ( ) {
151
159
160
+ const isNbFormat = this . _path !== undefined && this . _path !== '' ? false : true ;
161
+
152
162
this . _context = new Context ( {
153
163
manager : this . _serviceManager ,
154
164
factory : this . _notebookModelFactory ! ,
@@ -177,10 +187,7 @@ export class NotebookAdapter {
177
187
},
178
188
1
179
189
);
180
- */
181
- /*
182
- // This code block was causing https://github.com/datalayer/jupyter-ui/issues/195
183
- // TODO Double check there is not side effect.
190
+ // This code block is causing https://github.com/datalayer/jupyter-ui/issues/195
184
191
this._context?.sessionContext
185
192
.changeKernel({id: this._kernel.id })
186
193
.then(() => {
@@ -189,53 +196,56 @@ export class NotebookAdapter {
189
196
);
190
197
});
191
198
*/
199
+
192
200
// These are fixes to have more control on the kernel launch.
193
- ( this . _context . sessionContext as any ) . _initialize =
194
- async ( ) : Promise < boolean > => {
195
- const manager = ( this . _context ! . sessionContext as any )
196
- . sessionManager as SessionManager ;
197
- await manager . ready ;
198
- await manager . refreshRunning ( ) ;
199
- const model = find ( manager . running ( ) , model => {
200
- return model . kernel ?. id === this . _kernel ?. id ;
201
- } ) ;
202
- if ( model ) {
203
- try {
204
- const session = manager . connectTo ( {
205
- model : {
206
- ...model ,
207
- path : this . _path ?? model . path ,
208
- name : this . _path ?? model . name ,
209
- } ,
210
- kernelConnectionOptions : {
211
- handleComms : true ,
212
- } ,
213
- } ) ;
214
- ( this . _context ! . sessionContext as any ) . _handleNewSession ( session ) ;
215
- } catch ( err ) {
216
- void ( this . _context ! . sessionContext as any ) . _handleSessionError (
217
- err
218
- ) ;
219
- return Promise . reject ( err ) ;
220
- }
201
+ ( this . _context . sessionContext as any ) . _initialize = async ( ) : Promise < boolean > => {
202
+ const manager = ( this . _context ! . sessionContext as any ) . sessionManager as SessionManager ;
203
+ await manager . ready ;
204
+ await manager . refreshRunning ( ) ;
205
+ const model = find ( manager . running ( ) , model => {
206
+ return model . kernel ?. id === this . _kernel ?. id ;
207
+ } ) ;
208
+ if ( model ) {
209
+ try {
210
+ const session = manager . connectTo ( {
211
+ model : {
212
+ ...model ,
213
+ path : this . _path ?? model . path ,
214
+ name : this . _path ?? model . name ,
215
+ } ,
216
+ kernelConnectionOptions : {
217
+ handleComms : true ,
218
+ } ,
219
+ } ) ;
220
+ ( this . _context ! . sessionContext as any ) . _handleNewSession ( session ) ;
221
+ } catch ( err ) {
222
+ void ( this . _context ! . sessionContext as any ) . _handleSessionError (
223
+ err
224
+ ) ;
225
+ return Promise . reject ( err ) ;
221
226
}
222
- return await ( this . _context ! . sessionContext as any ) . _startIfNecessary ( ) ;
223
- } ;
227
+ }
228
+ return await ( this . _context ! . sessionContext as any ) . _startIfNecessary ( ) ;
229
+ } ;
230
+
231
+ this . _context . sessionContext . ready . then ( ( ) => {
232
+ if ( this . _onKernelConnection ) {
233
+ const kernelConnection = this . _context ?. sessionContext . session ?. kernel ;
234
+ this . _onKernelConnection ( kernelConnection ) ;
235
+ }
236
+ } ) ;
224
237
225
238
this . _context . sessionContext . kernelChanged . connect ( ( _ , args ) => {
226
- console . log ( 'Previous Jupyter Kernel connection ' , args . oldValue ) ;
239
+ console . log ( 'Previous Jupyter Kernel Connection. ' , args . oldValue ) ;
227
240
const kernelConnection = args . newValue ;
228
- console . log ( 'Current Jupyter Kernel connection ' , kernelConnection ) ;
241
+ console . log ( 'Current Jupyter Kernel Connection. ' , kernelConnection ) ;
229
242
if ( kernelConnection && ! kernelConnection . handleComms ) {
230
- console . warn (
231
- 'The current Kernel Connection does not handle Comms' ,
232
- kernelConnection . id
233
- ) ;
243
+ console . warn ( 'The current Kernel Connection does not handle Comms...' , kernelConnection . id ) ;
234
244
( kernelConnection as any ) . handleComms = true ;
235
- console . log (
236
- 'The current Kernel Connection is updated to enforce Comms support' ,
237
- kernelConnection . handleComms
238
- ) ;
245
+ console . log ( 'The current Kernel Connection is updated to enforce Comms support!' , kernelConnection . handleComms ) ;
246
+ }
247
+ if ( this . _onKernelConnection ) {
248
+ this . _onKernelConnection ( kernelConnection ) ;
239
249
}
240
250
} ) ;
241
251
@@ -302,10 +312,35 @@ export class NotebookAdapter {
302
312
this . _notebookPanel ?. update ( ) ;
303
313
} ) ;
304
314
305
- const isNbFormat = ( this . _path !== undefined && this . _path !== '' ) ? false : true ;
306
-
307
315
if ( isNbFormat ) {
308
316
// If nbformat is provided and we don't want to interact with the content manager.
317
+ ( this . _context as any ) . _populate = async ( ) : Promise < void > => {
318
+ ( this . _context as any ) . _isPopulated = true ;
319
+ ( this . _context as any ) . _isReady = true ;
320
+ ( this . _context as any ) . _populatedPromise . resolve ( void 0 ) ;
321
+ // Add a checkpoint if none exists and the file is writable.
322
+ // Force skip this step for nbformat notebooks.
323
+ // await (this._context as any)._maybeCheckpoint(false);
324
+ if ( ( this . _context as any ) . isDisposed ) {
325
+ return ;
326
+ }
327
+ // Update the kernel preference.
328
+ const name = ( this . _context as any ) . _model . defaultKernelName ||
329
+ ( this . _context as any ) . sessionContext . kernelPreference . name ;
330
+ ( this . _context as any ) . sessionContext . kernelPreference = {
331
+ ...( this . _context as any ) . sessionContext . kernelPreference ,
332
+ name,
333
+ language : ( this . _context as any ) . _model . defaultKernelLanguage
334
+ } ;
335
+ // Note: we don't wait on the session to initialize
336
+ // so that the user can be shown the content before
337
+ // any kernel has started.
338
+ void ( this . _context as any ) . sessionContext . initialize ( ) . then ( ( shouldSelect : boolean ) => {
339
+ if ( shouldSelect ) {
340
+ void ( this . _context as any ) . _dialogs . selectKernel ( ( this . _context ! . sessionContext as any ) . sessionContext ) ;
341
+ }
342
+ } ) ;
343
+ } ;
309
344
( this . _context as any ) . initialize = async ( isNew : boolean ) : Promise < void > => {
310
345
( this . _context as Context < INotebookModel > ) . model . dirty = false ;
311
346
const now = new Date ( ) . toISOString ( ) ;
0 commit comments