forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathjupyterVariableDataProvider.ts
More file actions
113 lines (102 loc) · 4.18 KB
/
jupyterVariableDataProvider.ts
File metadata and controls
113 lines (102 loc) · 4.18 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import '../../common/extensions';
import { inject, injectable, named } from 'inversify';
import { Identifiers } from '../constants';
import { IJupyterVariable, IJupyterVariableDataProvider, IJupyterVariables, INotebook } from '../types';
import { DataViewerDependencyService } from './dataViewerDependencyService';
import { ColumnType, IDataFrameInfo, IRowsResponse } from './types';
@injectable()
export class JupyterVariableDataProvider implements IJupyterVariableDataProvider {
private initialized: boolean = false;
private notebook: INotebook | undefined;
private variable: IJupyterVariable | undefined;
constructor(
@inject(IJupyterVariables) @named(Identifiers.ALL_VARIABLES) private variableManager: IJupyterVariables,
@inject(DataViewerDependencyService) private dependencyService: DataViewerDependencyService
) {}
/**
* Normalizes column types to the types the UI component understands.
* Defaults to 'string'.
* @param columns
* @returns Array of columns with normalized type
*/
private static getNormalizedColumns(columns: { key: string; type: string }[]): { key: string; type: ColumnType }[] {
return columns.map((column: { key: string; type: string }) => {
let normalizedType: ColumnType;
switch (column.type) {
case 'bool':
normalizedType = ColumnType.Bool;
break;
case 'integer':
case 'int32':
case 'int64':
case 'float':
case 'float32':
case 'float64':
case 'number':
normalizedType = ColumnType.Number;
break;
default:
normalizedType = ColumnType.String;
}
return {
key: column.key,
type: normalizedType
};
});
}
public dispose(): void {
return;
}
public setDependencies(variable: IJupyterVariable, notebook?: INotebook): void {
this.notebook = notebook;
this.variable = variable;
}
public async getDataFrameInfo(): Promise<IDataFrameInfo> {
let dataFrameInfo: IDataFrameInfo = {};
await this.ensureInitialized();
if (this.variable) {
dataFrameInfo = {
columns: this.variable.columns
? JupyterVariableDataProvider.getNormalizedColumns(this.variable.columns)
: this.variable.columns,
indexColumn: this.variable.indexColumn,
rowCount: this.variable.rowCount
};
}
return dataFrameInfo;
}
public async getAllRows() {
let allRows: IRowsResponse = [];
await this.ensureInitialized();
if (this.variable && this.variable.rowCount) {
const dataFrameRows = await this.variableManager.getDataFrameRows(
this.variable,
0,
this.variable.rowCount,
this.notebook
);
allRows = dataFrameRows && dataFrameRows.data ? (dataFrameRows.data as IRowsResponse) : [];
}
return allRows;
}
public async getRows(start: number, end: number) {
let rows: IRowsResponse = [];
await this.ensureInitialized();
if (this.variable && this.variable.rowCount) {
const dataFrameRows = await this.variableManager.getDataFrameRows(this.variable, start, end, this.notebook);
rows = dataFrameRows && dataFrameRows.data ? (dataFrameRows.data as IRowsResponse) : [];
}
return rows;
}
private async ensureInitialized(): Promise<void> {
// Postpone pre-req and variable initialization until data is requested.
if (!this.initialized && this.variable) {
this.initialized = true;
await this.dependencyService.checkAndInstallMissingDependencies(this.notebook?.getMatchingInterpreter());
this.variable = await this.variableManager.getDataFrameInfo(this.variable, this.notebook);
}
}
}