Skip to content

Commit 82abee1

Browse files
committed
add console.forlog for forloop and fix the printing of array
1 parent 8aed994 commit 82abee1

2 files changed

Lines changed: 23 additions & 16 deletions

File tree

notebookjs/src/public/javascripts/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@ function exec_cell(c_id) {
5151

5252
try {
5353
let output = ("global", eval)(vars_in_scope[id].getValue())
54+
let command = vars_in_scope[id].getValue()
5455
if (Array.isArray(output)) {
5556
output = print_val(output)
5657
} else if (typeof output === 'object' && output !== null) {
5758
output = JSON.stringify(output)
5859
if (output == "{}") {
5960
output = ""
6061
}
61-
} else if (console) {
62+
} else if (command.includes("console.log(")) {
6263
//retreive value from the console funcction
6364
console.oldLog = console.log;
6465
console.log = function (value) {
@@ -80,7 +81,7 @@ function exec_cell(c_id) {
8081
}
8182

8283
// $(`#out_${id}`).empty()
83-
let command = vars_in_scope[id].getValue()
84+
// let command = vars_in_scope[id].getValue()
8485
if (command.includes("table") || command.includes("plot") || command.includes("console.log(")){
8586
// $(`#out_${id}`).html("")
8687
$(`#out_${id}`).html(output);

notebookjs/src/public/javascripts/utils.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11

22
function print_val(val) {
3+
/// Neeed refactoring. Lot of duplicated line of code
34

45
if (Array.isArray(val[0])) {
56

6-
let col_length = val[0].length;
7+
78
let row_length = val.length;
89

910
let data_string = "[";
@@ -12,21 +13,21 @@ function print_val(val) {
1213
for (let i = 0; i < 10; i++) {
1314

1415
let row_val = val[i]
15-
16+
let col_length = row_val.length;
1617
data_string += "[";
1718
if (col_length > 10) {
1819

1920
for (let j = 0; j < 10; j++) {
20-
data_string += `${row_val[j]},`
21+
data_string += j== 9 ? `${row_val[j]}`:`${row_val[j]},`
2122
}
2223

2324
data_string += `.......${col_length - 10} more],`
2425
} else {
2526

2627
for (let j = 0; j < col_length; j++) {
27-
data_string += `${row_val[j]},`
28+
data_string += j== (col_length-1) ? `${row_val[j]}` :`${row_val[j]},`
2829
}
29-
data_string += "],"
30+
data_string += i == (row_length-1) ? "]" : "],"
3031
}
3132
}
3233
data_string += `...${row_length - 10} more]`
@@ -35,21 +36,23 @@ function print_val(val) {
3536
for (let i = 0; i < row_length; i++) {
3637

3738
let row_val = val[i]
39+
let col_length = row_val.length;
3840

3941
data_string += "[";
4042
if (col_length > 10) {
4143

4244
for (let j = 0; j < 10; j++) {
43-
data_string += `${row_val[j]},`
45+
data_string += j== 9 ? `${row_val[j]}`:`${row_val[j]},`
4446
}
4547

4648
data_string += `.......${col_length - 10} more],`
4749
} else {
4850

4951
for (let j = 0; j < col_length; j++) {
50-
data_string += `${row_val[j]},`
52+
53+
data_string += j== (col_length-1) ? `${row_val[j]}` :`${row_val[j]},`
5154
}
52-
data_string += "],"
55+
data_string += i == (row_length-1) ? "]" : "],"
5356
}
5457
}
5558
data_string += "]"
@@ -64,8 +67,7 @@ function print_val(val) {
6467
let count = row_length > 10 ? 10 : row_length
6568

6669
for (let i = 0; i < count; i++) {
67-
68-
data_string += `${val[i]},`
70+
data_string += i == (count-1) ? `${val[i]}` : `${val[i]},`
6971
}
7072

7173
let diff = row_length - count;
@@ -104,8 +106,7 @@ function viz(name, callback) {
104106

105107
let id = `#out_${window.current_cell}`
106108
$(`${id}`).append(`<div id=${name}></div>`)
107-
108-
return cb
109+
let cb = callback(name);
109110
}
110111

111112
function table(df) {
@@ -389,7 +390,12 @@ function load_notebook(json) {
389390
}
390391
}
391392

393+
function forLoop_log(args){
392394

393-
// function show_error_modal(){
395+
let id = `#out_${window.current_cell}`
396+
397+
$(`${id}`).append(`${args}<br />`)
398+
399+
}
394400

395-
// }
401+
console.forlog = forLoop_log

0 commit comments

Comments
 (0)