|
8 | 8 |
|
9 | 9 | # In IJupyterVariables.getValue this '_VSCode_JupyterTestValue' will be replaced with the json stringified value of the target variable |
10 | 10 | # Indexes off of _VSCODE_targetVariable need to index types that are part of IJupyterVariable |
11 | | -_VSCODE_targetVariable = _VSCODE_json.loads('_VSCode_JupyterTestValue') |
| 11 | +_VSCODE_targetVariable = _VSCODE_json.loads("""_VSCode_JupyterTestValue""") |
12 | 12 |
|
13 | 13 | # First check to see if we are a supported type, this prevents us from adding types that are not supported |
14 | 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: |
| 15 | +if _VSCODE_targetVariable["type"] not in _VSCode_supportsDataExplorer: |
16 | 16 | del _VSCode_supportsDataExplorer |
17 | 17 | print(_VSCODE_json.dumps(_VSCODE_targetVariable)) |
18 | 18 | del _VSCODE_targetVariable |
19 | 19 | else: |
20 | 20 | del _VSCode_supportsDataExplorer |
21 | | - _VSCODE_evalResult = eval(_VSCODE_targetVariable['name']) |
| 21 | + _VSCODE_evalResult = eval(_VSCODE_targetVariable["name"]) |
22 | 22 |
|
23 | 23 | # Figure out shape if not already there. Use the shape to compute the row count |
24 | | - if (hasattr(_VSCODE_evalResult, 'shape')): |
| 24 | + if hasattr(_VSCODE_evalResult, "shape"): |
25 | 25 | try: |
26 | 26 | # Get a bit more restrictive with exactly what we want to count as a shape, since anything can define it |
27 | 27 | if isinstance(_VSCODE_evalResult.shape, tuple): |
28 | | - _VSCODE_targetVariable['rowCount'] = _VSCODE_evalResult.shape[0] |
| 28 | + _VSCODE_targetVariable["rowCount"] = _VSCODE_evalResult.shape[0] |
29 | 29 | except TypeError: |
30 | | - _VSCODE_targetVariable['rowCount'] = 0 |
31 | | - elif (hasattr(_VSCODE_evalResult, '__len__')): |
| 30 | + _VSCODE_targetVariable["rowCount"] = 0 |
| 31 | + elif hasattr(_VSCODE_evalResult, "__len__"): |
32 | 32 | try: |
33 | | - _VSCODE_targetVariable['rowCount'] = len(_VSCODE_evalResult) |
| 33 | + _VSCODE_targetVariable["rowCount"] = len(_VSCODE_evalResult) |
34 | 34 | except TypeError: |
35 | | - _VSCODE_targetVariable['rowCount'] = 0 |
| 35 | + _VSCODE_targetVariable["rowCount"] = 0 |
36 | 36 |
|
37 | 37 | # Turn the eval result into a df |
38 | 38 | _VSCODE_df = _VSCODE_evalResult |
|
43 | 43 | elif isinstance(_VSCODE_evalResult, dict): |
44 | 44 | _VSCODE_evalResult = _VSCODE_pd.Series(_VSCODE_evalResult) |
45 | 45 | _VSCODE_df = _VSCODE_pd.Series.to_frame(_VSCODE_evalResult) |
46 | | - elif _VSCODE_targetVariable['type'] == 'ndarray': |
| 46 | + elif _VSCODE_targetVariable["type"] == "ndarray": |
47 | 47 | _VSCODE_df = _VSCODE_pd.DataFrame(_VSCODE_evalResult) |
48 | 48 |
|
49 | 49 | # If any rows, use pandas json to convert a single row to json. Extract |
50 | 50 | # the column names and types from the json so we match what we'll fetch when |
51 | 51 | # we ask for all of the rows |
52 | | - if _VSCODE_targetVariable['rowCount']: |
| 52 | + if _VSCODE_targetVariable["rowCount"]: |
53 | 53 | try: |
54 | 54 | _VSCODE_row = _VSCODE_df.iloc[0:1] |
55 | | - _VSCODE_json_row = _VSCODE_pd_json.to_json(None, _VSCODE_row, date_format='iso') |
| 55 | + _VSCODE_json_row = _VSCODE_pd_json.to_json( |
| 56 | + None, _VSCODE_row, date_format="iso" |
| 57 | + ) |
56 | 58 | _VSCODE_columnNames = list(_VSCODE_json.loads(_VSCODE_json_row)) |
57 | 59 | del _VSCODE_row |
58 | 60 | del _VSCODE_json_row |
|
62 | 64 | _VSCODE_columnNames = list(_VSCODE_df) |
63 | 65 |
|
64 | 66 | # Compute the index column. It may have been renamed |
65 | | - _VSCODE_indexColumn = _VSCODE_df.index.name if _VSCODE_df.index.name else 'index' |
| 67 | + _VSCODE_indexColumn = _VSCODE_df.index.name if _VSCODE_df.index.name else "index" |
66 | 68 | _VSCODE_columnTypes = list(_VSCODE_df.dtypes) |
67 | 69 | del _VSCODE_df |
68 | 70 |
|
69 | 71 | # Make sure the index column exists |
70 | 72 | if _VSCODE_indexColumn not in _VSCODE_columnNames: |
71 | 73 | _VSCODE_columnNames.insert(0, _VSCODE_indexColumn) |
72 | | - _VSCODE_columnTypes.insert(0, 'int64') |
| 74 | + _VSCODE_columnTypes.insert(0, "int64") |
73 | 75 |
|
74 | 76 | # Then loop and generate our output json |
75 | 77 | _VSCODE_columns = [] |
76 | 78 | for _VSCODE_n in range(0, len(_VSCODE_columnNames)): |
77 | 79 | _VSCODE_column_type = _VSCODE_columnTypes[_VSCODE_n] |
78 | 80 | _VSCODE_column_name = str(_VSCODE_columnNames[_VSCODE_n]) |
79 | 81 | _VSCODE_colobj = {} |
80 | | - _VSCODE_colobj['key'] = _VSCODE_column_name |
81 | | - _VSCODE_colobj['name'] = _VSCODE_column_name |
82 | | - _VSCODE_colobj['type'] = str(_VSCODE_column_type) |
| 82 | + _VSCODE_colobj["key"] = _VSCODE_column_name |
| 83 | + _VSCODE_colobj["name"] = _VSCODE_column_name |
| 84 | + _VSCODE_colobj["type"] = str(_VSCODE_column_type) |
83 | 85 | _VSCODE_columns.append(_VSCODE_colobj) |
84 | 86 | del _VSCODE_column_name |
85 | 87 | del _VSCODE_column_type |
|
88 | 90 | del _VSCODE_columnTypes |
89 | 91 |
|
90 | 92 | # Save this in our target |
91 | | - _VSCODE_targetVariable['columns'] = _VSCODE_columns |
92 | | - _VSCODE_targetVariable['indexColumn'] = _VSCODE_indexColumn |
| 93 | + _VSCODE_targetVariable["columns"] = _VSCODE_columns |
| 94 | + _VSCODE_targetVariable["indexColumn"] = _VSCODE_indexColumn |
93 | 95 | del _VSCODE_columns |
94 | 96 | del _VSCODE_indexColumn |
95 | 97 |
|
96 | | - |
97 | 98 | # Transform this back into a string |
98 | 99 | print(_VSCODE_json.dumps(_VSCODE_targetVariable)) |
99 | 100 | del _VSCODE_targetVariable |
|
0 commit comments