Skip to content

Commit

Permalink
Add settings
Browse files Browse the repository at this point in the history
  • Loading branch information
martinRenou committed Sep 3, 2024
1 parent 3540826 commit 7766ec5
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 27 deletions.
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
13 changes: 13 additions & 0 deletions schema/jupyterlab-variableInspector-settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"title": "jupyterlab-variableInspector settings",
"description": "Settings for the jupyterlab-variableInspector extension.",
"type": "object",
"properties": {
"maxItems": {
"type": "number",
"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
34 changes: 27 additions & 7 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 @@ -26,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 @@ -56,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 @@ -106,15 +117,18 @@ def _jupyterlab_variableinspector_getcontentof(x):
if isinstance(x, (bool, str, int, float, type(None))):
content = str(x)
elif isinstance(x, (list, tuple)):
if len(x) < 10:
if len(x) <= _jupyterlab_variableinspector_maxitems:
content = str(x)
else:
content = f"[{x[0]}, {x[1]}, {x[2]}, ..., {x[-1]}]"
content = "["
for i in range(_jupyterlab_variableinspector_maxitems):
content += f"{x[i]}, "
content += "...]"
elif isinstance(x, collections.abc.Mapping):
if len(x.keys()) < 10:
if len(x.keys()) <= _jupyterlab_variableinspector_maxitems:
content = str(x)
else:
first_ten_keys = list(islice(x.keys(), 10))
first_ten_keys = list(islice(x.keys(), _jupyterlab_variableinspector_maxitems))
content = "{"
for idx, key in enumerate(first_ten_keys):
if idx > 0:
Expand Down Expand Up @@ -331,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

0 comments on commit 7766ec5

Please sign in to comment.