-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.js
More file actions
286 lines (268 loc) · 8.2 KB
/
Copy pathutils.js
File metadata and controls
286 lines (268 loc) · 8.2 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import { Blob } from "blob-polyfill";
/* eslint-disable prefer-const */
// Function for cell on going activity
function cellActivity(type, message) {
if (type === "loading") {
document.getElementById("play").style.display = "none";
document.getElementById("activity-loader").style.display = "block";
document.getElementById(`out_${window.current_cell}`).innerHTML = "";
} else if (type === "success") {
document.getElementById("play").style.display = "block";
document.getElementById("activity-loader").style.display = "none";
document.getElementById(`out_${window.current_cell}`).innerHTML = message;
document.getElementById(`out_${window.current_cell}`).style.color = "white";
} else {
document.getElementById("play").style.display = "block";
document.getElementById("activity-loader").style.display = "none";
document.getElementById(`out_${window.current_cell}`).innerHTML = message;
document.getElementById(`out_${window.current_cell}`).style.color = "red";
}
}
/* eslint-disable no-eval */
/* eslint-disable no-multi-assign */
/* eslint-disable no-plusplus */
export function print_val(val) {
/// Neeed refactoring. Lot of duplicated line of code
if (Array.isArray(val[0])) {
const row_length = val.length;
let data_string = "[";
if (row_length > 10) {
for (let i = 0; i < 10; i++) {
const row_val = val[i];
const col_length = row_val.length;
data_string += "[";
if (col_length > 10) {
for (let j = 0; j < 10; j++) {
data_string += j === 9 ? `${row_val[j]}` : `${row_val[j]},`;
}
data_string += `.......${col_length - 10} more],`;
} else {
for (let j = 0; j < col_length; j++) {
data_string +=
j === col_length - 1 ? `${row_val[j]}` : `${row_val[j]},`;
}
data_string += i === row_length - 1 ? "]" : "],";
}
}
data_string += `...${row_length - 10} more]`;
} else {
for (let i = 0; i < row_length; i++) {
const row_val = val[i];
const col_length = row_val.length;
data_string += "[";
if (col_length > 10) {
for (let j = 0; j < 10; j++) {
data_string += j === 9 ? `${row_val[j]}` : `${row_val[j]},`;
}
data_string += `.......${col_length - 10} more],`;
} else {
for (let j = 0; j < col_length; j++) {
data_string +=
j === col_length - 1 ? `${row_val[j]}` : `${row_val[j]},`;
}
data_string += i === row_length - 1 ? "]" : "],";
}
}
data_string += "]";
}
return data_string;
}
const row_length = val.length;
let data_string = "[";
const count = row_length > 10 ? 10 : row_length;
for (let i = 0; i < count; i++) {
data_string += i === count - 1 ? `${val[i]}` : `${val[i]},`;
}
const diff = row_length - count;
if (diff > 0) {
data_string += `....${diff} more]`;
} else {
data_string += "]";
}
return data_string;
}
/**
* Displays Danfo DataFrame/Series in a formated table
* @param {DataFrame} df
*/
export function table(df) {
const { col_types, series, columns, index, values } = df;
let head = "";
if (series) {
head += `<th class="${col_types[0]}">${columns}</th>`;
} else {
columns.forEach((name, i) => {
head += `<th class="${col_types[i]}">${name}</th>`;
});
}
let body = "";
values.forEach((row, i) => {
let b_String = `<tr><th>${index[i]}</th>`;
if (series) {
b_String += `<td class="${col_types[0]}">${row}</td>`;
} else {
row.forEach((v, j) => {
b_String += `<td class="${col_types[j]}">${v}</td>`;
});
}
b_String += "</tr>";
body += b_String;
});
// eslint-disable-next-line no-shadow
const table = `
<div style="overflow: auto; max-height: 300px;"><table class="df-table" border="1">
<thead>
<tr>
<th></th>
${head}
</tr>
</thead>
<tbody>
${body}
</tbody>
</table>
</div>
`;
return table;
}
export const downLoad_notebook = (state, name) => {
const data = JSON.stringify(state.cells);
const blob = new Blob([data], { type: "application/json" });
const url = (window.URL || window.webkitURL).createObjectURL(blob);
const link = document.createElement("a");
let fileName = "Dnote-react";
if (name) {
// eslint-disable-next-line prefer-destructuring
fileName = name.split(".")[0];
}
link.download = `${fileName}.json`;
link.href = url;
document.body.appendChild(link);
link.click();
link.remove();
return link;
};
/**
* load package scripts via CDN into scope
* @param {Array} array of package CDNs to load
* @param {*} callback
*/
// eslint-disable-next-line consistent-return
export const load_package = async (array, callback) => {
cellActivity("loading", "");
const loader = function (src, handler) {
const script = document.createElement("script");
script.type = "text/javascript";
script.src = src;
script.onload = script.onreadystatechange = function () {
script.onreadystatechange = script.onload = null;
handler();
cellActivity("success", "Package successfully loaded");
};
script.onerror = function (error) {
cellActivity(
"error",
`Failed to load package ${error.path[0].src}. Check internet connection or package url`
);
};
script.async = true;
document.body.appendChild(script);
eval(script);
};
(function run() {
if (array.length !== 0) {
loader(array.shift(), run);
} else {
// eslint-disable-next-line no-unused-expressions
callback && callback();
}
})();
return "done";
};
export const load_notebook = (dispatch) => {
let fileName = null;
const { files } = document.getElementById("import-notebook-file");
let json_content = null;
if (files.length > 0) {
const content = files[0];
const reader = new FileReader();
reader.onload = function (t) {
json_content = t.target.result;
const json = JSON.parse(json_content);
dispatch({ type: "LOAD_NOTE", payload: json });
};
reader.readAsText(content);
fileName = content.name;
}
return fileName;
};
/**
* Returns the id of the current cell's output div
*/
function this_div() {
const id = `out_${window.current_cell}`;
const rand_div_name = `random_div_${id}`;
const html = `
<div class="col-md-1"></div>
<div class="col-md-9" id=${rand_div_name}>
</div>
<div class="col-md-2"></div>
`;
document.getElementById(id).innerHTML += html;
return rand_div_name;
}
/**
* Creates multiple divs for plotting in an output cell
* @param {String} name of the div to create
* @param {Function} callback
*/
// eslint-disable-next-line no-unused-vars
function viz(name, callback) {
// utility function to enabling ploting
// create the ploting div needed
// eslint-disable-next-line prefer-const
let id = `out_${window.current_cell}`;
const a = (document.getElementById(id).innerHTML += `<div id=${name}></div>`);
// eslint-disable-next-line no-unused-vars
let cb = callback(name);
return a;
// eslint-disable-next-line no-unused-vars
}
/**
* Helper function to load CSV data into Danfo.js Data Structure
* @param {String} path to CSV data.
*/
// eslint-disable-next-line consistent-return
export async function load_csv(path) {
cellActivity("loading", "");
try {
// eslint-disable-next-line no-undef
const df = await dfd.read_csv(path);
cellActivity("success", "Successfully loaded csv");
return df;
} catch (error) {
return cellActivity(
"error",
"Failed to load csv. Check your internet connection or your csv path"
);
}
}
/**
* Helper function to easily log output from for loops in Dom
* @param {*} args
*/
function forLoop_log(args) {
let id = `out_${window.current_cell}`;
let logs = document.createElement("p");
logs.append(`${args}`);
document.getElementById(id).append(logs);
}
console.forlog = forLoop_log;
export const makeGlobal = () => {
window.print_val = print_val;
window.table = table;
window.load_package = load_package;
window.load_csv = load_csv;
window.this_div = this_div;
window.viz = viz;
};