Skip to content

Commit ce6ffab

Browse files
committed
add ctrl-enter key to run uploaded notebook
2 parents 8fc66a5 + a424813 commit ce6ffab

5 files changed

Lines changed: 149 additions & 55 deletions

File tree

notebookjs/src/public/javascripts/index.js

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ const editor = CodeMirror(document.getElementById('div-1'), {
88
autoCloseBrackets: true
99
});
1010

11+
//run first cell on CTRL ENTER Pressed
12+
$(`#div-1`).keydown(function (e) {
13+
if ((e.ctrlKey || e.metaKey) && (e.keyCode == 13 || e.keyCode == 10)) {
14+
// document.getElementById("cell_spinner-1").style.display = "block"
15+
// document.getElementById("cell_num-1").style.display = "none"
16+
exec_cell(`run_div-1`);
17+
18+
}
19+
});
20+
21+
1122

1223
var md = new Remarkable()
1324
//Global Params
@@ -61,6 +72,8 @@ function exec_cell(c_id) {
6172
}
6273

6374
$(`#out_${id}`).html(output);
75+
// document.getElementById("cell_spinner-1").style.display = "none"
76+
// document.getElementById("cell_num-1").style.display = "block"
6477

6578
count = parseInt(count) + 1
6679
let div_count = `div-${count}`
@@ -69,6 +82,8 @@ function exec_cell(c_id) {
6982
} catch (error) {
7083
$(`#out_${id}`).html(error)
7184
console.log(error)
85+
// document.getElementById("cell_spinner-1").style.display = "none"
86+
// document.getElementById("cell_num-1").style.display = "block"
7287

7388
}
7489
}
@@ -156,6 +171,17 @@ function add_new_code_cell(c_id, where) {
156171
$(`#btn-actions-${new_id}`).hide()
157172
});
158173

174+
175+
//run cell on CTRL-ENTER Pressed
176+
$(`#div-${new_id}`).keydown(function (e) {
177+
if ((e.ctrlKey || e.metaKey) && (e.keyCode == 13 || e.keyCode == 10)) {
178+
// document.getElementById("cell_spinner-1").style.display = "block"
179+
// document.getElementById("cell_num-1").style.display = "none"
180+
exec_cell(`run_div-${new_id}`);
181+
182+
}
183+
});
184+
159185
}
160186

161187
function add_new_text_cell(c_id, where) {
@@ -260,6 +286,8 @@ $(document).on("click", "button.run", function () {
260286
let val = document.getElementById(`text-box_${id}`).value
261287
show_md(id, val)
262288
} else {
289+
// document.getElementById("cell_spinner-1").style.display = "block"
290+
// document.getElementById("cell_num-1").style.display = "none"
263291
exec_cell(this.id);
264292
}
265293
})
@@ -292,11 +320,11 @@ $(document).on("click", "button.add-text", function () {
292320
add_new_text_cell(this.id, where)
293321
})
294322

295-
// $(document).on("dblclick", "textarea.text-box", function () {
296-
// let id = this.id.split("_")[1]
297-
// show_md(id, this.value)
323+
$(document).on("dblclick", "textarea.text-box", function () {
324+
let id = this.id.split("_")[1]
325+
show_md(id, this.value)
298326

299-
// })
327+
})
300328

301329
function show_md(id, value) {
302330
div_id = `text-div_${id}`
@@ -362,3 +390,17 @@ $("#uploadnb").click(function() {
362390
reader.readAsText(content);
363391
}
364392
})
393+
394+
395+
396+
async function load_data(path) {
397+
document.getElementById("cell-running").style.display = "block"
398+
let df = await dfd.read_csv(path)
399+
document.getElementById("cell-running").style.display = "none"
400+
return df
401+
402+
}
403+
404+
405+
406+

notebookjs/src/public/javascripts/utils.js

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -144,27 +144,27 @@ function table(df) {
144144

145145
}
146146

