Skip to content

Commit dca5114

Browse files
committed
fix plot div bug
1 parent 4f0df1a commit dca5114

4 files changed

Lines changed: 94 additions & 54 deletions

File tree

notebookjs/src/public/javascripts/index.js

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -48,36 +48,44 @@ function exec_cell(c_id) {
4848
window.current_cell = id;
4949

5050
try {
51-
let output = ("global", eval)(vars_in_scope[id].getValue())
52-
if (Array.isArray(output)) {
53-
output = print_val(output)
54-
} else if (typeof output === 'object' && output !== null) {
55-
output = JSON.stringify(output)
56-
if (output == "{}") {
57-
output = ""
58-
}
59-
} else if (console) {
60-
//retreive value from the console funcction
61-
console.oldLog = console.log;
62-
console.log = function (value) {
63-
return value;
64-
};
65-
output = eval(vars_in_scope[id].getValue());
66-
51+
let command = vars_in_scope[id].getValue()
52+
if (command.includes("console.log(") || command.includes("table") || command.includes("plot")) {
53+
let output = ("global", eval)(vars_in_scope[id].getValue())
6754
if (Array.isArray(output)) {
6855
output = print_val(output)
69-
} else {
70-
if (typeof output === 'object' && output !== null) {
71-
output = JSON.stringify(output)
72-
if (output == "{}") {
73-
output = ""
74-
}
56+
} else if (typeof output === 'object' && output !== null) {
57+
output = JSON.stringify(output)
58+
if (output == "{}") {
59+
output = ""
7560
}
61+
} else if (console) {
62+
//retreive value from the console funcction
63+
console.oldLog = console.log;
64+
console.log = function (value) {
65+
return value;
66+
};
67+
output = eval(vars_in_scope[id].getValue());
68+
69+
if (Array.isArray(output)) {
70+
output = print_val(output)
71+
} else {
72+
if (typeof output === 'object' && output !== null) {
73+
output = JSON.stringify(output)
74+
if (output == "{}") {
75+
output = ""
76+
}
77+
}
7678

79+
}
80+
$(`#out_${id}`).html("");
81+
$(`#out_${id}`).html(output);
7782
}
83+
} else {
84+
("global", eval)(command)
85+
$(`#out_${id}`).html("");
7886
}
7987

80-
$(`#out_${id}`).html(output);
88+
// $(`#out_${id}`).html(output);
8189
// document.getElementById("cell_spinner-1").style.display = "none"
8290
// document.getElementById("cell_num-1").style.display = "block"
8391

@@ -86,6 +94,7 @@ function exec_cell(c_id) {
8694
window.current_cell = div_count
8795

8896
} catch (error) {
97+
$(`#out_${id}`).html("");
8998
$(`#out_${id}`).html(error)
9099
console.log(error)
91100
// document.getElementById("cell_spinner-1").style.display = "none"
@@ -156,13 +165,12 @@ function add_new_code_cell(c_id, where) {
156165
if (where == "up") {
157166
divReference.insertAdjacentHTML("beforebegin", html);
158167
let current_cell_id = cells_order.indexOf(`div-${id}`)
159-
cells_order.splice(current_cell_id,0,`div-${new_id}`)
168+
cells_order.splice(current_cell_id, 0, `div-${new_id}`)
160169
} else {
161170
divReference.insertAdjacentHTML("afterend", html);
162-
cells_order[new_id-1] = `div-${new_id}`
171+
cells_order[new_id - 1] = `div-${new_id}`
163172
}
164173

165-
console.log(cells_order)
166174
let editor = CodeMirror(document.getElementById(`div-${new_id}`), {
167175
lineNumbers: true,
168176
tabSize: 2,
@@ -199,7 +207,7 @@ function add_new_text_cell(c_id, where) {
199207
__code_cell_count += 1
200208
let last_scope_id = parseInt(Object.keys(vars_in_scope).pop().split("-")[1])
201209
let id = c_id.split("-")[1]
202-
210+
203211
if (where == "down") {
204212
where = "down"
205213
} else {
@@ -259,10 +267,10 @@ function add_new_text_cell(c_id, where) {
259267
if (where == "up") {
260268
divReference.insertAdjacentHTML("beforebegin", html);
261269
let current_cell_id = cells_order.indexOf(`div_text-${id}`)
262-
cells_order.splice(current_cell_id,0,`div_text-${new_id}`)
270+
cells_order.splice(current_cell_id, 0, `div_text-${new_id}`)
263271
} else {
264272
divReference.insertAdjacentHTML("afterend", html);
265-
cells_order[new_id-1] = `div_text-${new_id}`
273+
cells_order[new_id - 1] = `div_text-${new_id}`
266274
}
267275

268276
console.log(cells_order)
@@ -372,7 +380,7 @@ function update_text_box_size() {
372380

373381

374382
$("#download").click(function () {
375-
let out = notebook_json(cells_order,vars_in_scope, md_texts);
383+
let out = notebook_json(cells_order, vars_in_scope, md_texts);
376384

377385
var blob = new Blob([out], { "type": "application/json" });
378386
var url = (window.URL || window.webkitURL).createObjectURL(blob);
@@ -430,14 +438,6 @@ $("#import-notebook-file").change(() => {
430438

431439

432440

433-
async function load_data(path) {
434-
document.getElementById("cell-running").style.display = "block"
435-
let df = await dfd.read_csv(path)
436-
document.getElementById("cell-running").style.display = "none"
437-
return df
438-
439-
}
440-
441441

442442

443443

notebookjs/src/public/javascripts/utils.js

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,28 @@ function print_val(val) {
8080
}
8181

8282
function this_div() {
83-
let id = `#out_${window.current_cell}`
83+
let id = `cell-${window.current_cell.split("-")[1]}`
8484
let rand_div_name = `random_div_#${id}`
85-
$(`${id}`).append(`<div id=${rand_div_name}></div>`)
85+
html = `
86+
<div class="col-md-1"></div>
87+
<div class="col-md-9" id=${rand_div_name}>
88+
</div>
89+
<div class="col-md-2"></div>
90+
`
91+
92+
$(`#${id}`).append(html)
8693
return rand_div_name
8794
}
8895

96+
// function viz(name, callback) {
97+
// // out_div-1
98+
// let id = `#out_div${window.current_cell}`
99+
// $(`${id}`).append(`<div id=${name}></div>`)
89100

90-
function viz(name, callback) {
91-
// out_div-1
92-
let id = `#out_div${window.current_cell}`
93-
$(`${id}`).append(`<div id=${name}></div>`)
101+
// let cb = callback(name);
94102

95-
let cb = callback(name);
96-
97-
return cb
98-
}
103+
// return cb
104+
// }
99105

100106
function table(df) {
101107

@@ -151,11 +157,11 @@ function table(df) {
151157

152158
}
153159

154-
function notebook_json(cells_order,scope, md_scope) {
160+
function notebook_json(cells_order, scope, md_scope) {
155161

156162
var store = {}
157163

158-
for (let i=0; i < cells_order.length; i++) {
164+
for (let i = 0; i < cells_order.length; i++) {
159165

160166
let key = cells_order[i];
161167
let key_split = key.split("-")
@@ -192,7 +198,9 @@ function notebook_json(cells_order,scope, md_scope) {
192198

193199

194200
//load package scripts
195-
function LoadPackage(array, callback) {
201+
function load_package(array, callback) {
202+
203+
document.getElementById("cell-running").style.display = "block"
196204
let loader = function (src, handler) {
197205
let script = document.createElement("script");
198206
script.src = src;
@@ -209,9 +217,36 @@ function LoadPackage(array, callback) {
209217
} else {
210218
callback && callback();
211219
}
220+
document.getElementById("cell-running").style.display = "none"
212221
})();
213222
}
214223

224+
225+
async function load_data(path) {
226+
document.getElementById("cell-running").style.display = "block"
227+
let df = await dfd.read_csv(path)
228+
document.getElementById("cell-running").style.display = "none"
229+
return df
230+
// id = parseInt(window.current_cell.split("-")[1]) - 1 //window current cell starts from 2
231+
// $(`#out_div-${id}`).html(err);
232+
// document.getElementById("cell-running").style.display = "none"
233+
// return err
234+
235+
// dfd.read_csv(path).then((df) => {
236+
// document.getElementById("cell-running").style.display = "none"
237+
// return df
238+
// }).catch((err) => {
239+
// id = parseInt(window.current_cell.split("-")[1]) - 1 //window current cell starts from 2
240+
// $(`#out_div-${id}`).html(err);
241+
// document.getElementById("cell-running").style.display = "none"
242+
// return err
243+
// })
244+
245+
246+
}
247+
248+
249+
215250
function md_load(id) {
216251

217252
let md = `<div class="row" style="margin-top: 10px;" id="cell-${id}"></div>`
@@ -309,7 +344,7 @@ function load_notebook(json) {
309344
// document.getElementById("cell_spinner-1").style.display = "block"
310345
// document.getElementById("cell_num-1").style.display = "none"
311346
exec_cell(`run_div-${id}`);
312-
347+
313348
}
314349
});
315350

@@ -346,3 +381,7 @@ function load_notebook(json) {
346381
}
347382
}
348383

384+
385+
// function show_error_modal(){
386+
387+
// }

notebookjs/src/views/index.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<div class="col-md-2"> </div>
4040
{{!-- Output of code div start --}}
4141
<div class="col-md-1"></div>
42-
<div id="out_div-1" class="col-md-9 out-divs">
42+
<div id="out_div-1" style="padding: 20px;" class="col-md-9 out-divs">
4343
</div>
4444
<div class="col-md-2"></div>
4545

notebookjs/src/views/layout.hbs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
</head>
3939

4040
<body>
41-
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
41+
<nav class="navbar navbar-expand-lg navbar-dark bg-dark navbar-fixed-top">
4242
<a class="navbar-brand" href="/"><img style="width: 40px; width: 40px; margin-left: 5px; border-radius: 10px;"
4343
src="images/danfo.svg" /> notebook</a>
4444
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
@@ -112,6 +112,7 @@
112112
</div>
113113
</div>
114114

115+
115116
{{{body}}}
116117

117118

0 commit comments

Comments
 (0)