Skip to content

Commit 8c84dcd

Browse files
committed
Enhance documentation for IpylabModel methods, clarifying message handling and transformation processes using gemini
1 parent 60ceac3 commit 8c84dcd

1 file changed

Lines changed: 92 additions & 32 deletions

File tree

src/widgets/ipylab.ts

Lines changed: 92 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -217,20 +217,38 @@ export class IpylabModel extends DOMWidgetModel {
217217
}
218218
}
219219

220+
/**
221+
* Handles custom messages received from the Python backend.
222+
*
223+
* @param msg The message received from the backend.
224+
*/
220225
protected onCustomMessage(msg: any) {
221226
if (msg.ipylab) {
222227
this._onBackendMessage(JSON.parse(msg.ipylab));
223228
}
224229
}
225230

226231
/**
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`.
232241
*
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.
234252
*/
235253
private async _onBackendMessage(content: any) {
236254
if (content.ipylab_FE) {
@@ -258,8 +276,35 @@ export class IpylabModel extends DOMWidgetModel {
258276
}
259277

260278
/**
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.
263308
*/
264309
private async doOperationForPython(content: any) {
265310
const { operation, ipylab_PY, transform } = content;
@@ -285,13 +330,21 @@ export class IpylabModel extends DOMWidgetModel {
285330
}
286331

287332
/**
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.
291344
*
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.
295348
*/
296349
private async replaceParts(
297350
obj: Map<string, any>,
@@ -322,13 +375,14 @@ export class IpylabModel extends DOMWidgetModel {
322375
}
323376

324377
/**
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.
326379
*
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.
328382
*
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+
* @returns A tuple containing the base (obtained from `this.getBase` using the basename) and the subpath.
332386
*/
333387
private toBaseAndSubpath(
334388
value: string | Array<string>,
@@ -360,10 +414,14 @@ export class IpylabModel extends DOMWidgetModel {
360414
}
361415

362416
/**
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.
367425
*/
368426
private async transformObject(obj: any, args: string | any): Promise<any> {
369427
const transform = typeof args === 'string' ? args : args.transform;
@@ -471,17 +529,19 @@ export class IpylabModel extends DOMWidgetModel {
471529
return widget;
472530
}
473531

532+
474533
/**
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.
481539
*
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.
544+
* @throws Error if the value is not a string.
485545
*/
486546
static async toObject(
487547
obj: any,

0 commit comments

Comments
 (0)