forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetJupyterVariableValue.py
More file actions
25 lines (20 loc) · 1.19 KB
/
getJupyterVariableValue.py
File metadata and controls
25 lines (20 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Query Jupyter server for the value of a variable
import json as _VSCODE_json
_VSCODE_max_len = 200
# In IJupyterVariables.getValue this '_VSCode_JupyterTestValue' will be replaced with the json stringified value of the target variable
# Indexes off of _VSCODE_targetVariable need to index types that are part of IJupyterVariable
_VSCODE_targetVariable = _VSCODE_json.loads('_VSCode_JupyterTestValue')
_VSCODE_evalResult = eval(_VSCODE_targetVariable['name'])
# Find shape and count if available
if _VSCODE_targetVariable['type'] in ['ndarray','DataFrame','Series']:
_VSCODE_targetVariable['shape'] = str(_VSCODE_evalResult.shape)
if _VSCODE_targetVariable['type'] in ['tuple', 'str', 'dict', 'list', 'set', 'ndarray','DataFrame','Series']:
_VSCODE_targetVariable['count'] = len(_VSCODE_evalResult)
# Get the string of the eval result, truncate it as it could be far too long
_VSCODE_targetValue = str(_VSCODE_evalResult)
if len(_VSCODE_targetValue) > _VSCODE_max_len:
_VSCODE_targetVariable['truncated'] = True
_VSCODE_targetVariable['value'] = _VSCODE_targetValue[:_VSCODE_max_len]
else:
_VSCODE_targetVariable['value'] = _VSCODE_targetValue
print(_VSCODE_json.dumps(_VSCODE_targetVariable))