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

Variable inspector widget buttons visibility and delete functionality #329

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ export class VariableInspectionHandler extends AbstractHandler {
break;
}
case 'error':
console.log(response);
reject("Kernel error on 'matrixQuery' call!");
const error = response as KernelMessage.IErrorMsg;
reject(error);
break;
default:
break;
Expand Down
7 changes: 6 additions & 1 deletion src/inspectorscripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ def _check_imported():
__ipywidgets = _attempt_import('ipywidgets')
__xr = _attempt_import('xarray')

def _verify_import(module,name):
if module is None:
raise ImportError(f"{name} is required to inspect the variable. Please import it and try again.")

def _jupyterlab_variableinspector_changesettings(maxitems, **kwargs):
global _jupyterlab_variableinspector_maxitems
Expand Down Expand Up @@ -254,7 +257,8 @@ def _jupyterlab_variableinspector_getmatrixcontent(x, max_rows=10000):
if threshold is not None:
x = x.head(threshold)
return x.to_json(orient="table", default_handler=_jupyterlab_variableinspector_default, force_ascii=False)
elif __np and __pd and type(x).__name__ == "ndarray":
elif __np and type(x).__name__ == "ndarray":
_verify_import(__pd, "pandas")
df = __pd.DataFrame(x)
return _jupyterlab_variableinspector_getmatrixcontent(df)
elif __tf and (isinstance(x, __tf.Variable) or isinstance(x, __tf.Tensor)):
Expand All @@ -267,6 +271,7 @@ def _jupyterlab_variableinspector_getmatrixcontent(x, max_rows=10000):
df = x.to_numpy()
return _jupyterlab_variableinspector_getmatrixcontent(df)
elif isinstance(x, list):
_verify_import(__pd, "pandas")
s = __pd.Series(x)
return _jupyterlab_variableinspector_getmatrixcontent(s)

Expand Down
35 changes: 24 additions & 11 deletions src/variableinspector.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { OutputAreaModel, SimplifiedOutputArea } from '@jupyterlab/outputarea';

import { closeIcon, searchIcon } from '@jupyterlab/ui-components';
import {
closeIcon,
deleteIcon,
inspectorIcon
} from '@jupyterlab/ui-components';

import { DataGrid, DataModel } from '@lumino/datagrid';

Expand Down Expand Up @@ -37,6 +41,7 @@ provideJupyterDesignSystem().register(

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

const TITLE_CLASS = 'jp-VarInspector-title';
const PANEL_CLASS = 'jp-VarInspector';
Expand Down Expand Up @@ -329,35 +334,43 @@ export class VariableInspectorPanel
cell.title = 'Delete Variable';
cell.className = 'jp-VarInspector-deleteButton';
cell.gridColumn = '1';
const closeButton = document.createElement('jp-button') as Button;
closeButton.appearance = 'stealth';
const ico = closeIcon.element();
const deleteButton = document.createElement('jp-button') as Button;
deleteButton.appearance = 'stealth';
const ico = deleteIcon.element();
ico.className = 'icon-button';
ico.onclick = (ev: MouseEvent): any => {
this.source?.performDelete(name);
this.removeRow(name);
};
closeButton.append(ico);
cell.append(closeButton);
deleteButton.append(ico);
cell.append(deleteButton);
row.appendChild(cell);

// Add onclick event for inspection
cell = document.createElement('jp-data-grid-cell') as DataGridCell;
if (item.isMatrix) {
cell.title = 'View Contents';
cell.className = 'jp-VarInspector-inspectButton';
const searchButton = document.createElement('jp-button') as Button;
searchButton.appearance = 'stealth';
const ico = searchIcon.element();
const inspectorButton = document.createElement('jp-button') as Button;
inspectorButton.appearance = 'stealth';
const ico = inspectorIcon.element();
ico.className = 'icon-button';
ico.onclick = (ev: MouseEvent): any => {
this._source
?.performMatrixInspection(item.varName)
.then((model: DataModel) => {
this._showMatrix(model, item.varName, item.varType);
})
.catch((error: KernelMessage.IErrorMsg) => {
if (error.content.ename === 'ImportError') {
alert(error.content.evalue);
} else {
console.log(error);
}
});
};
searchButton.append(ico);
cell.append(searchButton);
inspectorButton.append(ico);
cell.append(inspectorButton);
} else {
cell.innerHTML = '';
}
Expand Down
4 changes: 2 additions & 2 deletions style/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@

.jp-VarInspector-deleteButton {
display: flex;
justify-content: space-around;
justify-content: center;
width: 1em;
}

.jp-VarInspector-inspectButton {
display: flex;
justify-content: space-around;
justify-content: center;
width: 1em;
}

Expand Down
Loading