147-
function notebook_json(scope,md_scope){
147+
function notebook_json(scope, md_scope) {
148148

149149
var store = {}
150150

151151
for (let key in scope) {
152152

153153
let key_split = key.split("-")
154154
let id = key_split[1]
155-
155+
156156
let type = key_split[0].split("_")[1]
157157

158-
if(type == "text"){
158+
if (type == "text") {
159159

160160
let md_out = md_scope[`text-div_${Number(id)}`]
161161
let text_output = $(`#cell-${id}`).html()
162162
store[`cell-${id}`] = {
163163
"out": text_output,
164-
"md" : md_out
164+
"md": md_out
165165
}
166166
}
167-
else{
167+
else {
168168
let cell_content = scope[key].getValue()
169169

170170
let cell_output = $(`#out_${key}`).html()
@@ -174,7 +174,7 @@ function notebook_json(scope,md_scope){
174174
"out": cell_output
175175
}
176176
}
177-
177+
178178
}
179179

180180
store = JSON.stringify(store);
@@ -204,14 +204,14 @@ function LoadPackage(array, callback) {
204204
})();
205205
}
206206

207-
function md_load(id){
207+
function md_load(id) {
208208

209209
let md = `<div class="row" style="margin-top: 10px;" id="cell-${id}"></div>`
210210

211211
return md;
212212
}
213213

214-
function html_load(new_id){
214+
function html_load(new_id) {
215215

216216
let html = `
217217
<div class="row" style="margin-top: 10px;" id="cell-${new_id}">
@@ -257,17 +257,17 @@ function html_load(new_id){
257257
<div class="col-md-2"></div>
258258
</div>
259259
`
260-
return html;
260+
return html;
261261
}
262262

263-
function load_notebook(json){
263+
function load_notebook(json) {
264+
265+
266+
for (let key in json) {
264267

265-
266-
for(let key in json){
267-
268268
let id = key.split("-")[1]
269269

270-
if(Object.prototype.hasOwnProperty.call(json[key], "in")){
270+
if (Object.prototype.hasOwnProperty.call(json[key], "in")) {
271271
let html = html_load(id)
272272

273273
$(".content").append(html)
@@ -289,44 +289,52 @@ function load_notebook(json){
289289
$(`#out_div-${id}`).html(out);
290290

291291
$(`#div-${id}`)
292-
.mouseover(function () {
293-
$(`#btn-actions-${id}`).show()
294-
})
295-
.mouseout(function () {
296-
$(`#btn-actions-${id}`).hide()
297-
});
292+
.mouseover(function () {
293+
$(`#btn-actions-${id}`).show()
294+
})
295+
.mouseout(function () {
296+
$(`#btn-actions-${id}`).hide()
297+
});
298+
299+
$(`#div-${id}`).keydown(function (e) {
300+
if ((e.ctrlKey || e.metaKey) && (e.keyCode == 13 || e.keyCode == 10)) {
301+
// document.getElementById("cell_spinner-1").style.display = "block"
302+
// document.getElementById("cell_num-1").style.display = "none"
303+
exec_cell(`run_div-${id}`);
304+
305+
}
306+
});
298307

299-
}else{
308+
} else {
300309

301-
let md = md_load(id)
310+
let md = md_load(id)
302311

303-
$(".content").append(md);
312+
$(".content").append(md);
304313

305-
let out = json[key]["out"]
306-
$(`#cell-${id}`).html(out)
314+
let out = json[key]["out"]
315+
$(`#cell-${id}`).html(out)
307316

308-
let md_out = json[key]["md"]
309-
// console.log(md_out)
310-
md_texts[`text-div_${Number(id)}`] = md_out;
317+
let md_out = json[key]["md"]
318+
// console.log(md_out)
319+
md_texts[`text-div_${Number(id)}`] = md_out;
311320

312-
vars_in_scope[`div_text-${id}`] = ""
321+
vars_in_scope[`div_text-${id}`] = ""
313322

314-
$(`textarea#text-box_${id}`).addClass("text-box")
315-
$(`textarea#text-box_${id}`).val(md_out)
316-
// update_text_box_size()
323+
$(`textarea#text-box_${id}`).addClass("text-box")
324+
$(`textarea#text-box_${id}`).val(md_out)
325+
// update_text_box_size()
317326

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-
});
327+
$(`#text-div_${id}`)
328+
.mouseover(function () {
329+
document.getElementById(`btn-actions-${id}`).style.display = "block"
330+
})
331+
.mouseout(function () {
332+
document.getElementById(`btn-actions-${id}`).style.display = "none"
333+
});
325334
}
326335

327336
__code_cell_count = parseInt(id)
328-
337+
329338
}
330339
}
331340

332-

notebookjs/src/public/stylesheets/style.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,16 @@ a {
4545

4646
}
4747

48+
49+
.inputfileholder {
50+
overflow: hidden;
51+
max-width: 200px;
52+
height: 21px;
53+
position: relative
54+
}
55+
56+
.inputfileholder .inputfile {
57+
position: absolute;
58+
left: -90px;
59+
outline: none;
60+
}

notebookjs/src/views/index.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
<div class="content">
44
<div class="row" style="margin-top: 50px;" id="cell-1">
55
<div class="col-md-1">
6-
<div id="cell_spinner-1" class="spinner-border text-info code_symbol" style="display: none;" role="status">
6+
{{!-- <div id="cell_spinner-1" class="spinner-border text-info code_symbol" style="display: none;" role="status">
77
<span class="sr-only">Loading...</span>
8-
</div>
8+
</div> --}}
99
<p id="cell_num-1" class="code_symbol">[1]</p>
1010
</div>
1111
<div id="div-1" class="col-md-9">

notebookjs/src/views/layout.hbs

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,10 @@
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>
8077
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
8178
<a class="dropdown-item" id="download" href="#">Download Notebook</a>
82-
<a class="dropdown-item" id="upload" href="#">Upload Notebook</a>
83-
<a class="dropdown-item" id="add-data" href="#">Add Data</a>
79+
<a class="dropdown-item" data-toggle="modal" data-target="#uploadNoteModal" href="#">Upload Notebook</a>
80+
{{!-- <a class="dropdown-item" id="add-data" href="#">Add Data</a> --}}
8481

8582
<div class="dropdown-divider"></div>
8683
<a class="nav-link" href="#">Getting Started</a>
@@ -89,9 +86,12 @@
8986
<a class="dropdown-item" id="add-data" href="#">About Dnotebooks</a>
9087
</div>
9188
</li>
92-
{{!-- <li class="nav-item">
93-
<a class="nav-link" href="#">Getting Started</a>
94-
</li> --}}
89+
90+
<li class="nav-item">
91+
<div id="cell-running" class="spinner-border text-info code_symbol" style="display: none;" role="status">
92+
<span class="sr-only">Loading...</span>
93+
</div>
94+
</li>
9595
{{!-- <li class="nav-item">
9696
<a class="nav-link" href="https://github.com/opensource9ja/notebookjs">Github</a>
9797
</li> --}}
@@ -118,6 +118,37 @@
118118
</div>
119119
</nav>
120120

121+
122+
123+
124+
{{!-- Notebook upload --}}
125+
<div class="modal" id="uploadNoteModal" tabindex="-1">
126+
<div class="modal-dialog">
127+
<div class="modal-content">
128+
<div class="modal-header">
129+
<h5 class="modal-title">Upload a Notebook</h5>
130+
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
131+
<span aria-hidden="true">&times;</span>
132+
</button>
133+
</div>
134+
<div class="modal-body">
135+
136+
{{!-- Import: <input type="file" id="import-notebook-file" name="import-notebook-file" style="display: inline;"> --}}
137+
<form>
138+
<div class="form-group">
139+
<label for="exampleFormControlFile1">Select a Notebook File</label>
140+
<input type="file" class="form-control-file" id="import-notebook-file" name="import-notebook-file">
141+
</div>
142+
</form>
143+
</div>
144+
<div class="modal-footer">
145+
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
146+
<button type="button" class="btn btn-primary" id="uploadnb">Import</button>
147+
</div>
148+
</div>
149+
</div>
150+
</div>
151+
121152
{{{body}}}
122153

123154

0 commit comments

Comments
 (0)