Skip to content

Commit b55c733

Browse files
committed
2 parents 540e41e + a0906d5 commit b55c733

5 files changed

Lines changed: 176 additions & 10 deletions

File tree

notebookjs/src/public/javascripts/index.js

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ function exec_cell(c_id) {
6565
if (Array.isArray(output)) {
6666
output = print_val(output)
6767
} else {
68-
output = JSON.stringify(output)
68+
if(typeof output === 'object' && output !== null){
69+
output = JSON.stringify(output)
70+
}
71+
6972
}
7073
}
7174

@@ -272,6 +275,7 @@ function delete_cell(id) {
272275
} else {
273276
row_id = `cell-${Number(id)}`
274277
var div_ele = document.getElementById(row_id);
278+
console.log(row_id, $(`#${row_id}`).parent().id)
275279
div_ele.parentNode.removeChild(div_ele);
276280
__code_cell_count -= 1
277281
}
@@ -294,6 +298,7 @@ $(document).on("click", "button.run", function () {
294298

295299
$(document).on("click", "button.del", function () {
296300
let id = this.id.split("_")[1]
301+
console.log(id,this.id, __code_cell_count)
297302
delete_cell(id)
298303
})
299304

@@ -319,15 +324,15 @@ $(document).on("click", "button.add-text", function () {
319324
add_new_text_cell(this.id, where)
320325
})
321326

322-
$(document).on("dblclick", "textarea.text-box", function () {
323-
let id = this.id.split("_")[1]
324-
show_md(id, this.value)
327+
// $(document).on("dblclick", "textarea.text-box", function () {
328+
// let id = this.id.split("_")[1]
329+
// show_md(id, this.value)
325330

326-
})
331+
// })
327332

328333
function show_md(id, value) {
329334
div_id = `text-div_${id}`
330-
// md_texts[div_id] = value //stores the markdown text for the corresponding div
335+
md_texts[div_id] = value //stores the markdown text for the corresponding div
331336
render_md = md.render(value)
332337
$(`#out-text-div_${id}`).html(render_md).show()
333338
document.getElementById(div_id).style.display = "none"
@@ -357,6 +362,36 @@ function update_text_box_size() {
357362

358363

359364
$("#download").click(function () {
360-
let out = notebook_json(vars_in_scope);
361-
console.log(out);
365+
let out = notebook_json(vars_in_scope,md_texts);
366+
367+
var blob = new Blob([out], { "type": "application/json" });
368+
var url = (window.URL || window.webkitURL).createObjectURL(blob);
369+
370+
var link = document.createElement('a');
371+
link.download = 'danfo_notebook.json';
372+
link.href = url;
373+
374+
var link_pae = $(link);
375+
$("body").append(link_pae);//maybe needed
376+
link.click();
377+
link_pae.remove();
362378
});
379+
380+
$("#uploadnb").click(function() {
381+
382+
var files = $("#import-notebook-file")[0].files
383+
let json_content = null
384+
if(files.length > 0){
385+
var content = files[0];
386+
var reader = new FileReader();
387+
reader.onload = function(t){
388+
json_content = t.target.result;
389+
let json = JSON.parse(json_content)
390+
391+
$(".content").empty()
392+
393+
load_notebook(json);
394+
}
395+
reader.readAsText(content);
396+
}
397+
})

notebookjs/src/public/javascripts/utils.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,129 @@ function LoadPackage(array, callback) {
204204
})();
205205
}
206206

207+
function md_load(id){
208+
209+
let md = `<div class="row" style="margin-top: 10px;" id="cell-${id}"></div>`
210+
211+
return md;
212+
}
213+
214+
function html_load(new_id){
215+
216+
let html = `
217+
<div class="row" style="margin-top: 10px;" id="cell-${new_id}">
218+
<div class="col-md-1">
219+
<p id="cell-num" class="code_symbol">[${new_id}]</p>
220+
</div>
221+
<div id="div-${new_id}" class="col-md-9">
222+
<div id="btn-actions-${new_id}" class="btn-group-horizontal" style="display: none;">
223+
<button type="button" id="run_div-${new_id}" class="btn btn-sm btn-success run"><i
224+
class="fas fa-play"></i>Run</button>
225+
<div class="btn-group" role="group" aria-label="Basic example">
226+
227+
<button type="button" id="add_code_down_btn-${new_id}" class="btn btn-sm btn-info add-code">
228+
<i class="fas fa-sort-down" style="margin-top: -10px;"></i> Code
229+
</button>
230+
<button type="button" id="add_code_up_btn-${new_id}" class="btn btn-sm btn-info add-code">
231+
<i class="fas fa-sort-up"></i> Code
232+
</button>
233+
234+
</div>
235+
236+
<div class="btn-group" role="group" aria-label="Basic example">
237+
238+
<button type="button" id="add_text_down_btn-${new_id}" class="btn btn-sm btn-info add-text">
239+
<i class="fas fa-sort-down" style="margin-top: -10px;"></i> Text
240+
</button>
241+
<button type="button" id="add_text_up_btn-${new_id}" class="btn btn-sm btn-info add-text">
242+
<i class="fas fa-sort-up"></i> Text
243+
</button>
244+
</div>
245+
246+
<button type="button" id="del-btn_${new_id}" class="btn btn-sm btn-danger del"><i
247+
class="fas fa-trash-alt"></i>
248+
</button>
249+
</div>
250+
251+
</div>
252+
<div class="col-md-2"></div>
253+
<div class="col-md-1"></div>
254+
<div id="out_div-${new_id}" class="col-md-9 out-divs">
255+
256+
</div>
257+
<div class="col-md-2"></div>
258+
</div>
259+
`
260+
return html;
261+
}
262+
263+
function load_notebook(json){
264+
265+
266+
for(let key in json){
267+
268+
let id = key.split("-")[1]
269+
270+
if(Object.prototype.hasOwnProperty.call(json[key], "in")){
271+
let html = html_load(id)
272+
273+
$(".content").append(html)
274+
275+
const editor = CodeMirror(document.getElementById(`div-${id}`), {
276+
lineNumbers: true,
277+
tabSize: 1,
278+
mode: 'javascript',
279+
theme: 'monokai',
280+
value: ''
281+
});
282+
let input = json[key]["in"]
283+
editor.getDoc().setValue(input);
284+
285+
vars_in_scope[`div-${id}`] = editor
286+
287+
let out = json[key]["out"]
288+
289+
$(`#out_div-${id}`).html(out);
290+
291+
$(`#div-${id}`)
292+
.mouseover(function () {
293+
$(`#btn-actions-${id}`).show()
294+
})
295+
.mouseout(function () {
296+
$(`#btn-actions-${id}`).hide()
297+
});
298+
299+
}else{
300+
301+
let md = md_load(id)
302+
303+
$(".content").append(md);
304+
305+
let out = json[key]["out"]
306+
$(`#cell-${id}`).html(out)
307+
308+
let md_out = json[key]["md"]
309+
// console.log(md_out)
310+
md_texts[`text-div_${Number(id)}`] = md_out;
311+
312+
vars_in_scope[`div_text-${id}`] = ""
313+
314+
$(`textarea#text-box_${id}`).addClass("text-box")
315+
$(`textarea#text-box_${id}`).val(md_out)
316+
// update_text_box_size()
317+
318+
$(`#text-div_${id}`)
319+
.mouseover(function () {
320+
document.getElementById(`btn-actions-${id}`).style.display = "block"
321+
})
322+
.mouseout(function () {
323+
document.getElementById(`btn-actions-${id}`).style.display = "none"
324+
});
325+
}
326+
327+
__code_cell_count = parseInt(id)
328+
329+
}
330+
}
331+
207332

notebookjs/src/views/index.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<body>
22

3-
<div class="">
3+
<div class="content">
44
<div class="row" style="margin-top: 50px;" id="cell-1">
55
<div class="col-md-1">
66
<div id="cell_spinner-1" class="spinner-border text-info code_symbol" style="display: none;" role="status">

notebookjs/src/views/layout.hbs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@
7474
aria-haspopup="true" aria-expanded="false">
7575
Options
7676
</a>
77+
Import: <input type="file"
78+
id="import-notebook-file" name="import-notebook-file" style="display: inline;">
79+
<button type="button" class="btn btn-lg btn-primary" id="uploadnb">Import</button>
7780
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
7881
<a class="dropdown-item" id="download" href="#">Download Notebook</a>
7982
<a class="dropdown-item" id="upload" href="#">Upload Notebook</a>

notebookjs/src/views/notebook.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,10 @@
137137
md_texts[`text-div_${Number(id)}`] = md_out;
138138

139139
vars_in_scope[`div_text-${id}`] = ""
140-
update_text_box_size()
140+
141+
$(`textarea#text-box_${id}`).addClass("text-box")
142+
$(`textarea#text-box_${id}`).val(md_out)
143+
// update_text_box_size()
141144

142145
$(`#text-div_${id}`)
143146
.mouseover(function () {

0 commit comments

Comments
 (0)