11export const getMatrix = (
2- varName : string ,
3- startRow : number ,
4- endRow : number ,
5- startColumn : number ,
6- endColumn : number
7- ) : string =>
8- `
9- import importlib
10- from IPython.display import JSON
11-
12- def __get_variable_shape(obj):
13- if hasattr(obj, 'shape'):
14- return " x ".join(map(str, obj.shape))
15- if isinstance(obj, list):
16- if obj and all(isinstance(el, list) for el in obj):
17- if len(set(map(len, obj))) == 1:
18- return f"{len(obj)} x {len(obj[0])}"
19- else:
20- return f"{len(obj)}"
21- return str(len(obj))
22- return ""
23-
24- def __format_content(item):
25- if isinstance(item, list):
26- return [__format_content(subitem) for subitem in item]
27- elif isinstance(item, str):
28- return item[:50] + "..." if len(item) > 50 else item
29- elif isinstance(item, (int, float, bool)) or item is None:
30- return item
31- elif isinstance(item, dict):
32- return {k: __format_content(v) for k, v in item.items()}
33- else:
34- if hasattr(item, "name"):
35- return getattr(item, "name")
36- return type(item).__name__
37-
38- def __mljar_variable_inspector_get_matrix_content(
39- var_name="${ varName } ",
40- start_row=${ startRow } ,
41- end_row=${ endRow } ,
42- start_column=${ startColumn } ,
43- end_column=${ endColumn }
44- ):
45- if var_name not in globals():
46- return JSON({"error": "Variable not found."})
47-
48- obj = globals()[var_name]
49- module_name = type(obj).__module__
50- var_type = type(obj).__name__
51- var_shape = __get_variable_shape(obj)
52-
53- if "numpy" in module_name:
54- try:
55- np = importlib.import_module("numpy")
56- except ImportError:
57- return JSON({"error": "Numpy is not installed."})
58- if isinstance(obj, np.ndarray):
59- if obj.ndim > 2:
60- return JSON({
61- "variable": var_name,
62- "variableType": var_type,
63- "variableShape": var_shape,
64- "error": "Numpy array has more than 2 dimensions."
65- })
66- if obj.ndim == 1:
67- actual_end_row = min(end_row, len(obj))
68- sliced = obj[start_row:actual_end_row]
69- returnedSize = [start_row, actual_end_row, 0, 1]
70- else:
71- actual_end_row = min(end_row, obj.shape[0])
72- actual_end_column = min(end_column, obj.shape[1])
73- sliced = obj[start_row:actual_end_row, start_column:actual_end_column]
74- returnedSize = [start_row, actual_end_row, start_column, actual_end_column]
75- return JSON({
76- "variable": var_name,
77- "variableType": var_type,
78- "variableShape": var_shape,
79- "returnedSize": returnedSize,
80- "content": __format_content(sliced.tolist())
81- })
82-
83- if "pandas" in module_name:
84- try:
85- pd = importlib.import_module("pandas")
86- except ImportError:
87- return JSON({"error": "Pandas is not installed."})
88- if isinstance(obj, pd.DataFrame):
89- actual_end_row = min(end_row, len(obj.index))
90- actual_end_column = min(end_column, len(obj.columns))
91- sliced = obj.iloc[start_row:actual_end_row, start_column:actual_end_column]
92- result = []
93- for col in sliced.columns:
94- col_values = [col] + sliced[col].tolist()
95- result.append(col_values)
96- returnedSize = [start_row, actual_end_row, start_column, actual_end_column]
97- return JSON({
98- "variable": var_name,
99- "variableType": var_type,
100- "variableShape": var_shape,
101- "returnedSize": returnedSize,
102- "content": __format_content(result)
103- })
104- elif isinstance(obj, pd.Series):
105- actual_end_row = min(end_row, len(obj))
106- sliced = obj.iloc[start_row:actual_end_row]
107- df = sliced.to_frame()
108- result = []
109- for col in df.columns:
110- col_values = [col] + df[col].tolist()
111- result.append(col_values)
112- returnedSize = [start_row, actual_end_row, 0, 1]
113- return JSON({
114- "variable": var_name,
115- "variableType": var_type,
116- "variableShape": var_shape,
117- "returnedSize": returnedSize,
118- "content": __format_content(result)
119- })
120-
121- if isinstance(obj, list):
122- if all(isinstance(el, list) for el in obj):
123- if len(set(map(len, obj))) == 1:
124- actual_end_row = min(end_row, len(obj))
125- actual_end_column = min(end_column, len(obj[0]))
126- sliced = [row[start_column:actual_end_column] for row in obj[start_row:actual_end_row]]
127- returnedSize = [start_row, actual_end_row, start_column, actual_end_column]
128- content = __format_content(sliced)
129- else:
130- actual_end_row = min(end_row, len(obj))
131- sliced = obj[start_row:actual_end_row]
132- returnedSize = [start_row, actual_end_row, 0, 1]
133- content = ["list" for _ in sliced]
134- var_shape = f"{len(obj)}"
135- return JSON({
136- "variable": var_name,
137- "variableType": var_type,
138- "variableShape": var_shape,
139- "returnedSize": returnedSize,
140- "content": content
141- })
142- else:
143- actual_end_row = min(end_row, len(obj))
144- sliced = obj[start_row:actual_end_row]
145- returnedSize = [start_row, actual_end_row, 0, 1]
146- return JSON({
147- "variable": var_name,
148- "variableType": var_type,
149- "variableShape": str(len(obj)),
150- "returnedSize": returnedSize,
151- "content": __format_content(sliced)
152- })
153-
154- if isinstance(obj, dict):
155- items = list(obj.items())[start_row:end_row]
156- sliced_dict = dict(items)
157- returnedSize = [start_row, end_row, 0, 1]
158- var_shape = str(len(obj))
159- return JSON({
160- "variable": var_name,
161- "variableType": var_type,
162- "variableShape": var_shape,
163- "returnedSize": returnedSize,
164- "content": __format_content(sliced_dict)
165- })
166-
167- return JSON({
168- "variable": var_name,
169- "variableType": var_type,
170- "variableShape": "unknown",
171- "error": "Variable is not a supported array type.",
172- "content": [10,10,10]
173- })
174-
175- __mljar_variable_inspector_get_matrix_content()
176- ` ;
177-
2+ varName : string ,
3+ startRow : number ,
4+ endRow : number ,
5+ startColumn : number ,
6+ endColumn : number
7+ ) : string => `
8+ import importlib
9+ from IPython.display import JSON
10+
11+ def __get_variable_shape(obj):
12+ if hasattr(obj, 'shape'):
13+ return " x ".join(map(str, obj.shape))
14+ if isinstance(obj, list):
15+ if obj and all(isinstance(el, list) for el in obj):
16+ if len(set(map(len, obj))) == 1:
17+ return f"{len(obj)} x {len(obj[0])}"
18+ else:
19+ return f"{len(obj)}"
20+ return str(len(obj))
21+ return ""
22+
23+ def __format_content(item):
24+ if isinstance(item, list):
25+ return [__format_content(subitem) for subitem in item]
26+ elif isinstance(item, dict):
27+ return {k: __format_content(v) for k, v in item.items()}
28+ elif isinstance(item, str):
29+ return item[:50] + "..." if len(item) > 50 else item
30+ elif isinstance(item, (int, float, bool)) or item is None:
31+ return item
32+ else:
33+ if hasattr(item, "name"):
34+ return getattr(item, "name")
35+ return type(item).__name__
36+
37+ def __mljar_variable_inspector_get_matrix_content(
38+ var_name="${ varName } ",
39+ start_row=${ startRow } ,
40+ end_row=${ endRow } ,
41+ start_column=${ startColumn } ,
42+ end_column=${ endColumn }
43+ ):
44+ if var_name not in globals():
45+ return JSON({"error": "Variable not found."})
46+
47+ obj = globals()[var_name]
48+ module_name = type(obj).__module__
49+ var_type = type(obj).__name__
50+ var_shape = __get_variable_shape(obj)
51+
52+ if "numpy" in module_name:
53+ try:
54+ np = importlib.import_module("numpy")
55+ except ImportError:
56+ return JSON({"error": "Numpy is not installed."})
57+ if isinstance(obj, np.ndarray):
58+ if obj.ndim > 2:
59+ return JSON({
60+ "variable": var_name,
61+ "variableType": var_type,
62+ "variableShape": var_shape,
63+ "error": "Numpy array has more than 2 dimensions."
64+ })
65+ if obj.ndim == 1:
66+ actual_end_row = min(end_row, len(obj))
67+ sliced = obj[start_row:actual_end_row]
68+ returnedSize = [start_row, actual_end_row, 0, 1]
69+ else:
70+ actual_end_row = min(end_row, obj.shape[0])
71+ actual_end_column = min(end_column, obj.shape[1])
72+ sliced = obj[start_row:actual_end_row, start_column:actual_end_column]
73+ returnedSize = [start_row, actual_end_row, start_column, actual_end_column]
74+ return JSON({
75+ "variable": var_name,
76+ "variableType": var_type,
77+ "variableShape": var_shape,
78+ "returnedSize": returnedSize,
79+ "content": __format_content(sliced.tolist())
80+ })
81+
82+ if "pandas" in module_name:
83+ try:
84+ pd = importlib.import_module("pandas")
85+ except ImportError:
86+ return JSON({"error": "Pandas is not installed."})
87+ if isinstance(obj, pd.DataFrame):
88+ actual_end_row = min(end_row, len(obj.index))
89+ actual_end_column = min(end_column, len(obj.columns))
90+ sliced = obj.iloc[start_row:actual_end_row, start_column:actual_end_column]
91+ result = []
92+ for col in sliced.columns:
93+ col_values = [col] + sliced[col].tolist()
94+ result.append(col_values)
95+ returnedSize = [start_row, actual_end_row, start_column, actual_end_column]
96+ return JSON({
97+ "variable": var_name,
98+ "variableType": var_type,
99+ "variableShape": var_shape,
100+ "returnedSize": returnedSize,
101+ "content": __format_content(result)
102+ })
103+ elif isinstance(obj, pd.Series):
104+ actual_end_row = min(end_row, len(obj))
105+ sliced = obj.iloc[start_row:actual_end_row]
106+ df = sliced.to_frame()
107+ result = []
108+ for col in df.columns:
109+ col_values = [col] + df[col].tolist()
110+ result.append(col_values)
111+ returnedSize = [start_row, actual_end_row, 0, 1]
112+ return JSON({
113+ "variable": var_name,
114+ "variableType": var_type,
115+ "variableShape": var_shape,
116+ "returnedSize": returnedSize,
117+ "content": __format_content(result)
118+ })
119+
120+ if isinstance(obj, list):
121+ if all(isinstance(el, list) for el in obj):
122+ if len(set(map(len, obj))) == 1:
123+ actual_end_row = min(end_row, len(obj))
124+ actual_end_column = min(end_column, len(obj[0]))
125+ sliced = [row[start_column:actual_end_column] for row in obj[start_row:actual_end_row]]
126+ returnedSize = [start_row, actual_end_row, start_column, actual_end_column]
127+ content = __format_content(sliced)
128+ else:
129+ actual_end_row = min(end_row, len(obj))
130+ sliced = obj[start_row:actual_end_row]
131+ returnedSize = [start_row, actual_end_row, 0, 1]
132+ content = ["list" for _ in sliced]
133+ var_shape = f"{len(obj)}"
134+ return JSON({
135+ "variable": var_name,
136+ "variableType": var_type,
137+ "variableShape": var_shape,
138+ "returnedSize": returnedSize,
139+ "content": content
140+ })
141+ else:
142+ actual_end_row = min(end_row, len(obj))
143+ sliced = obj[start_row:actual_end_row]
144+ returnedSize = [start_row, actual_end_row, 0, 1]
145+ return JSON({
146+ "variable": var_name,
147+ "variableType": var_type,
148+ "variableShape": str(len(obj)),
149+ "returnedSize": returnedSize,
150+ "content": __format_content(sliced)
151+ })
152+
153+ if isinstance(obj, dict):
154+ items = list(obj.items())[start_row:end_row]
155+ sliced_dict = dict(items)
156+ returnedSize = [start_row, end_row, 0, 1]
157+ var_shape = str(len(obj))
158+ return JSON({
159+ "variable": var_name,
160+ "variableType": var_type,
161+ "variableShape": var_shape,
162+ "returnedSize": returnedSize,
163+ "content": __format_content(sliced_dict)
164+ })
165+
166+ return JSON({
167+ "variable": var_name,
168+ "variableType": var_type,
169+ "variableShape": "unknown",
170+ "error": "Variable is not a supported array type.",
171+ "content": [10, 10, 10]
172+ })
173+
174+ __mljar_variable_inspector_get_matrix_content()
175+ ` ;
0 commit comments