Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove iframe when kernel is killed or restarted #22

Merged
merged 4 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion deephaven_ipywidgets/deephaven.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@
"""
Module for displaying Deephaven widgets within interactive python environments.
"""
from __future__ import annotations
jnumainville marked this conversation as resolved.
Show resolved Hide resolved

import __main__
from ipywidgets import DOMWidget
from traitlets import Unicode, Integer, Bytes
from traitlets import Unicode, Integer, Bytes, Bool
from deephaven_server import Server
from uuid import uuid4
from ._frontend import module_name, module_version
import os
import base64
import atexit


def _str_object_type(obj):
Expand All @@ -34,6 +36,15 @@ def _path_for_object(obj):
raise TypeError(f"Unknown object type: {name}")


def _cleanup(widget: DeephavenWidget):
"""
Remove the widget when the kernel is shutdown

Args:
widget (DeephavenWidget): The widget to remove
"""
widget.set_trait('kernel_active', False)


class DeephavenWidget(DOMWidget):
"""A wrapper for viewing DeephavenWidgets in IPython
Expand All @@ -52,6 +63,7 @@ class DeephavenWidget(DOMWidget):
width = Integer().tag(sync=True)
height = Integer().tag(sync=True)
token = Unicode().tag(sync=True)
kernel_active = Bool().tag(sync=True)

def __init__(self, deephaven_object, height=600, width=0):
"""Create a Deephaven widget for displaying in an interactive Python console.
Expand Down Expand Up @@ -122,3 +134,6 @@ def __init__(self, deephaven_object, height=600, width=0):
self.set_trait('width', width)
self.set_trait('height', height)
self.set_trait('token', token)
self.set_trait('kernel_active', True)

atexit.register(_cleanup, self)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"@babel/preset-env": "^7.5.0",
"@deephaven/eslint-config": "^0.41.0",
"@deephaven/prettier-config": "^0.41.0",
"@jupyterlab/builder": "^3.6.4",
"@jupyterlab/builder": "^4.0.5",
"@phosphor/application": "^1.6.0",
"@phosphor/widgets": "^1.6.0",
"@types/jest": "^26.0.0",
Expand Down
19 changes: 19 additions & 0 deletions src/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,33 @@
}
};

//@ts-ignore

Check failure on line 107 in src/widget.ts

View workflow job for this annotation

GitHub Actions / build

Do not use "@ts-ignore" because it alters compilation errors

Check failure on line 107 in src/widget.ts

View workflow job for this annotation

GitHub Actions / build

Expected exception block, space or tab after '//' in comment
onDisconnect = (type): void => {
log.info(`Kernel ${type}, removing iframe`);
this.iframe.remove();
}

Check failure on line 111 in src/widget.ts

View workflow job for this annotation

GitHub Actions / build

Insert `;`

// eslint-disable-next-line @typescript-eslint/no-explicit-any
render(): any {
const iframeUrl = this.model.get('iframe_url');
const width = this.model.get('width');
const height = this.model.get('height');
let removed = false;

window.addEventListener('message', this.handleAuthentication);

//@ts-ignore

Check failure on line 122 in src/widget.ts

View workflow job for this annotation

GitHub Actions / build

Do not use "@ts-ignore" because it alters compilation errors

Check failure on line 122 in src/widget.ts

View workflow job for this annotation

GitHub Actions / build

Expected exception block, space or tab after '//' in comment
const context = this.model.widget_manager._context;

Check failure on line 123 in src/widget.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected dangling '_' in '_context'

this.model.on('change:kernel_active', () => this.onDisconnect("exiting"), this)

Check failure on line 125 in src/widget.ts

View workflow job for this annotation

GitHub Actions / build

Replace `'change:kernel_active',·()·=>·this.onDisconnect("exiting"),·this)` with `⏎······'change:kernel_active',⏎······()·=>·this.onDisconnect('exiting'),⏎······this⏎····);`
jnumainville marked this conversation as resolved.
Show resolved Hide resolved

context.sessionContext.statusChanged.connect((sender: any, args: string) => {

Check failure on line 127 in src/widget.ts

View workflow job for this annotation

GitHub Actions / build

Insert `⏎······`

Check warning on line 127 in src/widget.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
jnumainville marked this conversation as resolved.
Show resolved Hide resolved
if (args === "restarting" || args === "terminating" && !removed) {

Check failure on line 128 in src/widget.ts

View workflow job for this annotation

GitHub Actions / build

Replace `if·(args·===·"restarting"·||·args·===·"terminating"·&&·!removed)` with `··if·(args·===·'restarting'·||·(args·===·'terminating'·&&·!removed))`
this.onDisconnect(args);

Check failure on line 129 in src/widget.ts

View workflow job for this annotation

GitHub Actions / build

Insert `··`
removed = true;
}
})

log.info('init_element for widget', iframeUrl, width, height);

this.iframe = document.createElement('iframe');
Expand Down