You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/widgets/ipylab.ts
+92-32Lines changed: 92 additions & 32 deletions
Original file line number
Diff line number
Diff line change
@@ -217,20 +217,38 @@ export class IpylabModel extends DOMWidgetModel {
217
217
}
218
218
}
219
219
220
+
/**
221
+
* Handles custom messages received from the Python backend.
222
+
*
223
+
* @param msg The message received from the backend.
224
+
*/
220
225
protectedonCustomMessage(msg: any){
221
226
if(msg.ipylab){
222
227
this._onBackendMessage(JSON.parse(msg.ipylab));
223
228
}
224
229
}
225
230
226
231
/**
227
-
* Handle messages
228
-
* 1. Response to requested operation sent to Python (ipylab_FE).
229
-
* 2. Operation requests received from the Python (ipylab_PY).
230
-
* 3. close - close always starts from the frontend unless the kernel is lost.
231
-
* 4. Log messages for the Jupyterlab logger.
232
+
* Handles messages received from the backend.
233
+
*
234
+
* This method processes messages from the Python backend, which can include:
235
+
* - Results of operations requested by the frontend.
236
+
* - Requests for operations to be performed by the frontend on behalf of the backend.
237
+
* - Instructions to close the widget.
238
+
*
239
+
* @param content - The content of the message received from the backend. The content
240
+
* is expected to have one of the following properties: `ipylab_FE`, `ipylab_PY`, or `close`.
232
241
*
233
-
* @param msg
242
+
* If `content.ipylab_FE` is present:
243
+
* - Retrieves the corresponding pending operation.
244
+
* - Replaces parts of the widget based on the received keyword arguments and conversion flags.
245
+
* - Resolves or rejects the pending operation based on the success of the `replaceParts` method and the presence of an error in the content.
246
+
*
247
+
* If `content.ipylab_PY` is present:
248
+
* - Executes an operation requested by the Python backend using the `doOperationForPython` method.
249
+
*
250
+
* If `content.close` is present:
251
+
* - Closes the widget.
234
252
*/
235
253
privateasync_onBackendMessage(content: any){
236
254
if(content.ipylab_FE){
@@ -258,8 +276,35 @@ export class IpylabModel extends DOMWidgetModel {
258
276
}
259
277
260
278
/**
261
-
* Perform an operation request issued from a Python kernel.
262
-
* @param content
279
+
* Executes a specified operation in Python, handles data transformation,
280
+
* and sends the result back to the Python environment.
281
+
*
282
+
* @param content - An object containing the operation details,
283
+
* including the operation name, Python object identifier,
284
+
* transformation instructions, keyword arguments, and flags for
285
+
* Lumino widget and object conversion.
286
+
*
287
+
* @remarks
288
+
* This method orchestrates the execution of a Python operation,
289
+
* potentially transforming the result before sending it back.
290
+
* It also handles error reporting to the Python environment.
291
+
*
292
+
* The `content` object is expected to have the following properties:
293
+
* - `operation`: The name of the operation to execute in Python.
294
+
* - `ipylab_PY`: An identifier for the Python object associated with the operation.
295
+
* - `transform`: Instructions for transforming the result object.
296
+
* - `kwgs`: Keyword arguments to pass to the Python operation.
297
+
* - `toLuminoWidget`: A flag indicating whether to convert objects to Lumino widgets.
298
+
* - `toObject`: A flag indicating whether to convert objects to plain JavaScript objects.
299
+
*
300
+
* The method performs the following steps:
301
+
* 1. Replaces parts of the keyword arguments based on `toLuminoWidget` and `toObject` flags.
302
+
* 2. Executes the specified operation with the provided keyword arguments.
303
+
* 3. Extracts the payload and buffers from the result object, if present.
304
+
* 4. Transforms the result object using the provided transformation instructions.
305
+
* 5. Sends the transformed payload back to the Python environment.
306
+
* 6. If an error occurs during any of these steps, it sends an error message
307
+
* back to the Python environment and logs the error to the console.
263
308
*/
264
309
privateasyncdoOperationForPython(content: any){
265
310
const{ operation, ipylab_PY, transform }=content;
@@ -285,13 +330,21 @@ export class IpylabModel extends DOMWidgetModel {
285
330
}
286
331
287
332
/**
288
-
* Replace parts in obj that are indicated by the arrays
289
-
* - toLuminoWidget
290
-
* - toObject
333
+
* Replaces parts of a nested object with Lumino widgets or plain JavaScript objects.
334
+
*
335
+
* @param obj The object whose parts need to be replaced.
336
+
* @param toLuminoWidget An array of subpaths within the object that should be replaced with Lumino widgets. Each subpath is a string representing the path to the property (e.g., 'a.b.c').
337
+
* @param toObject An array of subpaths within the object that should be replaced with plain JavaScript objects. Each subpath is a string representing the path to the property (e.g., 'a.b.c').
338
+
*
339
+
* @remarks
340
+
* The `toLuminoWidget` replacements are performed first.
341
+
* The `getNestedProperty` function is used to retrieve the value at the given subpath.
342
+
* The `IpylabModel.toLuminoWidget` method is used to convert the value to a Lumino widget.
343
+
* The `setNestedProperty` function is used to set the new widget value at the given subpath.
291
344
*
292
-
* @param obj The object (map) with elements to be replaced.
293
-
* @param toLuminoWidget A list of elements to replace with widgets.
294
-
* @param toObject A list of elements to replace with objects.
345
+
* For `toObject` replacements, the `toBaseAndSubpath` method is used to split the value into a base and subpath.
346
+
* The `IpylabModel.toObject` method is then used to convert the value to a plain JavaScript object.
347
+
* Finally, the `setNestedProperty` function is used to set the new object value at the given subpath.
295
348
*/
296
349
privateasyncreplaceParts(
297
350
obj: Map<string,any>,
@@ -322,13 +375,14 @@ export class IpylabModel extends DOMWidgetModel {
322
375
}
323
376
324
377
/**
325
-
* Get the base and subpath from value.
378
+
* Converts a value, which can be a string or an array, into a base and subpath.
326
379
*
327
-
* When `default_basename` will be used when `value` is a string.
380
+
* If the value is an array, the first element is treated as the basename and the second as the subpath.
381
+
* If the value is a string, it's treated as the subpath, and a default basename is used.
328
382
*
329
-
* @param value can be either [basename, subpath] or subpath.
330
-
* @param defaultBasename A basename to use when value is just a subpath.
331
-
* @returns[base, subpath]
383
+
* @param value The input value, which can be a string or an array of strings.
384
+
* @param defaultBasename The default basename to use if the value is a string. Defaults to 'base'.
385
+
* @returnsA tuple containing the base (obtained from `this.getBase` using the basename) and the subpath.
332
386
*/
333
387
privatetoBaseAndSubpath(
334
388
value: string|Array<string>,
@@ -360,10 +414,14 @@ export class IpylabModel extends DOMWidgetModel {
360
414
}
361
415
362
416
/**
363
-
* Transform the object for sending.
364
-
* @param base
365
-
* @param args The mode as a string or an object with mode and any other parameters.
366
-
* @returns
417
+
* Transforms an object based on the specified transformation type.
418
+
*
419
+
* @param obj The object to transform.
420
+
* @param args The transformation arguments. If a string, it's treated as the transformation type.
421
+
* If an object, it should contain a `transform` property specifying the transformation type,
422
+
* and other properties relevant to the specific transformation.
423
+
* @returns A promise resolving to the transformed object.
424
+
* @throws Error if an invalid transformation type is provided.
@@ -471,17 +529,19 @@ export class IpylabModel extends DOMWidgetModel {
471
529
returnwidget;
472
530
}
473
531
532
+
474
533
/**
475
-
* Returns the object for the subpath 'value'.
476
-
* 1. If value starts with IPY_MODEL_ it will ignore the base, instead
477
-
* unpacking the model and return the object relative to subpath after
478
-
* the model name. If there is no path after the model id it will be the model.
479
-
* 2. The object as specified by the subpath relate to the base will be returned.
480
-
* 3. An error will be thrown if the value doesn't point an existing attribute.
534
+
* Converts a value to an object, handling special cases for strings starting with 'IPY_MODEL_'.
535
+
*
536
+
* If the value is a string, it checks if it starts with 'IPY_MODEL_'. If so, it extracts the model ID and subpath,
537
+
* retrieves the widget model using `IpylabModel.JFEM.getWidgetModel`, and resolves nested properties using `getNestedProperty`.
538
+
* If the value is not a string, it throws an error.
481
539
*
482
-
* @param obj The object to locate the dotted value.
483
-
* @param value The subpath on the base (except for IPY_MODEL_).
484
-
* @param nullIfMissing Return a null instead of throwing an error if missing.
540
+
* @param obj The initial object to traverse, can be a widget model.
541
+
* @param value The value to convert to an object. If it's a string starting with 'IPY_MODEL_', it's treated as a reference to a widget model and a subpath.
542
+
* @param nullIfMissing Optional. If true, returns null if the nested property is missing. Defaults to false.
543
+
* @returns A promise that resolves to the object or nested property.
0 commit comments