Skip to content

Commit f4f9f96

Browse files
correctly show variables after jupyter restart (microsoft#5336)
1 parent 5765286 commit f4f9f96

7 files changed

Lines changed: 30 additions & 19 deletions

File tree

news/2 Fixes/5244.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Variables not cleared after a kernel restart

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,7 @@
12401240
},
12411241
"python.dataScience.variableExplorerExclude": {
12421242
"type": "string",
1243-
"default": "module;builtin_function_or_method",
1243+
"default": "module;function;builtin_function_or_method",
12441244
"description": "Types to exclude from showing in the Python Interactive variable explorer",
12451245
"scope": "resource"
12461246
},

pythonFiles/datascience/getJupyterVariableList.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,27 @@
22
# Tested on 2.7 and 3.6
33
from sys import getsizeof as _VSCODE_getsizeof
44
import json as _VSCODE_json
5-
from IPython import get_ipython
5+
from IPython import get_ipython as _VSCODE_get_ipython
66

77
# _VSCode_supportsDataExplorer will contain our list of data explorer supported types
88
_VSCode_supportsDataExplorer = "['list', 'Series', 'dict', 'ndarray', 'DataFrame']"
99

1010
# who_ls is a Jupyter line magic to fetch currently defined vars
11-
_VSCode_JupyterVars = get_ipython().run_line_magic('who_ls', '')
11+
_VSCode_JupyterVars = _VSCODE_get_ipython().run_line_magic('who_ls', '')
1212

1313
_VSCode_output = []
14-
for var in _VSCode_JupyterVars:
14+
for _VSCode_var in _VSCode_JupyterVars:
1515
try:
16-
_VSCode_type = type(eval(var))
17-
_VSCode_output.append({'name': var, 'type': _VSCode_type.__name__, 'size': _VSCODE_getsizeof(var), 'supportsDataExplorer': _VSCode_type.__name__ in _VSCode_supportsDataExplorer })
16+
_VSCode_type = type(eval(_VSCode_var))
17+
_VSCode_output.append({'name': _VSCode_var, 'type': _VSCode_type.__name__, 'size': _VSCODE_getsizeof(_VSCode_var), 'supportsDataExplorer': _VSCode_type.__name__ in _VSCode_supportsDataExplorer })
1818
del _VSCode_type
19+
del _VSCode_var
1920
except:
2021
pass
2122

2223
print(_VSCODE_json.dumps(_VSCode_output))
2324

25+
del _VSCODE_get_ipython
2426
del _VSCode_output
2527
del _VSCode_supportsDataExplorer
2628
del _VSCode_JupyterVars

src/client/datascience/history/history.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
IJupyterExecution,
4545
IJupyterVariable,
4646
IJupyterVariables,
47+
IJupyterVariablesResponse,
4748
INotebookExporter,
4849
INotebookServer,
4950
InterruptResult,
@@ -918,26 +919,27 @@ export class History extends WebViewHost<IHistoryMapping> implements IHistory {
918919
}
919920
}
920921

