Skip to content

Commit f49ae53

Browse files
committed
Misc
1 parent e4e2fd2 commit f49ae53

File tree

15 files changed

+1248
-539
lines changed

15 files changed

+1248
-539
lines changed

package.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,24 @@
548548
"command": "python.datascience.scrolltocell",
549549
"title": "%python.command.python.datascience.scrolltocell.title%",
550550
"category": "Python"
551+
},
552+
{
553+
"command": "python.viewList",
554+
"title": "View the List in an awesome viewer",
555+
"category": "Python",
556+
"icon": {
557+
"light": "resources/light/repl.svg",
558+
"dark": "resources/dark/repl.svg"
559+
}
560+
},
561+
{
562+
"command": "python.viewDataFrame",
563+
"title": "View the DataFrame in an awesome viewer",
564+
"category": "Python",
565+
"icon": {
566+
"light": "resources/light/open-file.svg",
567+
"dark": "resources/dark/open-file.svg"
568+
}
551569
}
552570
],
553571
"menus": {
@@ -1007,6 +1025,24 @@
10071025
"command": "python.runTestNode",
10081026
"when": "view == python_tests && viewItem == suite && !busyTests",
10091027
"group": "inline@0"
1028+
},
1029+
{
1030+
"command": "python.viewDataFrame",
1031+
"when": "view == dsVariables && viewItem == DataFrame",
1032+
"group": "inline@0"
1033+
},
1034+
{
1035+
"command": "python.viewList",
1036+
"when": "view == dsVariables && viewItem == list",
1037+
"group": "inline@0"
1038+
},
1039+
{
1040+
"command": "python.viewDataFrame",
1041+
"when": "view == dsVariables && viewItem == DataFrame"
1042+
},
1043+
{
1044+
"command": "python.viewList",
1045+
"when": "view == dsVariables && viewItem == list"
10101046
}
10111047
]
10121048
},
@@ -2567,6 +2603,13 @@
25672603
}
25682604
],
25692605
"views": {
2606+
"debug": [
2607+
{
2608+
"id": "dsVariables",
2609+
"name": "Data Science Variables",
2610+
"when": "inDebugMode"
2611+
}
2612+
],
25702613
"test": [
25712614
{
25722615
"id": "python_tests",
Lines changed: 93 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,108 @@
1-
# Query Jupyter server for the info about a dataframe
2-
import json as _VSCODE_json
3-
import pandas as _VSCODE_pd
4-
import pandas.io.json as _VSCODE_pd_json
1+
def do(_VSCODE_targetVariable, globs, locs):
2+
# Query Jupyter server for the info about a dataframe
3+
import json as _VSCODE_json
4+
import pandas as _VSCODE_pd
5+
import pandas.io.json as _VSCODE_pd_json
56

6-
# _VSCode_sub_supportsDataExplorer will contain our list of data explorer supported types
7-
_VSCode_supportsDataExplorer = "['list', 'Series', 'dict', 'ndarray', 'DataFrame']"
7+
# _VSCode_sub_supportsDataExplorer will contain our list of data explorer supported types
8+
_VSCode_supportsDataExplorer = "['list', 'Series', 'dict', 'ndarray', 'DataFrame']"
89

9-
# In IJupyterVariables.getValue this '_VSCode_JupyterTestValue' will be replaced with the json stringified value of the target variable
10-
# Indexes off of _VSCODE_targetVariable need to index types that are part of IJupyterVariable
11-
_VSCODE_targetVariable = _VSCODE_json.loads('_VSCode_JupyterTestValue')
10+
# In IJupyterVariables.getValue this '_VSCode_JupyterTestValue' will be replaced with the json stringified value of the target variable
11+
# Indexes off of _VSCODE_targetVariable need to index types that are part of IJupyterVariable
12+
# _VSCODE_targetVariable = _VSCODE_json.loads('_VSCode_JupyterTestValue')
1213

13-
# First check to see if we are a supported type, this prevents us from adding types that are not supported
14-
# and also keeps our types in sync with what the variable explorer says that we support
15-
if _VSCODE_targetVariable['type'] not in _VSCode_supportsDataExplorer:
16-
del _VSCode_supportsDataExplorer
17-
print(_VSCODE_json.dumps(_VSCODE_targetVariable))
18-
del _VSCODE_targetVariable
19-
else:
20-
del _VSCode_supportsDataExplorer
21-
_VSCODE_evalResult = eval(_VSCODE_targetVariable['name'])
14+
# First check to see if we are a supported type, this prevents us from adding types that are not supported
15+
# and also keeps our types in sync with what the variable explorer says that we support
16+
if _VSCODE_targetVariable['type'] not in _VSCode_supportsDataExplorer:
17+
del _VSCode_supportsDataExplorer
18+
return _VSCODE_json.dumps(_VSCODE_targetVariable)
19+
del _VSCODE_targetVariable
20+
else:
21+
del _VSCode_supportsDataExplorer
22+
# _VSCODE_evalResult = eval(_VSCODE_targetVariable['name'])
23+
_VSCODE_evalResult = eval(_VSCODE_targetVariable['name'], globs, locs)
2224

23-
# Figure out shape if not already there. Use the shape to compute the row count
24-
if (hasattr(_VSCODE_evalResult, 'shape')):
25-
try:
26-
# Get a bit more restrictive with exactly what we want to count as a shape, since anything can define it
27-
if isinstance(_VSCODE_evalResult.shape, tuple):
28-
_VSCODE_targetVariable['rowCount'] = _VSCODE_evalResult.shape[0]
29-
except TypeError:
30-
_VSCODE_targetVariable['rowCount'] = 0
31-
elif (hasattr(_VSCODE_evalResult, '__len__')):
32-
try:
33-
_VSCODE_targetVariable['rowCount'] = len(_VSCODE_evalResult)
34-
except TypeError:
35-
_VSCODE_targetVariable['rowCount'] = 0
25+
# Figure out shape if not already there. Use the shape to compute the row count
26+
if (hasattr(_VSCODE_evalResult, 'shape')):
27+
try:
28+
# Get a bit more restrictive with exactly what we want to count as a shape, since anything can define it
29+
if isinstance(_VSCODE_evalResult.shape, tuple):
30+
_VSCODE_targetVariable['rowCount'] = _VSCODE_evalResult.shape[0]
31+
except TypeError:
32+
_VSCODE_targetVariable['rowCount'] = 0
33+
elif (hasattr(_VSCODE_evalResult, '__len__')):
34+
try:
35+
_VSCODE_targetVariable['rowCount'] = len(_VSCODE_evalResult)
36+
except TypeError:
37+
_VSCODE_targetVariable['rowCount'] = 0
3638

37-
# Turn the eval result into a df
38-
_VSCODE_df = _VSCODE_evalResult
39-
if isinstance(_VSCODE_evalResult, list):
40-
_VSCODE_df = _VSCODE_pd.DataFrame(_VSCODE_evalResult)
41-
elif isinstance(_VSCODE_evalResult, _VSCODE_pd.Series):
42-
_VSCODE_df = _VSCODE_pd.Series.to_frame(_VSCODE_evalResult)
43-
elif isinstance(_VSCODE_evalResult, dict):
44-
_VSCODE_evalResult = _VSCODE_pd.Series(_VSCODE_evalResult)
45-
_VSCODE_df = _VSCODE_pd.Series.to_frame(_VSCODE_evalResult)
46-
elif _VSCODE_targetVariable['type'] == 'ndarray':
47-
_VSCODE_df = _VSCODE_pd.DataFrame(_VSCODE_evalResult)
39+
# Turn the eval result into a df
40+
_VSCODE_df = _VSCODE_evalResult
41+
if isinstance(_VSCODE_evalResult, list):
42+
_VSCODE_df = _VSCODE_pd.DataFrame(_VSCODE_evalResult)
43+
elif isinstance(_VSCODE_evalResult, _VSCODE_pd.Series):
44+
_VSCODE_df = _VSCODE_pd.Series.to_frame(_VSCODE_evalResult)
45+
elif isinstance(_VSCODE_evalResult, dict):
46+
_VSCODE_evalResult = _VSCODE_pd.Series(_VSCODE_evalResult)
47+
_VSCODE_df = _VSCODE_pd.Series.to_frame(_VSCODE_evalResult)
48+
elif _VSCODE_targetVariable['type'] == 'ndarray':
49+
_VSCODE_df = _VSCODE_pd.DataFrame(_VSCODE_evalResult)
4850

49-
# If any rows, use pandas json to convert a single row to json. Extract
50-
# the column names and types from the json so we match what we'll fetch when
51-
# we ask for all of the rows
52-
if _VSCODE_targetVariable['rowCount']:
53-
try:
54-
_VSCODE_row = _VSCODE_df.iloc[0:1]
55-
_VSCODE_json_row = _VSCODE_pd_json.to_json(None, _VSCODE_row, date_format='iso')
56-
_VSCODE_columnNames = list(_VSCODE_json.loads(_VSCODE_json_row))
57-
del _VSCODE_row
58-
del _VSCODE_json_row
59-
except:
51+
# If any rows, use pandas json to convert a single row to json. Extract
52+
# the column names and types from the json so we match what we'll fetch when
53+
# we ask for all of the rows
54+
if _VSCODE_targetVariable['rowCount']:
55+
try:
56+
_VSCODE_row = _VSCODE_df.iloc[0:1]
57+
_VSCODE_json_row = _VSCODE_pd_json.to_json(None, _VSCODE_row, date_format='iso')
58+
_VSCODE_columnNames = list(_VSCODE_json.loads(_VSCODE_json_row))
59+
del _VSCODE_row
60+
del _VSCODE_json_row
61+
except:
62+
_VSCODE_columnNames = list(_VSCODE_df)
63+
else:
6064
_VSCODE_columnNames = list(_VSCODE_df)
61-
else:
62-
_VSCODE_columnNames = list(_VSCODE_df)
6365

64-
# Compute the index column. It may have been renamed
65-
_VSCODE_indexColumn = _VSCODE_df.index.name if _VSCODE_df.index.name else 'index'
66-
_VSCODE_columnTypes = list(_VSCODE_df.dtypes)
67-
del _VSCODE_df
66+
# Compute the index column. It may have been renamed
67+
_VSCODE_indexColumn = _VSCODE_df.index.name if _VSCODE_df.index.name else 'index'
68+
_VSCODE_columnTypes = list(_VSCODE_df.dtypes)
69+
del _VSCODE_df
70+
71+
# Make sure the index column exists
72+
if _VSCODE_indexColumn not in _VSCODE_columnNames:
73+
_VSCODE_columnNames.insert(0, _VSCODE_indexColumn)
74+
_VSCODE_columnTypes.insert(0, 'int64')
6875

69-
# Make sure the index column exists
70-
if _VSCODE_indexColumn not in _VSCODE_columnNames:
71-
_VSCODE_columnNames.insert(0, _VSCODE_indexColumn)
72-
_VSCODE_columnTypes.insert(0, 'int64')
76+
# Then loop and generate our output json
77+
_VSCODE_columns = []
78+
for _VSCODE_n in range(0, len(_VSCODE_columnNames)):
79+
_VSCODE_column_type = _VSCODE_columnTypes[_VSCODE_n]
80+
_VSCODE_column_name = str(_VSCODE_columnNames[_VSCODE_n])
81+
_VSCODE_colobj = {}
82+
_VSCODE_colobj['key'] = _VSCODE_column_name
83+
_VSCODE_colobj['name'] = _VSCODE_column_name
84+
_VSCODE_colobj['type'] = str(_VSCODE_column_type)
85+
_VSCODE_columns.append(_VSCODE_colobj)
86+
del _VSCODE_column_name
87+
del _VSCODE_column_type
7388

74-
# Then loop and generate our output json
75-
_VSCODE_columns = []
76-
for _VSCODE_n in range(0, len(_VSCODE_columnNames)):
77-
_VSCODE_column_type = _VSCODE_columnTypes[_VSCODE_n]
78-
_VSCODE_column_name = str(_VSCODE_columnNames[_VSCODE_n])
79-
_VSCODE_colobj = {}
80-
_VSCODE_colobj['key'] = _VSCODE_column_name
81-
_VSCODE_colobj['name'] = _VSCODE_column_name
82-
_VSCODE_colobj['type'] = str(_VSCODE_column_type)
83-
_VSCODE_columns.append(_VSCODE_colobj)
84-
del _VSCODE_column_name
85-
del _VSCODE_column_type
89+
del _VSCODE_columnNames
90+
del _VSCODE_columnTypes
8691

87-
del _VSCODE_columnNames
88-
del _VSCODE_columnTypes
92+
# Save this in our target
93+
_VSCODE_targetVariable['columns'] = _VSCODE_columns
94+
_VSCODE_targetVariable['indexColumn'] = _VSCODE_indexColumn
95+
del _VSCODE_columns
96+
del _VSCODE_indexColumn
8997

90-
# Save this in our target
91-
_VSCODE_targetVariable['columns'] = _VSCODE_columns
92-
_VSCODE_targetVariable['indexColumn'] = _VSCODE_indexColumn
93-
del _VSCODE_columns
94-
del _VSCODE_indexColumn
98+
_VSCODE_result = _VSCODE_json.dumps(_VSCODE_targetVariable)
9599

100+
# Transform this back into a string
101+
del _VSCODE_targetVariable
96102

97-
# Transform this back into a string
98-
print(_VSCODE_json.dumps(_VSCODE_targetVariable))
99-
del _VSCODE_targetVariable
103+
# Cleanup imports
104+
del _VSCODE_json
105+
del _VSCODE_pd
106+
del _VSCODE_pd_json
100107

101-
# Cleanup imports
102-
del _VSCODE_json
103-
del _VSCODE_pd
104-
del _VSCODE_pd_json
108+
return _VSCODE_result
Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,46 @@
1-
# Query Jupyter server for the rows of a data frame
2-
import json as _VSCODE_json
3-
import pandas as _VSCODE_pd
4-
import pandas.io.json as _VSCODE_pd_json
1+
def do(_VSCODE_targetVariable, _VSCode_JupyterStartRow, _VSCode_JupyterEndRow, globs, locs):
2+
# Query Jupyter server for the rows of a data frame
3+
import json as _VSCODE_json
4+
import pandas as _VSCODE_pd
5+
import pandas.io.json as _VSCODE_pd_json
56

6-
# In IJupyterVariables.getValue this '_VSCode_JupyterTestValue' will be replaced with the json stringified value of the target variable
7-
# Indexes off of _VSCODE_targetVariable need to index types that are part of IJupyterVariable
8-
_VSCODE_targetVariable = _VSCODE_json.loads('_VSCode_JupyterTestValue')
9-
_VSCODE_evalResult = eval(_VSCODE_targetVariable['name'])
7+
# In IJupyterVariables.getValue this '_VSCode_JupyterTestValue' will be replaced with the json stringified value of the target variable
8+
# Indexes off of _VSCODE_targetVariable need to index types that are part of IJupyterVariable
9+
# _VSCODE_targetVariable = _VSCODE_json.loads('_VSCode_JupyterTestValue')
10+
# _VSCODE_evalResult = eval(_VSCODE_targetVariable['name'])
11+
_VSCODE_evalResult = eval(_VSCODE_targetVariable['name'], globs, locs)
1012

11-
# _VSCode_JupyterStartRow and _VSCode_JupyterEndRow should be replaced dynamically with the literals
12-
# for our start and end rows
13-
_VSCODE_startRow = max(_VSCode_JupyterStartRow, 0)
14-
_VSCODE_endRow = min(_VSCode_JupyterEndRow, _VSCODE_targetVariable['rowCount'])
13+
# _VSCode_JupyterStartRow and _VSCode_JupyterEndRow should be replaced dynamically with the literals
14+
# for our start and end rows
15+
_VSCODE_startRow = max(_VSCode_JupyterStartRow, 0)
16+
_VSCODE_endRow = min(_VSCode_JupyterEndRow, _VSCODE_targetVariable['rowCount'])
1517

16-
# Assume we have a dataframe. If not, turn our eval result into a dataframe
17-
_VSCODE_df = _VSCODE_evalResult
18-
if isinstance(_VSCODE_evalResult, list):
19-
_VSCODE_df = _VSCODE_pd.DataFrame(_VSCODE_evalResult)
20-
elif isinstance(_VSCODE_evalResult, _VSCODE_pd.Series):
21-
_VSCODE_df = _VSCODE_pd.Series.to_frame(_VSCODE_evalResult)
22-
elif isinstance(_VSCODE_evalResult, dict):
23-
_VSCODE_evalResult = _VSCODE_pd.Series(_VSCODE_evalResult)
24-
_VSCODE_df = _VSCODE_pd.Series.to_frame(_VSCODE_evalResult)
25-
elif _VSCODE_targetVariable['type'] == 'ndarray':
26-
_VSCODE_df = _VSCODE_pd.DataFrame(_VSCODE_evalResult)
27-
# If not a known type, then just let pandas handle it.
28-
elif not (hasattr(_VSCODE_df, 'iloc')):
29-
_VSCODE_df = _VSCODE_pd.DataFrame(_VSCODE_evalResult)
18+
# Assume we have a dataframe. If not, turn our eval result into a dataframe
19+
_VSCODE_df = _VSCODE_evalResult
20+
if isinstance(_VSCODE_evalResult, list):
21+
_VSCODE_df = _VSCODE_pd.DataFrame(_VSCODE_evalResult)
22+
elif isinstance(_VSCODE_evalResult, _VSCODE_pd.Series):
23+
_VSCODE_df = _VSCODE_pd.Series.to_frame(_VSCODE_evalResult)
24+
elif isinstance(_VSCODE_evalResult, dict):
25+
_VSCODE_evalResult = _VSCODE_pd.Series(_VSCODE_evalResult)
26+
_VSCODE_df = _VSCODE_pd.Series.to_frame(_VSCODE_evalResult)
27+
elif _VSCODE_targetVariable['type'] == 'ndarray':
28+
_VSCODE_df = _VSCODE_pd.DataFrame(_VSCODE_evalResult)
29+
# If not a known type, then just let pandas handle it.
30+
elif not (hasattr(_VSCODE_df, 'iloc')):
31+
_VSCODE_df = _VSCODE_pd.DataFrame(_VSCODE_evalResult)
3032

31-
# Turn into JSON using pandas. We use pandas because it's about 3 orders of magnitude faster to turn into JSON
32-
_VSCODE_rows = _VSCODE_df.iloc[_VSCODE_startRow:_VSCODE_endRow]
33-
_VSCODE_result = _VSCODE_pd_json.to_json(None, _VSCODE_rows, orient='table', date_format='iso')
34-
print(_VSCODE_result)
33+
# Turn into JSON using pandas. We use pandas because it's about 3 orders of magnitude faster to turn into JSON
34+
_VSCODE_rows = _VSCODE_df.iloc[_VSCODE_startRow:_VSCODE_endRow]
35+
_VSCODE_result = _VSCODE_pd_json.to_json(None, _VSCODE_rows, orient='table', date_format='iso')
3536

36-
# Cleanup our variables
37-
del _VSCODE_df
38-
del _VSCODE_endRow
39-
del _VSCODE_startRow
40-
del _VSCODE_rows
41-
del _VSCODE_result
42-
del _VSCODE_json
43-
del _VSCODE_pd
44-
del _VSCODE_pd_json
37+
# Cleanup our variables
38+
del _VSCODE_df
39+
del _VSCODE_endRow
40+
del _VSCODE_startRow
41+
del _VSCODE_rows
42+
del _VSCODE_json
43+
del _VSCODE_pd
44+
del _VSCODE_pd_json
45+
46+
return _VSCODE_result

0 commit comments

Comments
 (0)