Skip to content

Commit 8bc6e57

Browse files
authored
Merge pull request #309 from martinRenou/disable_inspection_when_ui_not_open
Lazy inspection
2 parents 7cdba26 + be01506 commit 8bc6e57

File tree

4 files changed

+58
-21
lines changed

4 files changed

+58
-21
lines changed

src/handler.ts

+15
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,19 @@ abstract class AbstractHandler implements IVariableInspector.IInspectable {
2929
>(this);
3030
protected _connector: KernelConnector;
3131
protected _rendermime: IRenderMimeRegistry | null = null;
32+
private _enabled: boolean;
3233

3334
constructor(connector: KernelConnector) {
3435
this._connector = connector;
36+
this._enabled = false;
37+
}
38+
39+
get enabled(): boolean {
40+
return this._enabled;
41+
}
42+
43+
set enabled(value: boolean) {
44+
this._enabled = value;
3545
}
3646

3747
get disposed(): ISignal<this, void> {
@@ -138,10 +148,15 @@ export class VariableInspectionHandler extends AbstractHandler {
138148
get ready(): Promise<void> {
139149
return this._ready;
140150
}
151+
141152
/**
142153
* Performs an inspection by sending an execute request with the query command to the kernel.
143154
*/
144155
performInspection(): void {
156+
if (!this.enabled) {
157+
return;
158+
}
159+
145160
const content: KernelMessage.IExecuteRequestMsg['content'] = {
146161
code: this._queryCommand,
147162
stop_on_error: false,

src/inspectorscripts.ts

+21-21
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,10 @@ def _jupyterlab_variableinspector_dict_list():
176176
vardic = [
177177
{
178178
'varName': _v,
179-
'varType': type(eval(_v)).__name__,
180-
'varSize': str(_jupyterlab_variableinspector_getsizeof(eval(_v))),
181-
'varShape': str(_jupyterlab_variableinspector_getshapeof(eval(_v))) if _jupyterlab_variableinspector_getshapeof(eval(_v)) else '',
182-
'varContent': str(_jupyterlab_variableinspector_getcontentof(eval(_v))),
179+
'varType': type(eval(_v)).__name__,
180+
'varSize': str(_jupyterlab_variableinspector_getsizeof(eval(_v))),
181+
'varShape': str(_jupyterlab_variableinspector_getshapeof(eval(_v))) if _jupyterlab_variableinspector_getshapeof(eval(_v)) else '',
182+
'varContent': str(_jupyterlab_variableinspector_getcontentof(eval(_v))),
183183
'isMatrix': _jupyterlab_variableinspector_is_matrix(eval(_v)),
184184
'isWidget': _jupyterlab_variableinspector_is_widget(type(eval(_v)))
185185
}
@@ -226,7 +226,7 @@ def _jupyterlab_variableinspector_displaywidget(widget):
226226
227227
228228
def _jupyterlab_variableinspector_default(o):
229-
if isinstance(o, __np.number): return int(o)
229+
if isinstance(o, __np.number): return int(o)
230230
raise TypeError
231231
232232
@@ -236,10 +236,10 @@ def _jupyterlab_variableinspector_deletevariable(x):
236236

237237
static r_script = `library(repr)
238238
239-
.ls.objects = function (pos = 1, pattern, order.by, decreasing = FALSE, head = FALSE,
240-
n = 5)
239+
.ls.objects = function (pos = 1, pattern, order.by, decreasing = FALSE, head = FALSE,
240+
n = 5)
241241
{
242-
napply <- function(names, fn) sapply(names, function(x) fn(get(x,
242+
napply <- function(names, fn) sapply(names, function(x) fn(get(x,
243243
pos = pos)))
244244
names <- ls(pos = pos, pattern = pattern)
245245
if (length(names) == 0) {
@@ -251,11 +251,11 @@ def _jupyterlab_variableinspector_deletevariable(x):
251251
obj.size <- napply(names, object.size)
252252
obj.dim <- t(napply(names, function(x) as.numeric(dim(x))[1:2]))
253253
obj.content <- rep("NA", length(names))
254-
has_no_dim <- is.na(obj.dim)[1:length(names)]
254+
has_no_dim <- is.na(obj.dim)[1:length(names)]
255255
obj.dim[has_no_dim, 1] <- napply(names, length)[has_no_dim]
256256
vec <- (obj.type != "function")
257257
obj.content[vec] <- napply(names[vec], function(x) toString(x, width = 154)[1])
258-
258+
259259
obj.rownames <- napply(names, rownames)
260260
has_rownames <- obj.rownames != "NULL"
261261
obj.rownames <- sapply(obj.rownames[has_rownames], function(x) paste(x,
@@ -264,24 +264,24 @@ def _jupyterlab_variableinspector_deletevariable(x):
264264
obj.rownames <- ifelse(nchar(obj.rownames) > 154, obj.rownames.short, obj.rownames)
265265
obj.rownames <- sapply(obj.rownames, function(x) paste("Row names: ",x))
266266
obj.content[has_rownames] <- obj.rownames
267-
268-
267+
268+
269269
obj.colnames <- napply(names, colnames)
270270
has_colnames <- obj.colnames != "NULL"
271-
obj.colnames <- sapply(obj.colnames[has_colnames], function(x) paste(x,
271+
obj.colnames <- sapply(obj.colnames[has_colnames], function(x) paste(x,
272272
collapse = ", "))
273-
obj.colnames.short <- sapply(obj.colnames, function(x) paste(substr(x,
273+
obj.colnames.short <- sapply(obj.colnames, function(x) paste(substr(x,
274274
1, 150), "...."))
275-
obj.colnames <- ifelse(nchar(obj.colnames) > 154, obj.colnames.short,
275+
obj.colnames <- ifelse(nchar(obj.colnames) > 154, obj.colnames.short,
276276
obj.colnames)
277277
obj.colnames <- sapply(obj.colnames, function(x) paste("Column names: ",x))
278-
278+
279279
obj.content[has_colnames] <- obj.colnames
280-
280+
281281
is_function <- (obj.type == "function")
282282
obj.content[is_function] <- napply(names[is_function], function(x) paste(strsplit(repr_text(x),")")[[1]][1],")",sep=""))
283283
obj.content <- unlist(obj.content, use.names = FALSE)
284-
284+
285285
286286
out <- data.frame(obj.type, obj.size, obj.dim)
287287
names(out) <- c("varType", "varSize", "Rows", "Columns")
@@ -292,10 +292,10 @@ def _jupyterlab_variableinspector_deletevariable(x):
292292
out <- out[, !(names(out) %in% c("Rows", "Columns"))]
293293
rownames(out) <- NULL
294294
print(out)
295-
if (!missing(order.by))
296-
out <- out[order(out[[order.by]], decreasing = decreasing),
295+
if (!missing(order.by))
296+
out <- out[order(out[[order.by]], decreasing = decreasing),
297297
]
298-
if (head)
298+
if (head)
299299
out <- head(out, n)
300300
jsonlite::toJSON(out)
301301
}

src/tokens.ts

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export namespace IVariableInspector {
3838
export interface IInspectable extends IObservableDisposable {
3939
inspected: ISignal<IInspectable, IVariableInspectorUpdate>;
4040
rendermime: IRenderMimeRegistry | null;
41+
enabled: boolean;
4142
performInspection(): void;
4243
performMatrixInspection(
4344
varName: string,

src/variableinspector.ts

+21
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ provideJupyterDesignSystem().register(
3636
);
3737

3838
import wildcardMatch from 'wildcard-match';
39+
import { Message } from '@lumino/messaging';
3940

4041
const TITLE_CLASS = 'jp-VarInspector-title';
4142
const PANEL_CLASS = 'jp-VarInspector';
@@ -233,12 +234,14 @@ export class VariableInspectorPanel
233234
}
234235
//Remove old subscriptions
235236
if (this._source) {
237+
this._source.enabled = false;
236238
this._source.inspected.disconnect(this.onInspectorUpdate, this);
237239
this._source.disposed.disconnect(this.onSourceDisposed, this);
238240
}
239241
this._source = source;
240242
//Subscribe to new object
241243
if (this._source) {
244+
this._source.enabled = true;
242245
this._source.inspected.connect(this.onInspectorUpdate, this);
243246
this._source.disposed.connect(this.onSourceDisposed, this);
244247
this._source.performInspection();
@@ -252,10 +255,28 @@ export class VariableInspectorPanel
252255
if (this.isDisposed) {
253256
return;
254257
}
258+
if (this.source) {
259+
this.source.enabled = false;
260+
}
255261
this.source = null;
256262
super.dispose();
257263
}
258264

265+
protected onCloseRequest(msg: Message): void {
266+
super.onCloseRequest(msg);
267+
if (this._source) {
268+
this._source.enabled = false;
269+
}
270+
}
271+
272+
protected onAfterShow(msg: Message): void {
273+
super.onAfterShow(msg);
274+
if (this._source) {
275+
this._source.enabled = true;
276+
this._source.performInspection();
277+
}
278+
}
279+
259280
protected onInspectorUpdate(
260281
sender: any,
261282
allArgs: IVariableInspector.IVariableInspectorUpdate

0 commit comments

Comments
 (0)