Skip to content

Commit

Permalink
hook up comm close + more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
seeM committed Jul 5, 2024
1 parent 2db7c0b commit c9de7ce
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 10 deletions.
1 change: 1 addition & 0 deletions extensions/positron-ipywidgets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"dependencies": {
"@jupyter-widgets/base-manager": "^1.0.8",
"@jupyter-widgets/html-manager": "^1.0.10",
"@jupyterlab/services": "^7.2.3",
"@lumino/coreutils": "^2.1.2",
"@lumino/widgets": "^2.3.2"
},
Expand Down
52 changes: 45 additions & 7 deletions extensions/positron-ipywidgets/preload/comm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
import * as base from '@jupyter-widgets/base';
import { JSONObject, JSONValue, UUID } from '@lumino/coreutils';
import { Disposable } from 'vscode-notebook-renderer/events';
import type { IIPyWidgetsMessaging, ICommMessage } from '../../../src/vs/workbench/contrib/positronIPyWidgets/browser/positronIPyWidgetsMessaging.js';
import type { IIPyWidgetsMessaging, ICommMessage, ICommClose } from '../../../src/vs/workbench/contrib/positronIPyWidgets/browser/positronIPyWidgetsMessaging.js';
import { KernelMessage } from '@jupyterlab/services';

/**
* An IClassicComm that interfaces with the main thread Positron IPyWidgets service.
*/
export class Comm implements base.IClassicComm, Disposable {
private _disposables: Disposable[] = [];
private _on_msg: ((x: any) => void) | undefined;
private _on_close: ((x: any) => void) | undefined;
private _on_msg: ((x: KernelMessage.ICommMsgMsg) => void) | undefined;
private _on_close: ((x: KernelMessage.ICommCloseMsg) => void) | undefined;
private _callbacks = new Map<string, base.ICallbacks>();

/**
Expand Down Expand Up @@ -149,7 +150,26 @@ export class Comm implements base.IClassicComm, Disposable {
*/
private handle_msg(message: ICommMessage): void {
console.log('Comm.handle_msg', message);
this._on_msg?.(message);
this._on_msg?.({
content: {
comm_id: this.comm_id,
data: message.content.data,
},
// Stub the rest of the interface - these are not currently used by widget libraries.
channel: 'iopub',
header: {
date: '',
msg_id: message.msg_id ?? '',
msg_type: message.type,
session: '',
username: '',
version: '',
},
parent_header: {
// msg_id: message.msg_id
},
metadata: {},
});

// Simulate an 'idle' status message after an RPC response is received from the runtime.
const msgId = message.msg_id;
Expand All @@ -159,7 +179,9 @@ export class Comm implements base.IClassicComm, Disposable {
if (callbacks) {
// Call the iopub.status callback with a stubbed 'idle' status message.
callbacks.iopub?.status?.({
content: { execution_state: 'idle' },
content: {
execution_state: 'idle'
},
// Stub the rest of the interface - these are not currently used by widget libraries.
channel: 'iopub',
header: {
Expand All @@ -182,9 +204,25 @@ export class Comm implements base.IClassicComm, Disposable {
*
* @param message The close message.
*/
private handle_close(message: any): void {
private handle_close(message: ICommClose): void {
console.log('Comm.handle_close', message);
this._on_close?.(message);
this._on_close?.({
content: {
comm_id: this.comm_id,
data: {},
},
channel: 'shell',
header: {
date: '',
msg_id: '',
msg_type: 'comm_close',
session: '',
username: '',
version: '',
},
parent_header: {},
metadata: {},
});
}

dispose(): void {
Expand Down
9 changes: 7 additions & 2 deletions extensions/positron-ipywidgets/preload/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,14 @@ export class PositronWidgetManager extends ManagerBase implements base.IWidgetMa
await this.handle_comm_open(
comm,
{
...message,
channel: 'shell',
content: {
comm_id: message.comm_id,
target_name: message.target_name,
data: message.content,
},
metadata: message.metadata,
// Stub the rest of the interface - these are not currently used by handle_comm_open.
channel: 'iopub',
header: {
date: '',
msg_id: '',
Expand Down
74 changes: 74 additions & 0 deletions extensions/positron-ipywidgets/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@
y-protocols "^1.0.5"
yjs "^13.5.40"

"@jupyter/ydoc@^2.0.1":
version "2.0.1"
resolved "https://registry.yarnpkg.com/@jupyter/ydoc/-/ydoc-2.0.1.tgz#d3ee3363a8bc3dcbc97177eceda6d60390f25876"
integrity sha512-HyJPi7dHEWqxBqfjU+QqY/ks5RpDPYFl8QtbBYQ56WRN6nOvI/QOnDUCTTRU9p+X38IDqM+Rym+SLVn0qLppzg==
dependencies:
"@jupyterlab/nbformat" "^3.0.0 || ^4.0.0-alpha.21 || ^4.0.0"
"@lumino/coreutils" "^1.11.0 || ^2.0.0"
"@lumino/disposable" "^1.10.0 || ^2.0.0"
"@lumino/signaling" "^1.10.0 || ^2.0.0"
y-protocols "^1.0.5"
yjs "^13.5.40"

"@jupyterlab/apputils@^4.2.6":
version "4.2.6"
resolved "https://registry.yarnpkg.com/@jupyterlab/apputils/-/apputils-4.2.6.tgz#bdf994ffbf28cefb43969bfb3501bcc9bbcb324e"
Expand Down Expand Up @@ -150,13 +162,32 @@
path-browserify "^1.0.0"
url-parse "~1.5.4"

"@jupyterlab/coreutils@^6.2.3":
version "6.2.3"
resolved "https://registry.yarnpkg.com/@jupyterlab/coreutils/-/coreutils-6.2.3.tgz#5f63a9a3e0714a90e66e6d583939a75d38a8894c"
integrity sha512-tGABmtHpBxgRPBg66SbuN54aVmgYYmfFqUoTURBbhu8ifbzX4FXhzzq23At36+jspQam/9U/cFnqBNBh8d40pQ==
dependencies:
"@lumino/coreutils" "^2.1.2"
"@lumino/disposable" "^2.1.2"
"@lumino/signaling" "^2.1.2"
minimist "~1.2.0"
path-browserify "^1.0.0"
url-parse "~1.5.4"

"@jupyterlab/nbformat@^3.0.0 || ^4.0.0-alpha.21 || ^4.0.0", "@jupyterlab/nbformat@^4.1.6":
version "4.1.6"
resolved "https://registry.yarnpkg.com/@jupyterlab/nbformat/-/nbformat-4.1.6.tgz#8ae366a3e74392cddb4633625ee5e0777b997149"
integrity sha512-wh/lw8+WciC/8kB25A8Ma27VvR6aR/TcrO4pL4nqCRI61YV0s/HbVL/0xug+qnGLRCNu6AfVYNEWPY2yTQguAw==
dependencies:
"@lumino/coreutils" "^2.1.2"

"@jupyterlab/nbformat@^4.2.3":
version "4.2.3"
resolved "https://registry.yarnpkg.com/@jupyterlab/nbformat/-/nbformat-4.2.3.tgz#6b7904a387f04542b8ffe7800d37a2ce41b625a9"
integrity sha512-eNn5F4JGw6Gq8xB1AaJ29hKQFgDvcjv9KZQeSp9joCYvedmc0LHivELCO+GticUjN5Y0+qgS4ZvnOARAqcIyvQ==
dependencies:
"@lumino/coreutils" "^2.1.2"

"@jupyterlab/observables@^5.1.6":
version "5.1.6"
resolved "https://registry.yarnpkg.com/@jupyterlab/observables/-/observables-5.1.6.tgz#d5ef4f800a108c2faa93c6b97e3a40da541063dc"
Expand Down Expand Up @@ -231,6 +262,23 @@
"@lumino/signaling" "^2.1.2"
ws "^8.11.0"

"@jupyterlab/services@^7.2.3":
version "7.2.3"
resolved "https://registry.yarnpkg.com/@jupyterlab/services/-/services-7.2.3.tgz#06af2069846093464aa9dd170ba55c86f8ad9746"
integrity sha512-Szx0FQ9Lehf/yrMOqVL1E5ZXDL8T97VgQqjfDkZx9xrA0SR7Ib7dMb3/8JF+IzV7eX5ycxT8U+LvPTGj0MOE2w==
dependencies:
"@jupyter/ydoc" "^2.0.1"
"@jupyterlab/coreutils" "^6.2.3"
"@jupyterlab/nbformat" "^4.2.3"
"@jupyterlab/settingregistry" "^4.2.3"
"@jupyterlab/statedb" "^4.2.3"
"@lumino/coreutils" "^2.1.2"
"@lumino/disposable" "^2.1.2"
"@lumino/polling" "^2.1.2"
"@lumino/properties" "^2.0.1"
"@lumino/signaling" "^2.1.2"
ws "^8.11.0"

"@jupyterlab/settingregistry@^4.1.6":
version "4.1.6"
resolved "https://registry.yarnpkg.com/@jupyterlab/settingregistry/-/settingregistry-4.1.6.tgz#845217ab6c6829ec48f66fab6dd4c2ba18e0fbfd"
Expand All @@ -246,6 +294,21 @@
ajv "^8.12.0"
json5 "^2.2.3"

"@jupyterlab/settingregistry@^4.2.3":
version "4.2.3"
resolved "https://registry.yarnpkg.com/@jupyterlab/settingregistry/-/settingregistry-4.2.3.tgz#4a1ee44422cc3f509202a6f8c954f2b673e12050"
integrity sha512-f5rNTd7wk/fn1C0ylD0/jDmMVvxgYYqOfPktaOAeWckS4iQ44XcrqfPUWs8sg66JYBHBFXBD9OkUKiMteQZDZg==
dependencies:
"@jupyterlab/nbformat" "^4.2.3"
"@jupyterlab/statedb" "^4.2.3"
"@lumino/commands" "^2.3.0"
"@lumino/coreutils" "^2.1.2"
"@lumino/disposable" "^2.1.2"
"@lumino/signaling" "^2.1.2"
"@rjsf/utils" "^5.13.4"
ajv "^8.12.0"
json5 "^2.2.3"

"@jupyterlab/statedb@^4.1.6":
version "4.1.6"
resolved "https://registry.yarnpkg.com/@jupyterlab/statedb/-/statedb-4.1.6.tgz#73c27c83ac651c371500be327db088ce8a7c4eab"
Expand All @@ -257,6 +320,17 @@
"@lumino/properties" "^2.0.1"
"@lumino/signaling" "^2.1.2"

"@jupyterlab/statedb@^4.2.3":
version "4.2.3"
resolved "https://registry.yarnpkg.com/@jupyterlab/statedb/-/statedb-4.2.3.tgz#9b5fc15bca1bd22025aa46e13574bc47bc23f03f"
integrity sha512-fHWkhMdz3U5ZCJmxsze39UyJnVaMnbRrHIhKh5nvwAQdg2h4KCP1Yg/EKKbsfmKYMgwE8YA5tCU7OmBuiyzKyg==
dependencies:
"@lumino/commands" "^2.3.0"
"@lumino/coreutils" "^2.1.2"
"@lumino/disposable" "^2.1.2"
"@lumino/properties" "^2.0.1"
"@lumino/signaling" "^2.1.2"

"@jupyterlab/statusbar@^4.1.6":
version "4.1.6"
resolved "https://registry.yarnpkg.com/@jupyterlab/statusbar/-/statusbar-4.1.6.tgz#3ae81ad356d38cdc1813b1e000726b4c661591e4"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class IPyWidgetsInstance extends Disposable {
this._clients.set(client.getClientId(), ipywidgetsClient);
}));

// Notify the editor to append the bundled widgets stylesheet.
this._extensionService.getExtension('vscode.positron-ipywidgets').then((extension) => {
if (!extension) {
throw new Error('positron-ipywidgets extension not found');
Expand Down Expand Up @@ -235,6 +236,10 @@ class IPyWidgetsClientInstance extends Disposable {
const stateChangeEvent = Event.fromObservable(_client.clientState);
this._register(stateChangeEvent(state => {
if (state === RuntimeClientState.Closed) {
this._messaging.postMessage({
type: 'comm_close',
comm_id: this._client.getClientId(),
});
this._closeEmitter.fire();
}
}));
Expand All @@ -244,7 +249,7 @@ class IPyWidgetsClientInstance extends Disposable {
type: 'comm_open',
comm_id: this._client.getClientId(),
target_name: this._client.getClientType(),
content: { data: message.data },
content: message.data,
metadata: message.metadata,
});
}
Expand Down

0 comments on commit c9de7ce

Please sign in to comment.