Skip to content

Some more performance improvements #313

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

Merged
merged 4 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"yjs": "^13.5.40"
},
"jupyterlab": {
"schemaDir": "schema",
"extension": true,
"outputDir": "lckr_jupyterlab_variableinspector/labextension",
"sharedPackages": {
Expand Down
15 changes: 15 additions & 0 deletions schema/jupyterlab-variableInspector-settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"jupyter.lab.setting-icon": "ui-components:list",
"title": "Variable Inspector",
"description": "Settings for the jupyterlab-variableInspector extension.",
"type": "object",
"properties": {
"maxItems": {
"type": "number",
"minimum": 0,
"title": "Maximum number of items",
"description": "The maximum number of items to show in lists/dicts etc",
"default": 10
}
}
}
40 changes: 40 additions & 0 deletions src/handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ISessionContext } from '@jupyterlab/apputils';

import { ISettingRegistry } from '@jupyterlab/settingregistry';

import { IExecuteResult } from '@jupyterlab/nbformat';

import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
Expand Down Expand Up @@ -100,8 +102,12 @@ export class VariableInspectionHandler extends AbstractHandler {
private _matrixQueryCommand: string;
private _widgetQueryCommand: string;
private _deleteCommand: string;
private _changeSettingsCommand:
| ((settings: IVariableInspector.ISettings) => string)
| undefined;
private _ready: Promise<void>;
private _id: string;
private _setting: ISettingRegistry.ISettings;

constructor(options: VariableInspectionHandler.IOptions) {
super(options.connector);
Expand All @@ -110,11 +116,14 @@ export class VariableInspectionHandler extends AbstractHandler {
this._queryCommand = options.queryCommand;
this._matrixQueryCommand = options.matrixQueryCommand;
this._widgetQueryCommand = options.widgetQueryCommand;
this._changeSettingsCommand = options.changeSettingsCommand;
this._deleteCommand = options.deleteCommand;
this._initScript = options.initScript;
this._setting = options.setting;

this._ready = this._connector.ready.then(() => {
this._initOnKernel().then((msg: KernelMessage.IExecuteReplyMsg) => {
this.performSettingsChange();
this._connector.iopubMessage.connect(this._queryCall);
return;
});
Expand All @@ -131,12 +140,20 @@ export class VariableInspectionHandler extends AbstractHandler {

this._ready = kernelReady.then(() => {
this._initOnKernel().then((msg: KernelMessage.IExecuteReplyMsg) => {
this.performSettingsChange();
this._connector.iopubMessage.connect(this._queryCall);
this.performInspection();
});
});
};

this._setting.changed.connect(async () => {
await this._ready;

this.performSettingsChange();
this.performInspection();
});

this._connector.kernelRestarted.connect(onKernelReset);
this._connector.kernelChanged.connect(onKernelReset);
}
Expand Down Expand Up @@ -232,6 +249,27 @@ export class VariableInspectionHandler extends AbstractHandler {
this._connector.fetch(content, this._handleQueryResponse);
}

/**
* Send a kernel request to change settings
*/
performSettingsChange(): void {
if (!this._changeSettingsCommand) {
return;
}

const settings: IVariableInspector.ISettings = {
maxItems: this._setting.get('maxItems').composite as number
};

const content: KernelMessage.IExecuteRequestMsg['content'] = {
code: this._changeSettingsCommand(settings),
stop_on_error: false,
store_history: false
};

this._connector.fetch(content, this._handleQueryResponse);
}

/**
* Initializes the kernel by running the set up script located at _initScriptPath.
*/
Expand Down Expand Up @@ -344,9 +382,11 @@ export namespace VariableInspectionHandler {
queryCommand: string;
matrixQueryCommand: string;
widgetQueryCommand: string;
changeSettingsCommand?(settings: IVariableInspector.ISettings): string;
deleteCommand: string;
initScript: string;
id: string;
setting: ISettingRegistry.ISettings;
}
}

Expand Down
65 changes: 45 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { IConsoleTracker } from '@jupyterlab/console';

import { INotebookTracker, NotebookPanel } from '@jupyterlab/notebook';

import { ISettingRegistry } from '@jupyterlab/settingregistry';

import { listIcon } from '@jupyterlab/ui-components';

import { DummyHandler, VariableInspectionHandler } from './handler';
Expand All @@ -30,6 +32,9 @@ namespace CommandIDs {
export const open = 'variableinspector:open';
}

const SETTINGS_ID =
'@lckr/jupyterlab_variableinspector:jupyterlab-variableInspector-settings';

/**
* A service providing variable introspection.
*/
Expand Down Expand Up @@ -110,17 +115,24 @@ const variableinspector: JupyterFrontEndPlugin<IVariableInspectorManager> = {
*/
const consoles: JupyterFrontEndPlugin<void> = {
id: '@lckr/jupyterlab-variableinspector:consoles',
requires: [IVariableInspectorManager, IConsoleTracker, ILabShell],
requires: [
IVariableInspectorManager,
IConsoleTracker,
ILabShell,
ISettingRegistry
],
autoStart: true,
activate: (
activate: async (
app: JupyterFrontEnd,
manager: IVariableInspectorManager,
consoles: IConsoleTracker,
labShell: ILabShell
): void => {
labShell: ILabShell,
settings: ISettingRegistry
): Promise<void> => {
const handlers: {
[id: string]: Promise<IVariableInspector.IInspectable>;
} = {};
const setting = await settings.load(SETTINGS_ID);

/**
* Subscribes to the creation of new consoles. If a new notebook is created, build a new handler for the consoles.
Expand Down Expand Up @@ -150,15 +162,18 @@ const consoles: JupyterFrontEndPlugin<void> = {
const matrixQueryCommand = result.matrixQueryCommand;
const widgetQueryCommand = result.widgetQueryCommand;
const deleteCommand = result.deleteCommand;
const changeSettingsCommand = result.changeSettingsCommand;

const options: VariableInspectionHandler.IOptions = {
queryCommand: queryCommand,
matrixQueryCommand: matrixQueryCommand,
queryCommand,
matrixQueryCommand,
widgetQueryCommand,
deleteCommand: deleteCommand,
connector: connector,
initScript: initScript,
id: session.path //Using the sessions path as an identifier for now.
deleteCommand,
connector,
initScript,
changeSettingsCommand,
id: session.path, //Using the sessions path as an identifier for now.
setting
};
const handler = new VariableInspectionHandler(options);
manager.addHandler(handler);
Expand Down Expand Up @@ -222,15 +237,22 @@ const consoles: JupyterFrontEndPlugin<void> = {
*/
const notebooks: JupyterFrontEndPlugin<void> = {
id: '@lckr/jupyterlab-variableinspector:notebooks',
requires: [IVariableInspectorManager, INotebookTracker, ILabShell],
requires: [
IVariableInspectorManager,
INotebookTracker,
ILabShell,
ISettingRegistry
],
autoStart: true,
activate: (
activate: async (
app: JupyterFrontEnd,
manager: IVariableInspectorManager,
notebooks: INotebookTracker,
labShell: ILabShell
): void => {
labShell: ILabShell,
settings: ISettingRegistry
): Promise<void> => {
const handlers: { [id: string]: Promise<VariableInspectionHandler> } = {};
const setting = await settings.load(SETTINGS_ID);

/**
* Subscribes to the creation of new notebooks. If a new notebook is created, build a new handler for the notebook.
Expand All @@ -256,16 +278,19 @@ const notebooks: JupyterFrontEndPlugin<void> = {
const matrixQueryCommand = result.matrixQueryCommand;
const widgetQueryCommand = result.widgetQueryCommand;
const deleteCommand = result.deleteCommand;
const changeSettingsCommand = result.changeSettingsCommand;

const options: VariableInspectionHandler.IOptions = {
queryCommand: queryCommand,
matrixQueryCommand: matrixQueryCommand,
queryCommand,
matrixQueryCommand,
widgetQueryCommand,
deleteCommand: deleteCommand,
connector: connector,
deleteCommand,
connector,
rendermime,
initScript: initScript,
id: session.path //Using the sessions path as an identifier for now.
initScript,
changeSettingsCommand,
id: session.path, //Using the sessions path as an identifier for now.
setting
};
const handler = new VariableInspectionHandler(options);
manager.addHandler(handler);
Expand Down
50 changes: 45 additions & 5 deletions src/inspectorscripts.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { IVariableInspector } from './tokens';

export namespace Languages {
export type LanguageModel = {
initScript: string;
queryCommand: string;
matrixQueryCommand: string;
widgetQueryCommand: string;
deleteCommand: string;
changeSettingsCommand?: (settings: IVariableInspector.ISettings) => string;
};
}

Expand All @@ -16,6 +19,8 @@ export abstract class Languages {
static py_script = `import json
import sys
from importlib import __import__
from itertools import islice
import collections
from IPython import get_ipython
from IPython.core.magics.namespace import NamespaceMagics

Expand All @@ -24,6 +29,8 @@ _jupyterlab_variableinspector_nms = NamespaceMagics()
_jupyterlab_variableinspector_Jupyter = get_ipython()
_jupyterlab_variableinspector_nms.shell = _jupyterlab_variableinspector_Jupyter.kernel.shell

_jupyterlab_variableinspector_maxitems = 10

__np = None
__pd = None
__pyspark = None
Expand Down Expand Up @@ -54,6 +61,12 @@ def _check_imported():
__xr = _attempt_import('xarray')


def _jupyterlab_variableinspector_changesettings(maxitems, **kwargs):
global _jupyterlab_variableinspector_maxitems

_jupyterlab_variableinspector_maxitems = maxitems


def _jupyterlab_variableinspector_getsizeof(x):
if type(x).__name__ in ['ndarray', 'Series']:
return x.nbytes
Expand Down Expand Up @@ -101,7 +114,28 @@ def _jupyterlab_variableinspector_getshapeof(x):
def _jupyterlab_variableinspector_getcontentof(x):
# returns content in a friendly way for python variables
# pandas and numpy
if __pd and isinstance(x, __pd.DataFrame):
if isinstance(x, (bool, str, int, float, type(None))):
content = str(x)
elif isinstance(x, (list, tuple)):
if len(x) <= _jupyterlab_variableinspector_maxitems:
content = str(x)
else:
content = "["
for i in range(_jupyterlab_variableinspector_maxitems):
content += f"{x[i]}, "
content += "...]"
elif isinstance(x, collections.abc.Mapping):
if len(x.keys()) <= _jupyterlab_variableinspector_maxitems:
content = str(x)
else:
first_ten_keys = list(islice(x.keys(), _jupyterlab_variableinspector_maxitems))
content = "{"
for idx, key in enumerate(first_ten_keys):
if idx > 0:
content += ", "
content += f'"{key}": {x[key]}'
content += ", ...}"
elif __pd and isinstance(x, __pd.DataFrame):
colnames = ', '.join(x.columns.map(str))
content = "Columns: %s" % colnames
elif __pd and isinstance(x, __pd.Series):
Expand Down Expand Up @@ -152,7 +186,7 @@ def _jupyterlab_variableinspector_dict_list():
def keep_cond(v):
try:
obj = eval(v)
if isinstance(obj, (bool, str, list, int, float, type(None))):
if isinstance(obj, (bool, str, list, tuple, collections.abc.Mapping, int, float, type(None))):
return True
if __tf and isinstance(obj, __tf.Variable):
return True
Expand Down Expand Up @@ -311,21 +345,27 @@ def _jupyterlab_variableinspector_deletevariable(x):
queryCommand: '_jupyterlab_variableinspector_dict_list()',
matrixQueryCommand: '_jupyterlab_variableinspector_getmatrixcontent',
widgetQueryCommand: '_jupyterlab_variableinspector_displaywidget',
deleteCommand: '_jupyterlab_variableinspector_deletevariable'
deleteCommand: '_jupyterlab_variableinspector_deletevariable',
changeSettingsCommand: (settings: IVariableInspector.ISettings) =>
`_jupyterlab_variableinspector_changesettings(maxitems=${settings.maxItems})`
},
python2: {
initScript: Languages.py_script,
queryCommand: '_jupyterlab_variableinspector_dict_list()',
matrixQueryCommand: '_jupyterlab_variableinspector_getmatrixcontent',
widgetQueryCommand: '_jupyterlab_variableinspector_displaywidget',
deleteCommand: '_jupyterlab_variableinspector_deletevariable'
deleteCommand: '_jupyterlab_variableinspector_deletevariable',
changeSettingsCommand: (settings: IVariableInspector.ISettings) =>
`_jupyterlab_variableinspector_changesettings(maxitems=${settings.maxItems})`
},
python: {
initScript: Languages.py_script,
queryCommand: '_jupyterlab_variableinspector_dict_list()',
matrixQueryCommand: '_jupyterlab_variableinspector_getmatrixcontent',
widgetQueryCommand: '_jupyterlab_variableinspector_displaywidget',
deleteCommand: '_jupyterlab_variableinspector_deletevariable'
deleteCommand: '_jupyterlab_variableinspector_deletevariable',
changeSettingsCommand: (settings: IVariableInspector.ISettings) =>
`_jupyterlab_variableinspector_changesettings(maxitems=${settings.maxItems})`
},
R: {
initScript: Languages.r_script,
Expand Down
5 changes: 5 additions & 0 deletions src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export namespace IVariableInspector {
performDelete(varName: string): void;
}

export interface ISettings {
maxItems: number;
}

export interface IVariableInspectorUpdate {
title: IVariableTitle;
payload: Array<IVariable>;
Expand All @@ -67,6 +71,7 @@ export namespace IVariableInspector {
isMatrix: boolean;
isWidget: boolean;
}

export interface IVariableTitle {
kernelName?: string;
contextName?: string; //Context currently reserved for special information.
Expand Down
Loading