Skip to content

Commit

Permalink
Improved closing and kernel checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Fleming committed May 5, 2024
1 parent 837bc6e commit 81e0169
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
4 changes: 2 additions & 2 deletions examples/main_area.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"\n",
"\n",
"def on_click(_):\n",
" ma.load_console() if ma.console_status == \"unloaded\" else ma.unload_console()\n",
" ma.load_console(path=\"console\") if ma.console_status == \"unloaded\" else ma.unload_console()\n",
"\n",
"\n",
"console_button.on_click(on_click)\n",
Expand Down Expand Up @@ -148,7 +148,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
"version": "3.10.14"
}
},
"nbformat": 4,
Expand Down
1 change: 1 addition & 0 deletions ipylab/asyncwidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ def schedule_operation(
**kwgs: Keyword arguments for the frontend operation.
"""
# validation
self._check_closed()
if not operation or not isinstance(operation, str):
msg = f"Invalid {operation=}"
raise ValueError(msg)
Expand Down
9 changes: 4 additions & 5 deletions src/widgets/ipylab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ export class IpylabModel extends DOMWidgetModel {
}

get kernelLive() {
return !['dead'].includes((this.widget_manager as any).kernel.status);
const status = (this.widget_manager as any)?.kernel?.status;
return status ? !['dead'].includes(status) : false;
}

/**
Expand Down Expand Up @@ -247,10 +248,8 @@ export class IpylabModel extends DOMWidgetModel {
}

close(comm_closed?: boolean): Promise<void> {
comm_closed = comm_closed ?? !this.kernelLive;
if (!comm_closed) {
return super.close(comm_closed);
}
comm_closed = comm_closed || !this.kernelLive;
return super.close(comm_closed);
}

save_changes(callbacks?: unknown): void {
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/main_area.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export class MainAreaModel extends IpylabModel {
...options
}
);
// The console toobar takes up space and currently only provides a debugger
// The console toolbar takes up space and currently only provides a debugger
if (cp?.toolbar?.node) {
cp.node.removeChild(cp.toolbar.node);
}
Expand Down
15 changes: 8 additions & 7 deletions src/widgets/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,23 +349,24 @@ export function onKernelLost(
thisArg?: any,
onceOnly = true
) {
if (!Private.kernelLostSlot.has(kernel.id)) {
const id = kernel.id;
if (!Private.kernelLostSlot.has(id)) {
kernel.statusChanged.connect(_onKernelStatusChanged);
Private.kernelLostSlot.set(kernel.id, new Signal<any, null>(kernel));
Private.kernelLostSlot.set(id, new Signal<any, null>(kernel));
kernel.disposed.connect(() => {
Private.kernelLostSlot.get(kernel.id).emit(null);
Signal.clearData(Private.kernelLostSlot.get(kernel.id));
Private.kernelLostSlot.delete(kernel.id);
Private.kernelLostSlot.get(id).emit(null);
Signal.clearData(Private.kernelLostSlot.get(id));
Private.kernelLostSlot.delete(id);
kernel.statusChanged.disconnect(_onKernelStatusChanged);
});
}
const callback = () => {
slot.bind(thisArg)();
if (onceOnly) {
Private.kernelLostSlot.get(kernel.id)?.disconnect(callback);
Private.kernelLostSlot.get(id)?.disconnect(callback);
}
};
Private.kernelLostSlot.get(kernel.id).connect(callback);
Private.kernelLostSlot.get(id).connect(callback);
}

/**
Expand Down

0 comments on commit 81e0169

Please sign in to comment.