|
10 | 10 | # Indexes off of _VSCODE_targetVariable need to index types that are part of IJupyterVariable
|
11 | 11 | _VSCODE_targetVariable = _VSCODE_json.loads("""_VSCode_JupyterTestValue""")
|
12 | 12 |
|
| 13 | +# Function to compute row count for a value |
| 14 | +def _VSCODE_getRowCount(var): |
| 15 | + if hasattr(var, "shape"): |
| 16 | + try: |
| 17 | + # Get a bit more restrictive with exactly what we want to count as a shape, since anything can define it |
| 18 | + if isinstance(var.shape, tuple): |
| 19 | + return var.shape[0] |
| 20 | + except TypeError: |
| 21 | + return 0 |
| 22 | + elif hasattr(var, "__len__"): |
| 23 | + try: |
| 24 | + return len(var) |
| 25 | + except TypeError: |
| 26 | + return 0 |
| 27 | + |
| 28 | + |
13 | 29 | # First check to see if we are a supported type, this prevents us from adding types that are not supported
|
14 | 30 | # and also keeps our types in sync with what the variable explorer says that we support
|
15 | 31 | if _VSCODE_targetVariable["type"] not in _VSCode_supportsDataExplorer:
|
|
21 | 37 | _VSCODE_evalResult = eval(_VSCODE_targetVariable["name"])
|
22 | 38 |
|
23 | 39 | # 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 |
| 40 | + _VSCODE_targetVariable["rowCount"] = _VSCODE_getRowCount(_VSCODE_evalResult) |
36 | 41 |
|
37 | 42 | # Turn the eval result into a df
|
38 | 43 | _VSCODE_df = _VSCODE_evalResult
|
|
45 | 50 | _VSCODE_df = _VSCODE_pd.Series.to_frame(_VSCODE_evalResult)
|
46 | 51 | elif _VSCODE_targetVariable["type"] == "ndarray":
|
47 | 52 | _VSCODE_df = _VSCODE_pd.DataFrame(_VSCODE_evalResult)
|
| 53 | + elif hasattr(_VSCODE_df, "toPandas"): |
| 54 | + _VSCODE_df = _VSCODE_df.toPandas() |
| 55 | + _VSCODE_targetVariable["rowCount"] = _VSCODE_getRowCount(_VSCODE_df) |
48 | 56 |
|
49 | 57 | # If any rows, use pandas json to convert a single row to json. Extract
|
50 | 58 | # the column names and types from the json so we match what we'll fetch when
|
|
0 commit comments