Skip to content

Commit

Permalink
Lazy inspection
Browse files Browse the repository at this point in the history
Do not request anything from the kernel if the variable inspector is not
currently open
  • Loading branch information
martinRenou committed Aug 29, 2024
1 parent 7168804 commit 2fed5c8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 31 deletions.
15 changes: 15 additions & 0 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,19 @@ abstract class AbstractHandler implements IVariableInspector.IInspectable {
>(this);
protected _connector: KernelConnector;
protected _rendermime: IRenderMimeRegistry | null = null;
private _enabled: boolean;

constructor(connector: KernelConnector) {
this._connector = connector;
this._enabled = false;
}

get enabled(): boolean {
return this._enabled;
}

set enabled(value: boolean) {
this._enabled = value;
}

get disposed(): ISignal<this, void> {
Expand Down Expand Up @@ -138,10 +148,15 @@ export class VariableInspectionHandler extends AbstractHandler {
get ready(): Promise<void> {
return this._ready;
}

/**
* Performs an inspection by sending an execute request with the query command to the kernel.
*/
performInspection(): void {
if (!this.enabled) {
return;
}

const content: KernelMessage.IExecuteRequestMsg['content'] = {
code: this._queryCommand,
stop_on_error: false,
Expand Down
11 changes: 1 addition & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { ICommandPalette, WidgetTracker } from '@jupyterlab/apputils';

import {
ILabShell,
ILayoutRestorer,
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';
Expand Down Expand Up @@ -35,13 +34,12 @@ namespace CommandIDs {
*/
const variableinspector: JupyterFrontEndPlugin<IVariableInspectorManager> = {
id: '@lckr/jupyterlab_variableinspector',
requires: [ICommandPalette, ILayoutRestorer, ILabShell],
requires: [ICommandPalette, ILabShell],
provides: IVariableInspectorManager,
autoStart: true,
activate: (
app: JupyterFrontEnd,
palette: ICommandPalette,
restorer: ILayoutRestorer,
labShell: ILabShell
): IVariableInspectorManager => {
const manager = new VariableInspectorManager();
Expand Down Expand Up @@ -73,13 +71,6 @@ const variableinspector: JupyterFrontEndPlugin<IVariableInspectorManager> = {
return panel;
}

// Enable state restoration
restorer.restore(tracker, {
command,
args: () => ({}),
name: () => 'variableinspector'
});

// Add command to palette
app.commands.addCommand(command, {
label,
Expand Down
42 changes: 21 additions & 21 deletions src/inspectorscripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@ def _jupyterlab_variableinspector_dict_list():
vardic = [
{
'varName': _v,
'varType': type(eval(_v)).__name__,
'varSize': str(_jupyterlab_variableinspector_getsizeof(eval(_v))),
'varShape': str(_jupyterlab_variableinspector_getshapeof(eval(_v))) if _jupyterlab_variableinspector_getshapeof(eval(_v)) else '',
'varContent': str(_jupyterlab_variableinspector_getcontentof(eval(_v))),
'varType': type(eval(_v)).__name__,
'varSize': str(_jupyterlab_variableinspector_getsizeof(eval(_v))),
'varShape': str(_jupyterlab_variableinspector_getshapeof(eval(_v))) if _jupyterlab_variableinspector_getshapeof(eval(_v)) else '',
'varContent': str(_jupyterlab_variableinspector_getcontentof(eval(_v))),
'isMatrix': _jupyterlab_variableinspector_is_matrix(eval(_v)),
'isWidget': _jupyterlab_variableinspector_is_widget(type(eval(_v)))
}
Expand Down Expand Up @@ -226,7 +226,7 @@ def _jupyterlab_variableinspector_displaywidget(widget):
def _jupyterlab_variableinspector_default(o):
if isinstance(o, __np.number): return int(o)
if isinstance(o, __np.number): return int(o)
raise TypeError
Expand All @@ -236,10 +236,10 @@ def _jupyterlab_variableinspector_deletevariable(x):

static r_script = `library(repr)
.ls.objects = function (pos = 1, pattern, order.by, decreasing = FALSE, head = FALSE,
n = 5)
.ls.objects = function (pos = 1, pattern, order.by, decreasing = FALSE, head = FALSE,
n = 5)
{
napply <- function(names, fn) sapply(names, function(x) fn(get(x,
napply <- function(names, fn) sapply(names, function(x) fn(get(x,
pos = pos)))
names <- ls(pos = pos, pattern = pattern)
if (length(names) == 0) {
Expand All @@ -251,11 +251,11 @@ def _jupyterlab_variableinspector_deletevariable(x):
obj.size <- napply(names, object.size)
obj.dim <- t(napply(names, function(x) as.numeric(dim(x))[1:2]))
obj.content <- rep("NA", length(names))
has_no_dim <- is.na(obj.dim)[1:length(names)]
has_no_dim <- is.na(obj.dim)[1:length(names)]
obj.dim[has_no_dim, 1] <- napply(names, length)[has_no_dim]
vec <- (obj.type != "function")
obj.content[vec] <- napply(names[vec], function(x) toString(x, width = 154)[1])
obj.rownames <- napply(names, rownames)
has_rownames <- obj.rownames != "NULL"
obj.rownames <- sapply(obj.rownames[has_rownames], function(x) paste(x,
Expand All @@ -264,24 +264,24 @@ def _jupyterlab_variableinspector_deletevariable(x):
obj.rownames <- ifelse(nchar(obj.rownames) > 154, obj.rownames.short, obj.rownames)
obj.rownames <- sapply(obj.rownames, function(x) paste("Row names: ",x))
obj.content[has_rownames] <- obj.rownames
obj.colnames <- napply(names, colnames)
has_colnames <- obj.colnames != "NULL"
obj.colnames <- sapply(obj.colnames[has_colnames], function(x) paste(x,
obj.colnames <- sapply(obj.colnames[has_colnames], function(x) paste(x,
collapse = ", "))
obj.colnames.short <- sapply(obj.colnames, function(x) paste(substr(x,
obj.colnames.short <- sapply(obj.colnames, function(x) paste(substr(x,
1, 150), "...."))
obj.colnames <- ifelse(nchar(obj.colnames) > 154, obj.colnames.short,
obj.colnames <- ifelse(nchar(obj.colnames) > 154, obj.colnames.short,
obj.colnames)
obj.colnames <- sapply(obj.colnames, function(x) paste("Column names: ",x))
obj.content[has_colnames] <- obj.colnames
is_function <- (obj.type == "function")
obj.content[is_function] <- napply(names[is_function], function(x) paste(strsplit(repr_text(x),")")[[1]][1],")",sep=""))
obj.content <- unlist(obj.content, use.names = FALSE)
out <- data.frame(obj.type, obj.size, obj.dim)
names(out) <- c("varType", "varSize", "Rows", "Columns")
Expand All @@ -292,10 +292,10 @@ def _jupyterlab_variableinspector_deletevariable(x):
out <- out[, !(names(out) %in% c("Rows", "Columns"))]
rownames(out) <- NULL
print(out)
if (!missing(order.by))
out <- out[order(out[[order.by]], decreasing = decreasing),
if (!missing(order.by))
out <- out[order(out[[order.by]], decreasing = decreasing),
]
if (head)
if (head)
out <- head(out, n)
jsonlite::toJSON(out)
}
Expand Down
1 change: 1 addition & 0 deletions src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export namespace IVariableInspector {
export interface IInspectable extends IObservableDisposable {
inspected: ISignal<IInspectable, IVariableInspectorUpdate>;
rendermime: IRenderMimeRegistry | null;
enabled: boolean;
performInspection(): void;
performMatrixInspection(
varName: string,
Expand Down
13 changes: 13 additions & 0 deletions src/variableinspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ provideJupyterDesignSystem().register(
);

import wildcardMatch from 'wildcard-match';
import { Message } from '@lumino/messaging';

const TITLE_CLASS = 'jp-VarInspector-title';
const PANEL_CLASS = 'jp-VarInspector';
Expand Down Expand Up @@ -233,12 +234,14 @@ export class VariableInspectorPanel
}
//Remove old subscriptions
if (this._source) {
this._source.enabled = false;
this._source.inspected.disconnect(this.onInspectorUpdate, this);
this._source.disposed.disconnect(this.onSourceDisposed, this);
}
this._source = source;
//Subscribe to new object
if (this._source) {
this._source.enabled = true;
this._source.inspected.connect(this.onInspectorUpdate, this);
this._source.disposed.connect(this.onSourceDisposed, this);
this._source.performInspection();
Expand All @@ -252,10 +255,20 @@ export class VariableInspectorPanel
if (this.isDisposed) {
return;
}
if (this.source) {
this.source.enabled = false;
}
this.source = null;
super.dispose();
}

protected onCloseRequest(msg: Message): void {
super.onCloseRequest(msg);
if (this._source) {
this._source.enabled = false;
}
}

protected onInspectorUpdate(
sender: any,
allArgs: IVariableInspector.IVariableInspectorUpdate
Expand Down

0 comments on commit 2fed5c8

Please sign in to comment.