921-
private requestVariables = async (executionCount: number): Promise<void> => {
922+
private requestVariables = async (requestExecutionCount: number): Promise<void> => {
922923
// Request our new list of variables
923-
let vars: IJupyterVariable[] = await this.jupyterVariables.getVariables();
924+
const vars: IJupyterVariable[] = await this.jupyterVariables.getVariables();
925+
const variablesResponse: IJupyterVariablesResponse = {executionCount: requestExecutionCount, variables: vars };
924926

925927
// Tag all of our jupyter variables with the execution count of the request
926-
vars.forEach((value: IJupyterVariable) => {
927-
value.executionCount = executionCount;
928+
variablesResponse.variables.forEach((value: IJupyterVariable) => {
929+
value.executionCount = requestExecutionCount;
928930
});
929931

930932
const settings = this.configuration.getSettings();
931933
const excludeString = settings.datascience.variableExplorerExclude;
932934

933935
if (excludeString) {
934936
const excludeArray = excludeString.split(';');
935-
vars = vars.filter((value) => {
937+
variablesResponse.variables = variablesResponse.variables.filter((value) => {
936938
return excludeArray.indexOf(value.type) === -1;
937939
});
938940
}
939941

940-
this.postMessage(HistoryMessages.GetVariablesResponse, vars).ignoreErrors();
942+
this.postMessage(HistoryMessages.GetVariablesResponse, variablesResponse).ignoreErrors();
941943
}
942944

943945
// tslint:disable-next-line: no-any

src/client/datascience/history/historyTypes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT License.
33
'use strict';
44
import { CssMessages, IGetCssRequest, IGetCssResponse, SharedMessages } from '../constants';
5-
import { ICell, IHistoryInfo, IJupyterVariable } from '../types';
5+
import { ICell, IHistoryInfo, IJupyterVariable, IJupyterVariablesResponse } from '../types';
66

77
export namespace HistoryMessages {
88
export const StartCell = 'start_cell';
@@ -97,7 +97,7 @@ export class IHistoryMapping {
9797
public [HistoryMessages.Activate] : never | undefined;
9898
public [HistoryMessages.ShowDataViewer]: string;
9999
public [HistoryMessages.GetVariablesRequest]: number;
100-
public [HistoryMessages.GetVariablesResponse]: IJupyterVariable[];
100+
public [HistoryMessages.GetVariablesResponse]: IJupyterVariablesResponse;
101101
public [HistoryMessages.GetVariableValueRequest]: IJupyterVariable;
102102
public [HistoryMessages.GetVariableValueResponse]: IJupyterVariable;
103103
public [CssMessages.GetCssRequest] : IGetCssRequest;

src/client/datascience/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,12 @@ export interface IJupyterVariables {
281281
getDataFrameRows(targetVariable: IJupyterVariable, start: number, end: number) : Promise<JSONObject>;
282282
}
283283

284+
// Wrapper to hold an execution count for our variable requests
285+
export interface IJupyterVariablesResponse {
286+
executionCount: number;
287+
variables: IJupyterVariable[];
288+
}
289+
284290
export const IDataViewerProvider = Symbol('IDataViewerProvider');
285291
export interface IDataViewerProvider {
286292
create(variable: string) : Promise<IDataViewer>;

src/datascience-ui/history-react/MainPanel.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as React from 'react';
99
import { CellMatcher } from '../../client/datascience/cellMatcher';
1010
import { generateMarkdownFromCodeLines } from '../../client/datascience/common';
1111
import { HistoryMessages, IHistoryMapping } from '../../client/datascience/history/historyTypes';
12-
import { CellState, ICell, IHistoryInfo, IJupyterVariable } from '../../client/datascience/types';
12+
import { CellState, ICell, IHistoryInfo, IJupyterVariable, IJupyterVariablesResponse } from '../../client/datascience/types';
1313
import { IMessageHandler, PostOffice } from '../react-common/postOffice';
1414
import { getSettings, updateSettings } from '../react-common/settingsReactSide';
1515
import { StyleInjector } from '../react-common/styleInjector';
@@ -784,16 +784,16 @@ export class MainPanel extends React.Component<IMainPanelProps, IMainPanelState>
784784
// tslint:disable-next-line:no-any
785785
private getVariablesResponse = (payload?: any) => {
786786
if (payload) {
787-
const variables = payload as IJupyterVariable[];
787+
const variablesResponse = payload as IJupyterVariablesResponse;
788788

789789
// Check to see if we have moved to a new execution count only send our update if we are on the same count as the request
790-
if (variables.length > 0 && variables[0].executionCount !== undefined && variables[0].executionCount === this.currentExecutionCount) {
790+
if (variablesResponse.executionCount === this.currentExecutionCount) {
791791
if (this.variableExplorerRef.current) {
792-
this.variableExplorerRef.current.newVariablesData(variables);
792+
this.variableExplorerRef.current.newVariablesData(variablesResponse.variables);
793793
}
794794

795795
// Now put out a request for all of the sub values for the variables
796-
variables.forEach(this.refreshVariable);
796+
variablesResponse.variables.forEach(this.refreshVariable);
797797
}
798798
}
799799
}

0 commit comments

Comments
 (0)