|
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 |
5 | 6 |
|
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']" |
8 | 9 |
|
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') |
12 | 13 |
|
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) |
22 | 24 |
|
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 |
36 | 38 |
|
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) |
48 | 50 |
|
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: |
60 | 64 | _VSCODE_columnNames = list(_VSCODE_df) |
61 | | - else: |
62 | | - _VSCODE_columnNames = list(_VSCODE_df) |
63 | 65 |
|
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') |
68 | 75 |
|
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 |
73 | 88 |
|
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 |
86 | 91 |
|
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 |
89 | 97 |
|
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) |
95 | 99 |
|
| 100 | + # Transform this back into a string |
| 101 | + del _VSCODE_targetVariable |
96 | 102 |
|
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 |
100 | 107 |
|
101 | | - # Cleanup imports |
102 | | - del _VSCODE_json |
103 | | - del _VSCODE_pd |
104 | | - del _VSCODE_pd_json |
| 108 | + return _VSCODE_result |
0 commit comments