Show More
@@ -1,616 +1,618 | |||||
1 |
|
1 | |||
2 | //============================================================================ |
|
2 | //============================================================================ | |
3 | // Notebook |
|
3 | // Notebook | |
4 | //============================================================================ |
|
4 | //============================================================================ | |
5 |
|
5 | |||
6 | var IPython = (function (IPython) { |
|
6 | var IPython = (function (IPython) { | |
7 |
|
7 | |||
8 | var utils = IPython.utils; |
|
8 | var utils = IPython.utils; | |
9 |
|
9 | |||
10 | var Notebook = function (selector) { |
|
10 | var Notebook = function (selector) { | |
11 | this.element = $(selector); |
|
11 | this.element = $(selector); | |
12 | this.element.scroll(); |
|
12 | this.element.scroll(); | |
13 | this.element.data("notebook", this); |
|
13 | this.element.data("notebook", this); | |
14 | this.next_prompt_number = 1; |
|
14 | this.next_prompt_number = 1; | |
15 | this.kernel = null; |
|
15 | this.kernel = null; | |
16 | this.msg_cell_map = {}; |
|
16 | this.msg_cell_map = {}; | |
17 | this.filename = null; |
|
17 | this.filename = null; | |
18 | this.notebook_load_re = /%notebook load/ |
|
18 | this.notebook_load_re = /%notebook load/ | |
19 | this.notebook_save_re = /%notebook save/ |
|
19 | this.notebook_save_re = /%notebook save/ | |
20 | this.notebook_filename_re = /(\w)+.ipynb/ |
|
20 | this.notebook_filename_re = /(\w)+.ipynb/ | |
21 | this.style(); |
|
21 | this.style(); | |
22 | this.create_elements(); |
|
22 | this.create_elements(); | |
23 | this.bind_events(); |
|
23 | this.bind_events(); | |
24 | this.start_kernel(); |
|
24 | this.start_kernel(); | |
25 | }; |
|
25 | }; | |
26 |
|
26 | |||
27 |
|
27 | |||
28 | Notebook.prototype.style = function () { |
|
28 | Notebook.prototype.style = function () { | |
29 | $('div#notebook').addClass('border-box-sizing'); |
|
29 | $('div#notebook').addClass('border-box-sizing'); | |
30 | }; |
|
30 | }; | |
31 |
|
31 | |||
32 |
|
32 | |||
33 | Notebook.prototype.create_elements = function () { |
|
33 | Notebook.prototype.create_elements = function () { | |
34 | // We add this end_space div to the end of the notebook div to: |
|
34 | // We add this end_space div to the end of the notebook div to: | |
35 | // i) provide a margin between the last cell and the end of the notebook |
|
35 | // i) provide a margin between the last cell and the end of the notebook | |
36 | // ii) to prevent the div from scrolling up when the last cell is being |
|
36 | // ii) to prevent the div from scrolling up when the last cell is being | |
37 | // edited, but is too low on the page, which browsers will do automatically. |
|
37 | // edited, but is too low on the page, which browsers will do automatically. | |
38 | this.element.append($('<div class="end_space"></div>').height(50)); |
|
38 | this.element.append($('<div class="end_space"></div>').height(50)); | |
39 | $('div#notebook').addClass('border-box-sizing'); |
|
39 | $('div#notebook').addClass('border-box-sizing'); | |
40 | }; |
|
40 | }; | |
41 |
|
41 | |||
42 |
|
42 | |||
43 | Notebook.prototype.bind_events = function () { |
|
43 | Notebook.prototype.bind_events = function () { | |
44 | var that = this; |
|
44 | var that = this; | |
45 | $(document).keydown(function (event) { |
|
45 | $(document).keydown(function (event) { | |
46 | // console.log(event); |
|
46 | // console.log(event); | |
47 | if (event.which === 38) { |
|
47 | if (event.which === 38) { | |
48 | var cell = that.selected_cell(); |
|
48 | var cell = that.selected_cell(); | |
49 | if (cell.at_top()) { |
|
49 | if (cell.at_top()) { | |
50 | event.preventDefault(); |
|
50 | event.preventDefault(); | |
51 | that.select_prev(); |
|
51 | that.select_prev(); | |
52 | }; |
|
52 | }; | |
53 | } else if (event.which === 40) { |
|
53 | } else if (event.which === 40) { | |
54 | var cell = that.selected_cell(); |
|
54 | var cell = that.selected_cell(); | |
55 | if (cell.at_bottom()) { |
|
55 | if (cell.at_bottom()) { | |
56 | event.preventDefault(); |
|
56 | event.preventDefault(); | |
57 | that.select_next(); |
|
57 | that.select_next(); | |
58 | }; |
|
58 | }; | |
59 | } else if (event.which === 13 && event.shiftKey) { |
|
59 | } else if (event.which === 13 && event.shiftKey) { | |
60 | that.execute_selected_cell(true); |
|
60 | that.execute_selected_cell(true); | |
61 | return false; |
|
61 | return false; | |
62 | } else if (event.which === 13 && event.ctrlKey) { |
|
62 | } else if (event.which === 13 && event.ctrlKey) { | |
63 | that.execute_selected_cell(false); |
|
63 | that.execute_selected_cell(false); | |
64 | that.selected_cell().clear_input(); |
|
64 | that.selected_cell().clear_input(); | |
65 | return false; |
|
65 | return false; | |
66 | }; |
|
66 | }; | |
67 | }); |
|
67 | }); | |
68 |
|
68 | |||
69 | this.element.bind('collapse_pager', function () { |
|
69 | this.element.bind('collapse_pager', function () { | |
70 | var app_height = $('div#notebook_app').height(); // content height |
|
70 | var app_height = $('div#notebook_app').height(); // content height | |
71 | var splitter_height = $('div#pager_splitter').outerHeight(true); |
|
71 | var splitter_height = $('div#pager_splitter').outerHeight(true); | |
72 | var new_height = app_height - splitter_height; |
|
72 | var new_height = app_height - splitter_height; | |
73 | that.element.animate({height : new_height + 'px'}, 'fast'); |
|
73 | that.element.animate({height : new_height + 'px'}, 'fast'); | |
74 | }); |
|
74 | }); | |
75 |
|
75 | |||
76 | this.element.bind('expand_pager', function () { |
|
76 | this.element.bind('expand_pager', function () { | |
77 | var app_height = $('div#notebook_app').height(); // content height |
|
77 | var app_height = $('div#notebook_app').height(); // content height | |
78 | var splitter_height = $('div#pager_splitter').outerHeight(true); |
|
78 | var splitter_height = $('div#pager_splitter').outerHeight(true); | |
79 | var pager_height = $('div#pager').outerHeight(true); |
|
79 | var pager_height = $('div#pager').outerHeight(true); | |
80 | var new_height = app_height - pager_height - splitter_height; |
|
80 | var new_height = app_height - pager_height - splitter_height; | |
81 | that.element.animate({height : new_height + 'px'}, 'fast'); |
|
81 | that.element.animate({height : new_height + 'px'}, 'fast'); | |
82 | }); |
|
82 | }); | |
83 |
|
83 | |||
84 | this.element.bind('collapse_left_panel', function () { |
|
84 | this.element.bind('collapse_left_panel', function () { | |
85 | var splitter_width = $('div#left_panel_splitter').outerWidth(true); |
|
85 | var splitter_width = $('div#left_panel_splitter').outerWidth(true); | |
86 | var new_margin = splitter_width; |
|
86 | var new_margin = splitter_width; | |
87 | $('div#notebook_panel').animate({marginLeft : new_margin + 'px'}, 'fast'); |
|
87 | $('div#notebook_panel').animate({marginLeft : new_margin + 'px'}, 'fast'); | |
88 | }); |
|
88 | }); | |
89 |
|
89 | |||
90 | this.element.bind('expand_left_panel', function () { |
|
90 | this.element.bind('expand_left_panel', function () { | |
91 | var splitter_width = $('div#left_panel_splitter').outerWidth(true); |
|
91 | var splitter_width = $('div#left_panel_splitter').outerWidth(true); | |
92 | var left_panel_width = IPython.left_panel.width; |
|
92 | var left_panel_width = IPython.left_panel.width; | |
93 | var new_margin = splitter_width + left_panel_width; |
|
93 | var new_margin = splitter_width + left_panel_width; | |
94 | $('div#notebook_panel').animate({marginLeft : new_margin + 'px'}, 'fast'); |
|
94 | $('div#notebook_panel').animate({marginLeft : new_margin + 'px'}, 'fast'); | |
95 | }); |
|
95 | }); | |
96 | }; |
|
96 | }; | |
97 |
|
97 | |||
98 |
|
98 | |||
99 | Notebook.prototype.scroll_to_bottom = function () { |
|
99 | Notebook.prototype.scroll_to_bottom = function () { | |
100 | this.element.animate({scrollTop:this.element.get(0).scrollHeight}, 0); |
|
100 | this.element.animate({scrollTop:this.element.get(0).scrollHeight}, 0); | |
101 | }; |
|
101 | }; | |
102 |
|
102 | |||
103 | // Cell indexing, retrieval, etc. |
|
103 | // Cell indexing, retrieval, etc. | |
104 |
|
104 | |||
105 |
|
105 | |||
106 | Notebook.prototype.cell_elements = function () { |
|
106 | Notebook.prototype.cell_elements = function () { | |
107 | return this.element.children("div.cell"); |
|
107 | return this.element.children("div.cell"); | |
108 | } |
|
108 | } | |
109 |
|
109 | |||
110 |
|
110 | |||
111 | Notebook.prototype.ncells = function (cell) { |
|
111 | Notebook.prototype.ncells = function (cell) { | |
112 | return this.cell_elements().length; |
|
112 | return this.cell_elements().length; | |
113 | } |
|
113 | } | |
114 |
|
114 | |||
115 |
|
115 | |||
116 | // TODO: we are often calling cells as cells()[i], which we should optimize |
|
116 | // TODO: we are often calling cells as cells()[i], which we should optimize | |
117 | // to cells(i) or a new method. |
|
117 | // to cells(i) or a new method. | |
118 | Notebook.prototype.cells = function () { |
|
118 | Notebook.prototype.cells = function () { | |
119 | return this.cell_elements().toArray().map(function (e) { |
|
119 | return this.cell_elements().toArray().map(function (e) { | |
120 | return $(e).data("cell"); |
|
120 | return $(e).data("cell"); | |
121 | }); |
|
121 | }); | |
122 | } |
|
122 | } | |
123 |
|
123 | |||
124 |
|
124 | |||
125 | Notebook.prototype.find_cell_index = function (cell) { |
|
125 | Notebook.prototype.find_cell_index = function (cell) { | |
126 | var result = null; |
|
126 | var result = null; | |
127 | this.cell_elements().filter(function (index) { |
|
127 | this.cell_elements().filter(function (index) { | |
128 | if ($(this).data("cell") === cell) { |
|
128 | if ($(this).data("cell") === cell) { | |
129 | result = index; |
|
129 | result = index; | |
130 | }; |
|
130 | }; | |
131 | }); |
|
131 | }); | |
132 | return result; |
|
132 | return result; | |
133 | }; |
|
133 | }; | |
134 |
|
134 | |||
135 |
|
135 | |||
136 | Notebook.prototype.index_or_selected = function (index) { |
|
136 | Notebook.prototype.index_or_selected = function (index) { | |
137 | return index || this.selected_index() || 0; |
|
137 | return index || this.selected_index() || 0; | |
138 | } |
|
138 | } | |
139 |
|
139 | |||
140 |
|
140 | |||
141 | Notebook.prototype.select = function (index) { |
|
141 | Notebook.prototype.select = function (index) { | |
142 | if (index !== undefined && index >= 0 && index < this.ncells()) { |
|
142 | if (index !== undefined && index >= 0 && index < this.ncells()) { | |
143 | if (this.selected_index() !== null) { |
|
143 | if (this.selected_index() !== null) { | |
144 | this.selected_cell().unselect(); |
|
144 | this.selected_cell().unselect(); | |
145 | }; |
|
145 | }; | |
146 | this.cells()[index].select(); |
|
146 | this.cells()[index].select(); | |
147 | if (index === (this.ncells()-1)) { |
|
147 | if (index === (this.ncells()-1)) { | |
148 | this.scroll_to_bottom(); |
|
148 | this.scroll_to_bottom(); | |
149 | }; |
|
149 | }; | |
150 | }; |
|
150 | }; | |
151 | return this; |
|
151 | return this; | |
152 | }; |
|
152 | }; | |
153 |
|
153 | |||
154 |
|
154 | |||
155 | Notebook.prototype.select_next = function () { |
|
155 | Notebook.prototype.select_next = function () { | |
156 | var index = this.selected_index(); |
|
156 | var index = this.selected_index(); | |
157 | if (index !== null && index >= 0 && (index+1) < this.ncells()) { |
|
157 | if (index !== null && index >= 0 && (index+1) < this.ncells()) { | |
158 | this.select(index+1); |
|
158 | this.select(index+1); | |
159 | }; |
|
159 | }; | |
160 | return this; |
|
160 | return this; | |
161 | }; |
|
161 | }; | |
162 |
|
162 | |||
163 |
|
163 | |||
164 | Notebook.prototype.select_prev = function () { |
|
164 | Notebook.prototype.select_prev = function () { | |
165 | var index = this.selected_index(); |
|
165 | var index = this.selected_index(); | |
166 | if (index !== null && index >= 0 && (index-1) < this.ncells()) { |
|
166 | if (index !== null && index >= 0 && (index-1) < this.ncells()) { | |
167 | this.select(index-1); |
|
167 | this.select(index-1); | |
168 | }; |
|
168 | }; | |
169 | return this; |
|
169 | return this; | |
170 | }; |
|
170 | }; | |
171 |
|
171 | |||
172 |
|
172 | |||
173 | Notebook.prototype.selected_index = function () { |
|
173 | Notebook.prototype.selected_index = function () { | |
174 | var result = null; |
|
174 | var result = null; | |
175 | this.cell_elements().filter(function (index) { |
|
175 | this.cell_elements().filter(function (index) { | |
176 | if ($(this).data("cell").selected === true) { |
|
176 | if ($(this).data("cell").selected === true) { | |
177 | result = index; |
|
177 | result = index; | |
178 | }; |
|
178 | }; | |
179 | }); |
|
179 | }); | |
180 | return result; |
|
180 | return result; | |
181 | }; |
|
181 | }; | |
182 |
|
182 | |||
183 |
|
183 | |||
184 | Notebook.prototype.cell_for_msg = function (msg_id) { |
|
184 | Notebook.prototype.cell_for_msg = function (msg_id) { | |
185 | var cell_id = this.msg_cell_map[msg_id]; |
|
185 | var cell_id = this.msg_cell_map[msg_id]; | |
186 | var result = null; |
|
186 | var result = null; | |
187 | this.cell_elements().filter(function (index) { |
|
187 | this.cell_elements().filter(function (index) { | |
188 | cell = $(this).data("cell"); |
|
188 | cell = $(this).data("cell"); | |
189 | if (cell.cell_id === cell_id) { |
|
189 | if (cell.cell_id === cell_id) { | |
190 | result = cell; |
|
190 | result = cell; | |
191 | }; |
|
191 | }; | |
192 | }); |
|
192 | }); | |
193 | return result; |
|
193 | return result; | |
194 | }; |
|
194 | }; | |
195 |
|
195 | |||
196 |
|
196 | |||
197 | Notebook.prototype.selected_cell = function () { |
|
197 | Notebook.prototype.selected_cell = function () { | |
198 | return this.cell_elements().eq(this.selected_index()).data("cell"); |
|
198 | return this.cell_elements().eq(this.selected_index()).data("cell"); | |
199 | } |
|
199 | } | |
200 |
|
200 | |||
201 |
|
201 | |||
202 | // Cell insertion, deletion and moving. |
|
202 | // Cell insertion, deletion and moving. | |
203 |
|
203 | |||
204 |
|
204 | |||
205 | Notebook.prototype.delete_cell = function (index) { |
|
205 | Notebook.prototype.delete_cell = function (index) { | |
206 | var i = index || this.selected_index(); |
|
206 | var i = index || this.selected_index(); | |
207 | if (i !== null && i >= 0 && i < this.ncells()) { |
|
207 | if (i !== null && i >= 0 && i < this.ncells()) { | |
208 | this.cell_elements().eq(i).remove(); |
|
208 | this.cell_elements().eq(i).remove(); | |
209 | if (i === (this.ncells())) { |
|
209 | if (i === (this.ncells())) { | |
210 | this.select(i-1); |
|
210 | this.select(i-1); | |
211 | } else { |
|
211 | } else { | |
212 | this.select(i); |
|
212 | this.select(i); | |
213 | }; |
|
213 | }; | |
214 | }; |
|
214 | }; | |
215 | return this; |
|
215 | return this; | |
216 | }; |
|
216 | }; | |
217 |
|
217 | |||
218 |
|
218 | |||
219 | Notebook.prototype.append_cell = function (cell) { |
|
219 | Notebook.prototype.append_cell = function (cell) { | |
220 | this.element.find('div.end_space').before(cell.element); |
|
220 | this.element.find('div.end_space').before(cell.element); | |
221 | return this; |
|
221 | return this; | |
222 | }; |
|
222 | }; | |
223 |
|
223 | |||
224 |
|
224 | |||
225 | Notebook.prototype.insert_cell_after = function (cell, index) { |
|
225 | Notebook.prototype.insert_cell_after = function (cell, index) { | |
226 | var ncells = this.ncells(); |
|
226 | var ncells = this.ncells(); | |
227 | if (ncells === 0) { |
|
227 | if (ncells === 0) { | |
228 | this.append_cell(cell); |
|
228 | this.append_cell(cell); | |
229 | return this; |
|
229 | return this; | |
230 | }; |
|
230 | }; | |
231 | if (index >= 0 && index < ncells) { |
|
231 | if (index >= 0 && index < ncells) { | |
232 | this.cell_elements().eq(index).after(cell.element); |
|
232 | this.cell_elements().eq(index).after(cell.element); | |
233 | }; |
|
233 | }; | |
234 | return this |
|
234 | return this | |
235 | }; |
|
235 | }; | |
236 |
|
236 | |||
237 |
|
237 | |||
238 | Notebook.prototype.insert_cell_before = function (cell, index) { |
|
238 | Notebook.prototype.insert_cell_before = function (cell, index) { | |
239 | var ncells = this.ncells(); |
|
239 | var ncells = this.ncells(); | |
240 | if (ncells === 0) { |
|
240 | if (ncells === 0) { | |
241 | this.append_cell(cell); |
|
241 | this.append_cell(cell); | |
242 | return this; |
|
242 | return this; | |
243 | }; |
|
243 | }; | |
244 | if (index >= 0 && index < ncells) { |
|
244 | if (index >= 0 && index < ncells) { | |
245 | this.cell_elements().eq(index).before(cell.element); |
|
245 | this.cell_elements().eq(index).before(cell.element); | |
246 | }; |
|
246 | }; | |
247 | return this; |
|
247 | return this; | |
248 | }; |
|
248 | }; | |
249 |
|
249 | |||
250 |
|
250 | |||
251 | Notebook.prototype.move_cell_up = function (index) { |
|
251 | Notebook.prototype.move_cell_up = function (index) { | |
252 | var i = index || this.selected_index(); |
|
252 | var i = index || this.selected_index(); | |
253 | if (i !== null && i < this.ncells() && i > 0) { |
|
253 | if (i !== null && i < this.ncells() && i > 0) { | |
254 | var pivot = this.cell_elements().eq(i-1); |
|
254 | var pivot = this.cell_elements().eq(i-1); | |
255 | var tomove = this.cell_elements().eq(i); |
|
255 | var tomove = this.cell_elements().eq(i); | |
256 | if (pivot !== null && tomove !== null) { |
|
256 | if (pivot !== null && tomove !== null) { | |
257 | tomove.detach(); |
|
257 | tomove.detach(); | |
258 | pivot.before(tomove); |
|
258 | pivot.before(tomove); | |
259 | this.select(i-1); |
|
259 | this.select(i-1); | |
260 | }; |
|
260 | }; | |
261 | }; |
|
261 | }; | |
262 | return this; |
|
262 | return this; | |
263 | } |
|
263 | } | |
264 |
|
264 | |||
265 |
|
265 | |||
266 | Notebook.prototype.move_cell_down = function (index) { |
|
266 | Notebook.prototype.move_cell_down = function (index) { | |
267 | var i = index || this.selected_index(); |
|
267 | var i = index || this.selected_index(); | |
268 | if (i !== null && i < (this.ncells()-1) && i >= 0) { |
|
268 | if (i !== null && i < (this.ncells()-1) && i >= 0) { | |
269 | var pivot = this.cell_elements().eq(i+1) |
|
269 | var pivot = this.cell_elements().eq(i+1) | |
270 | var tomove = this.cell_elements().eq(i) |
|
270 | var tomove = this.cell_elements().eq(i) | |
271 | if (pivot !== null && tomove !== null) { |
|
271 | if (pivot !== null && tomove !== null) { | |
272 | tomove.detach(); |
|
272 | tomove.detach(); | |
273 | pivot.after(tomove); |
|
273 | pivot.after(tomove); | |
274 | this.select(i+1); |
|
274 | this.select(i+1); | |
275 | }; |
|
275 | }; | |
276 | }; |
|
276 | }; | |
277 | return this; |
|
277 | return this; | |
278 | } |
|
278 | } | |
279 |
|
279 | |||
280 |
|
280 | |||
281 | Notebook.prototype.sort_cells = function () { |
|
281 | Notebook.prototype.sort_cells = function () { | |
282 | var ncells = this.ncells(); |
|
282 | var ncells = this.ncells(); | |
283 | var sindex = this.selected_index(); |
|
283 | var sindex = this.selected_index(); | |
284 | var swapped; |
|
284 | var swapped; | |
285 | do { |
|
285 | do { | |
286 | swapped = false |
|
286 | swapped = false | |
287 | for (var i=1; i<ncells; i++) { |
|
287 | for (var i=1; i<ncells; i++) { | |
288 | current = this.cell_elements().eq(i).data("cell"); |
|
288 | current = this.cell_elements().eq(i).data("cell"); | |
289 | previous = this.cell_elements().eq(i-1).data("cell"); |
|
289 | previous = this.cell_elements().eq(i-1).data("cell"); | |
290 | if (previous.input_prompt_number > current.input_prompt_number) { |
|
290 | if (previous.input_prompt_number > current.input_prompt_number) { | |
291 | this.move_cell_up(i); |
|
291 | this.move_cell_up(i); | |
292 | swapped = true; |
|
292 | swapped = true; | |
293 | }; |
|
293 | }; | |
294 | }; |
|
294 | }; | |
295 | } while (swapped); |
|
295 | } while (swapped); | |
296 | this.select(sindex); |
|
296 | this.select(sindex); | |
297 | return this; |
|
297 | return this; | |
298 | }; |
|
298 | }; | |
299 |
|
299 | |||
300 |
|
300 | |||
301 | Notebook.prototype.insert_code_cell_before = function (index) { |
|
301 | Notebook.prototype.insert_code_cell_before = function (index) { | |
302 | // TODO: Bounds check for i |
|
302 | // TODO: Bounds check for i | |
303 | var i = this.index_or_selected(index); |
|
303 | var i = this.index_or_selected(index); | |
304 | var cell = new IPython.CodeCell(this); |
|
304 | var cell = new IPython.CodeCell(this); | |
305 | cell.set_input_prompt(this.next_prompt_number); |
|
305 | // cell.set_input_prompt(this.next_prompt_number); | |
|
306 | cell.set_input_prompt(); | |||
306 | this.next_prompt_number = this.next_prompt_number + 1; |
|
307 | this.next_prompt_number = this.next_prompt_number + 1; | |
307 | this.insert_cell_before(cell, i); |
|
308 | this.insert_cell_before(cell, i); | |
308 | this.select(this.find_cell_index(cell)); |
|
309 | this.select(this.find_cell_index(cell)); | |
309 | return this; |
|
310 | return this; | |
310 | } |
|
311 | } | |
311 |
|
312 | |||
312 |
|
313 | |||
313 | Notebook.prototype.insert_code_cell_after = function (index) { |
|
314 | Notebook.prototype.insert_code_cell_after = function (index) { | |
314 | // TODO: Bounds check for i |
|
315 | // TODO: Bounds check for i | |
315 | var i = this.index_or_selected(index); |
|
316 | var i = this.index_or_selected(index); | |
316 | var cell = new IPython.CodeCell(this); |
|
317 | var cell = new IPython.CodeCell(this); | |
317 | cell.set_input_prompt(this.next_prompt_number); |
|
318 | // cell.set_input_prompt(this.next_prompt_number); | |
|
319 | cell.set_input_prompt(); | |||
318 | this.next_prompt_number = this.next_prompt_number + 1; |
|
320 | this.next_prompt_number = this.next_prompt_number + 1; | |
319 | this.insert_cell_after(cell, i); |
|
321 | this.insert_cell_after(cell, i); | |
320 | this.select(this.find_cell_index(cell)); |
|
322 | this.select(this.find_cell_index(cell)); | |
321 | return this; |
|
323 | return this; | |
322 | } |
|
324 | } | |
323 |
|
325 | |||
324 |
|
326 | |||
325 | Notebook.prototype.insert_text_cell_before = function (index) { |
|
327 | Notebook.prototype.insert_text_cell_before = function (index) { | |
326 | // TODO: Bounds check for i |
|
328 | // TODO: Bounds check for i | |
327 | var i = this.index_or_selected(index); |
|
329 | var i = this.index_or_selected(index); | |
328 | var cell = new IPython.TextCell(this); |
|
330 | var cell = new IPython.TextCell(this); | |
329 | cell.config_mathjax(); |
|
331 | cell.config_mathjax(); | |
330 | this.insert_cell_before(cell, i); |
|
332 | this.insert_cell_before(cell, i); | |
331 | this.select(this.find_cell_index(cell)); |
|
333 | this.select(this.find_cell_index(cell)); | |
332 | return this; |
|
334 | return this; | |
333 | } |
|
335 | } | |
334 |
|
336 | |||
335 |
|
337 | |||
336 | Notebook.prototype.insert_text_cell_after = function (index) { |
|
338 | Notebook.prototype.insert_text_cell_after = function (index) { | |
337 | // TODO: Bounds check for i |
|
339 | // TODO: Bounds check for i | |
338 | var i = this.index_or_selected(index); |
|
340 | var i = this.index_or_selected(index); | |
339 | var cell = new IPython.TextCell(this); |
|
341 | var cell = new IPython.TextCell(this); | |
340 | cell.config_mathjax(); |
|
342 | cell.config_mathjax(); | |
341 | this.insert_cell_after(cell, i); |
|
343 | this.insert_cell_after(cell, i); | |
342 | this.select(this.find_cell_index(cell)); |
|
344 | this.select(this.find_cell_index(cell)); | |
343 | return this; |
|
345 | return this; | |
344 | } |
|
346 | } | |
345 |
|
347 | |||
346 |
|
348 | |||
347 | Notebook.prototype.text_to_code = function (index) { |
|
349 | Notebook.prototype.text_to_code = function (index) { | |
348 | // TODO: Bounds check for i |
|
350 | // TODO: Bounds check for i | |
349 | var i = this.index_or_selected(index); |
|
351 | var i = this.index_or_selected(index); | |
350 | var source_element = this.cell_elements().eq(i); |
|
352 | var source_element = this.cell_elements().eq(i); | |
351 | var source_cell = source_element.data("cell"); |
|
353 | var source_cell = source_element.data("cell"); | |
352 | if (source_cell instanceof IPython.TextCell) { |
|
354 | if (source_cell instanceof IPython.TextCell) { | |
353 | this.insert_code_cell_after(i); |
|
355 | this.insert_code_cell_after(i); | |
354 | var target_cell = this.cells()[i+1]; |
|
356 | var target_cell = this.cells()[i+1]; | |
355 | target_cell.set_code(source_cell.get_text()); |
|
357 | target_cell.set_code(source_cell.get_text()); | |
356 | source_element.remove(); |
|
358 | source_element.remove(); | |
357 | }; |
|
359 | }; | |
358 | }; |
|
360 | }; | |
359 |
|
361 | |||
360 |
|
362 | |||
361 | Notebook.prototype.code_to_text = function (index) { |
|
363 | Notebook.prototype.code_to_text = function (index) { | |
362 | // TODO: Bounds check for i |
|
364 | // TODO: Bounds check for i | |
363 | var i = this.index_or_selected(index); |
|
365 | var i = this.index_or_selected(index); | |
364 | var source_element = this.cell_elements().eq(i); |
|
366 | var source_element = this.cell_elements().eq(i); | |
365 | var source_cell = source_element.data("cell"); |
|
367 | var source_cell = source_element.data("cell"); | |
366 | if (source_cell instanceof IPython.CodeCell) { |
|
368 | if (source_cell instanceof IPython.CodeCell) { | |
367 | this.insert_text_cell_after(i); |
|
369 | this.insert_text_cell_after(i); | |
368 | var target_cell = this.cells()[i+1]; |
|
370 | var target_cell = this.cells()[i+1]; | |
369 | var text = source_cell.get_code(); |
|
371 | var text = source_cell.get_code(); | |
370 | if (text === "") {text = target_cell.placeholder;}; |
|
372 | if (text === "") {text = target_cell.placeholder;}; | |
371 | target_cell.set_text(text); |
|
373 | target_cell.set_text(text); | |
372 | source_element.remove(); |
|
374 | source_element.remove(); | |
373 | target_cell.edit(); |
|
375 | target_cell.edit(); | |
374 | }; |
|
376 | }; | |
375 | }; |
|
377 | }; | |
376 |
|
378 | |||
377 |
|
379 | |||
378 | // Cell collapsing |
|
380 | // Cell collapsing | |
379 |
|
381 | |||
380 | Notebook.prototype.collapse = function (index) { |
|
382 | Notebook.prototype.collapse = function (index) { | |
381 | var i = this.index_or_selected(index); |
|
383 | var i = this.index_or_selected(index); | |
382 | this.cells()[i].collapse(); |
|
384 | this.cells()[i].collapse(); | |
383 | }; |
|
385 | }; | |
384 |
|
386 | |||
385 |
|
387 | |||
386 | Notebook.prototype.expand = function (index) { |
|
388 | Notebook.prototype.expand = function (index) { | |
387 | var i = this.index_or_selected(index); |
|
389 | var i = this.index_or_selected(index); | |
388 | this.cells()[i].expand(); |
|
390 | this.cells()[i].expand(); | |
389 | }; |
|
391 | }; | |
390 |
|
392 | |||
391 |
|
393 | |||
392 | // Kernel related things |
|
394 | // Kernel related things | |
393 |
|
395 | |||
394 | Notebook.prototype.start_kernel = function () { |
|
396 | Notebook.prototype.start_kernel = function () { | |
395 | this.kernel = new IPython.Kernel(); |
|
397 | this.kernel = new IPython.Kernel(); | |
396 | this.kernel.start_kernel($.proxy(this.kernel_started, this)); |
|
398 | this.kernel.start_kernel($.proxy(this.kernel_started, this)); | |
397 | }; |
|
399 | }; | |
398 |
|
400 | |||
399 |
|
401 | |||
400 | Notebook.prototype.handle_shell_reply = function (e) { |
|
402 | Notebook.prototype.handle_shell_reply = function (e) { | |
401 | reply = $.parseJSON(e.data); |
|
403 | reply = $.parseJSON(e.data); | |
402 | var header = reply.header; |
|
404 | var header = reply.header; | |
403 | var content = reply.content; |
|
405 | var content = reply.content; | |
404 | var msg_type = header.msg_type; |
|
406 | var msg_type = header.msg_type; | |
405 | // console.log(reply); |
|
407 | // console.log(reply); | |
406 | var cell = this.cell_for_msg(reply.parent_header.msg_id); |
|
408 | var cell = this.cell_for_msg(reply.parent_header.msg_id); | |
407 | if (msg_type === "execute_reply") { |
|
409 | if (msg_type === "execute_reply") { | |
408 | cell.set_input_prompt(content.execution_count); |
|
410 | cell.set_input_prompt(content.execution_count); | |
409 | } else if (msg_type === "complete_reply") { |
|
411 | } else if (msg_type === "complete_reply") { | |
410 | cell.finish_completing(content.matched_text, content.matches); |
|
412 | cell.finish_completing(content.matched_text, content.matches); | |
411 | }; |
|
413 | }; | |
412 | var payload = content.payload || []; |
|
414 | var payload = content.payload || []; | |
413 | this.handle_payload(payload); |
|
415 | this.handle_payload(payload); | |
414 | }; |
|
416 | }; | |
415 |
|
417 | |||
416 |
|
418 | |||
417 | Notebook.prototype.handle_payload = function (payload) { |
|
419 | Notebook.prototype.handle_payload = function (payload) { | |
418 | var l = payload.length; |
|
420 | var l = payload.length; | |
419 | if (l > 0) { |
|
421 | if (l > 0) { | |
420 | IPython.pager.clear(); |
|
422 | IPython.pager.clear(); | |
421 | IPython.pager.expand(); |
|
423 | IPython.pager.expand(); | |
422 | }; |
|
424 | }; | |
423 | for (var i=0; i<l; i++) { |
|
425 | for (var i=0; i<l; i++) { | |
424 | IPython.pager.append_text(payload[i].text); |
|
426 | IPython.pager.append_text(payload[i].text); | |
425 | }; |
|
427 | }; | |
426 | }; |
|
428 | }; | |
427 |
|
429 | |||
428 |
|
430 | |||
429 | Notebook.prototype.handle_iopub_reply = function (e) { |
|
431 | Notebook.prototype.handle_iopub_reply = function (e) { | |
430 | reply = $.parseJSON(e.data); |
|
432 | reply = $.parseJSON(e.data); | |
431 | var content = reply.content; |
|
433 | var content = reply.content; | |
432 | // console.log(reply); |
|
434 | // console.log(reply); | |
433 | var msg_type = reply.header.msg_type; |
|
435 | var msg_type = reply.header.msg_type; | |
434 | var cell = this.cell_for_msg(reply.parent_header.msg_id); |
|
436 | var cell = this.cell_for_msg(reply.parent_header.msg_id); | |
435 | if (msg_type === "stream") { |
|
437 | if (msg_type === "stream") { | |
436 | cell.expand(); |
|
438 | cell.expand(); | |
437 | cell.append_stream(content.data + "\n"); |
|
439 | cell.append_stream(content.data + "\n"); | |
438 | } else if (msg_type === "display_data") { |
|
440 | } else if (msg_type === "display_data") { | |
439 | cell.expand(); |
|
441 | cell.expand(); | |
440 | cell.append_display_data(content.data); |
|
442 | cell.append_display_data(content.data); | |
441 | } else if (msg_type === "pyout") { |
|
443 | } else if (msg_type === "pyout") { | |
442 | cell.expand(); |
|
444 | cell.expand(); | |
443 | cell.append_pyout(content.data, content.execution_count) |
|
445 | cell.append_pyout(content.data, content.execution_count) | |
444 | } else if (msg_type === "pyerr") { |
|
446 | } else if (msg_type === "pyerr") { | |
445 | cell.expand(); |
|
447 | cell.expand(); | |
446 | cell.append_pyerr(content.ename, content.evalue, content.traceback); |
|
448 | cell.append_pyerr(content.ename, content.evalue, content.traceback); | |
447 | } else if (msg_type === "status") { |
|
449 | } else if (msg_type === "status") { | |
448 | if (content.execution_state === "busy") { |
|
450 | if (content.execution_state === "busy") { | |
449 | IPython.kernel_status_widget.status_busy(); |
|
451 | IPython.kernel_status_widget.status_busy(); | |
450 | } else if (content.execution_state === "idle") { |
|
452 | } else if (content.execution_state === "idle") { | |
451 | IPython.kernel_status_widget.status_idle(); |
|
453 | IPython.kernel_status_widget.status_idle(); | |
452 | }; |
|
454 | }; | |
453 | } |
|
455 | } | |
454 | }; |
|
456 | }; | |
455 |
|
457 | |||
456 |
|
458 | |||
457 | Notebook.prototype.kernel_started = function () { |
|
459 | Notebook.prototype.kernel_started = function () { | |
458 | console.log("Kernel started: ", this.kernel.kernel_id); |
|
460 | console.log("Kernel started: ", this.kernel.kernel_id); | |
459 | this.kernel.shell_channel.onmessage = $.proxy(this.handle_shell_reply,this); |
|
461 | this.kernel.shell_channel.onmessage = $.proxy(this.handle_shell_reply,this); | |
460 | this.kernel.iopub_channel.onmessage = $.proxy(this.handle_iopub_reply,this); |
|
462 | this.kernel.iopub_channel.onmessage = $.proxy(this.handle_iopub_reply,this); | |
461 | }; |
|
463 | }; | |
462 |
|
464 | |||
463 |
|
465 | |||
464 | Notebook.prototype.execute_selected_cell = function (add_new) { |
|
466 | Notebook.prototype.execute_selected_cell = function (add_new) { | |
465 | if (add_new === undefined) {add_new = true;}; |
|
467 | if (add_new === undefined) {add_new = true;}; | |
466 | var that = this; |
|
468 | var that = this; | |
467 | var cell = that.selected_cell(); |
|
469 | var cell = that.selected_cell(); | |
468 | var cell_index = that.find_cell_index(cell); |
|
470 | var cell_index = that.find_cell_index(cell); | |
469 | // TODO: the logic here needs to be moved into appropriate |
|
471 | // TODO: the logic here needs to be moved into appropriate | |
470 | // methods of Notebook. |
|
472 | // methods of Notebook. | |
471 | if (cell instanceof IPython.CodeCell) { |
|
473 | if (cell instanceof IPython.CodeCell) { | |
472 | cell.clear_output(); |
|
474 | cell.clear_output(); | |
473 | var code = cell.get_code(); |
|
475 | var code = cell.get_code(); | |
474 | if (that.notebook_load_re.test(code)) { |
|
476 | if (that.notebook_load_re.test(code)) { | |
475 | var code_parts = code.split(' '); |
|
477 | var code_parts = code.split(' '); | |
476 | if (code_parts.length === 3) { |
|
478 | if (code_parts.length === 3) { | |
477 | that.load_notebook(code_parts[2]); |
|
479 | that.load_notebook(code_parts[2]); | |
478 | }; |
|
480 | }; | |
479 | } else if (that.notebook_save_re.test(code)) { |
|
481 | } else if (that.notebook_save_re.test(code)) { | |
480 | var code_parts = code.split(' '); |
|
482 | var code_parts = code.split(' '); | |
481 | if (code_parts.length === 3) { |
|
483 | if (code_parts.length === 3) { | |
482 | that.save_notebook(code_parts[2]); |
|
484 | that.save_notebook(code_parts[2]); | |
483 | } else { |
|
485 | } else { | |
484 | that.save_notebook() |
|
486 | that.save_notebook() | |
485 | }; |
|
487 | }; | |
486 | } else { |
|
488 | } else { | |
487 | var msg_id = that.kernel.execute(cell.get_code()); |
|
489 | var msg_id = that.kernel.execute(cell.get_code()); | |
488 | that.msg_cell_map[msg_id] = cell.cell_id; |
|
490 | that.msg_cell_map[msg_id] = cell.cell_id; | |
489 | }; |
|
491 | }; | |
490 | } else if (cell instanceof IPython.TextCell) { |
|
492 | } else if (cell instanceof IPython.TextCell) { | |
491 | cell.render(); |
|
493 | cell.render(); | |
492 | } |
|
494 | } | |
493 | if ((cell_index === (that.ncells()-1)) && add_new) { |
|
495 | if ((cell_index === (that.ncells()-1)) && add_new) { | |
494 | that.insert_code_cell_after(); |
|
496 | that.insert_code_cell_after(); | |
495 | // If we are adding a new cell at the end, scroll down to show it. |
|
497 | // If we are adding a new cell at the end, scroll down to show it. | |
496 | that.scroll_to_bottom(); |
|
498 | that.scroll_to_bottom(); | |
497 | } else { |
|
499 | } else { | |
498 | that.select(cell_index+1); |
|
500 | that.select(cell_index+1); | |
499 | }; |
|
501 | }; | |
500 | }; |
|
502 | }; | |
501 |
|
503 | |||
502 |
|
504 | |||
503 | Notebook.prototype.execute_all_cells = function () { |
|
505 | Notebook.prototype.execute_all_cells = function () { | |
504 | var ncells = this.ncells(); |
|
506 | var ncells = this.ncells(); | |
505 | for (var i=0; i<ncells; i++) { |
|
507 | for (var i=0; i<ncells; i++) { | |
506 | this.select(i); |
|
508 | this.select(i); | |
507 | this.execute_selected_cell(false); |
|
509 | this.execute_selected_cell(false); | |
508 | }; |
|
510 | }; | |
509 | this.scroll_to_bottom(); |
|
511 | this.scroll_to_bottom(); | |
510 | }; |
|
512 | }; | |
511 |
|
513 | |||
512 |
|
514 | |||
513 | Notebook.prototype.complete_cell = function (cell, line, cursor_pos) { |
|
515 | Notebook.prototype.complete_cell = function (cell, line, cursor_pos) { | |
514 | var msg_id = this.kernel.complete(line, cursor_pos); |
|
516 | var msg_id = this.kernel.complete(line, cursor_pos); | |
515 | this.msg_cell_map[msg_id] = cell.cell_id; |
|
517 | this.msg_cell_map[msg_id] = cell.cell_id; | |
516 | }; |
|
518 | }; | |
517 |
|
519 | |||
518 | // Persistance and loading |
|
520 | // Persistance and loading | |
519 |
|
521 | |||
520 |
|
522 | |||
521 | Notebook.prototype.fromJSON = function (data) { |
|
523 | Notebook.prototype.fromJSON = function (data) { | |
522 | var ncells = this.ncells(); |
|
524 | var ncells = this.ncells(); | |
523 | for (var i=0; i<ncells; i++) { |
|
525 | for (var i=0; i<ncells; i++) { | |
524 | // Always delete cell 0 as they get renumbered as they are deleted. |
|
526 | // Always delete cell 0 as they get renumbered as they are deleted. | |
525 | this.delete_cell(0); |
|
527 | this.delete_cell(0); | |
526 | }; |
|
528 | }; | |
527 | var new_cells = data.cells; |
|
529 | var new_cells = data.cells; | |
528 | ncells = new_cells.length; |
|
530 | ncells = new_cells.length; | |
529 | var cell_data = null; |
|
531 | var cell_data = null; | |
530 | for (var i=0; i<ncells; i++) { |
|
532 | for (var i=0; i<ncells; i++) { | |
531 | cell_data = new_cells[i]; |
|
533 | cell_data = new_cells[i]; | |
532 | if (cell_data.cell_type == 'code') { |
|
534 | if (cell_data.cell_type == 'code') { | |
533 | this.insert_code_cell_after(); |
|
535 | this.insert_code_cell_after(); | |
534 | this.selected_cell().fromJSON(cell_data); |
|
536 | this.selected_cell().fromJSON(cell_data); | |
535 | } else if (cell_data.cell_type === 'text') { |
|
537 | } else if (cell_data.cell_type === 'text') { | |
536 | this.insert_text_cell_after(); |
|
538 | this.insert_text_cell_after(); | |
537 | this.selected_cell().fromJSON(cell_data); |
|
539 | this.selected_cell().fromJSON(cell_data); | |
538 | }; |
|
540 | }; | |
539 | }; |
|
541 | }; | |
540 | }; |
|
542 | }; | |
541 |
|
543 | |||
542 |
|
544 | |||
543 | Notebook.prototype.toJSON = function () { |
|
545 | Notebook.prototype.toJSON = function () { | |
544 | var cells = this.cells(); |
|
546 | var cells = this.cells(); | |
545 | var ncells = cells.length; |
|
547 | var ncells = cells.length; | |
546 | cell_array = new Array(ncells); |
|
548 | cell_array = new Array(ncells); | |
547 | for (var i=0; i<ncells; i++) { |
|
549 | for (var i=0; i<ncells; i++) { | |
548 | cell_array[i] = cells[i].toJSON(); |
|
550 | cell_array[i] = cells[i].toJSON(); | |
549 | }; |
|
551 | }; | |
550 | json = { |
|
552 | json = { | |
551 | cells : cell_array |
|
553 | cells : cell_array | |
552 | }; |
|
554 | }; | |
553 | return json |
|
555 | return json | |
554 | }; |
|
556 | }; | |
555 |
|
557 | |||
556 |
|
558 | |||
557 | Notebook.prototype.test_filename = function (filename) { |
|
559 | Notebook.prototype.test_filename = function (filename) { | |
558 | if (this.notebook_filename_re.test(filename)) { |
|
560 | if (this.notebook_filename_re.test(filename)) { | |
559 | return true; |
|
561 | return true; | |
560 | } else { |
|
562 | } else { | |
561 | var bad_filename = $('<div/>'); |
|
563 | var bad_filename = $('<div/>'); | |
562 | bad_filename.html( |
|
564 | bad_filename.html( | |
563 | "The filename you entered (" + filename + ") is not valid. Notebook filenames must have the following form: foo.ipynb" |
|
565 | "The filename you entered (" + filename + ") is not valid. Notebook filenames must have the following form: foo.ipynb" | |
564 | ); |
|
566 | ); | |
565 | bad_filename.dialog({title: 'Invalid filename', modal: true}); |
|
567 | bad_filename.dialog({title: 'Invalid filename', modal: true}); | |
566 | return false; |
|
568 | return false; | |
567 | }; |
|
569 | }; | |
568 | }; |
|
570 | }; | |
569 |
|
571 | |||
570 | Notebook.prototype.save_notebook = function (filename) { |
|
572 | Notebook.prototype.save_notebook = function (filename) { | |
571 | this.filename = filename || this.filename || ''; |
|
573 | this.filename = filename || this.filename || ''; | |
572 | if (this.filename === '') { |
|
574 | if (this.filename === '') { | |
573 | var no_filename = $('<div/>'); |
|
575 | var no_filename = $('<div/>'); | |
574 | no_filename.html( |
|
576 | no_filename.html( | |
575 | "This notebook has no filename, please specify a filename of the form: foo.ipynb" |
|
577 | "This notebook has no filename, please specify a filename of the form: foo.ipynb" | |
576 | ); |
|
578 | ); | |
577 | no_filename.dialog({title: 'Missing filename', modal: true}); |
|
579 | no_filename.dialog({title: 'Missing filename', modal: true}); | |
578 | return; |
|
580 | return; | |
579 | } |
|
581 | } | |
580 | if (!this.test_filename(this.filename)) {return;} |
|
582 | if (!this.test_filename(this.filename)) {return;} | |
581 | var thedata = this.toJSON(); |
|
583 | var thedata = this.toJSON(); | |
582 | var settings = { |
|
584 | var settings = { | |
583 | processData : false, |
|
585 | processData : false, | |
584 | cache : false, |
|
586 | cache : false, | |
585 | type : "PUT", |
|
587 | type : "PUT", | |
586 | data : JSON.stringify(thedata), |
|
588 | data : JSON.stringify(thedata), | |
587 | success : function (data, status, xhr) {console.log(data);} |
|
589 | success : function (data, status, xhr) {console.log(data);} | |
588 | }; |
|
590 | }; | |
589 | $.ajax("/notebooks/" + this.filename, settings); |
|
591 | $.ajax("/notebooks/" + this.filename, settings); | |
590 | }; |
|
592 | }; | |
591 |
|
593 | |||
592 |
|
594 | |||
593 | Notebook.prototype.load_notebook = function (filename) { |
|
595 | Notebook.prototype.load_notebook = function (filename) { | |
594 | if (!this.test_filename(filename)) {return;} |
|
596 | if (!this.test_filename(filename)) {return;} | |
595 | var that = this; |
|
597 | var that = this; | |
596 | // We do the call with settings so we can set cache to false. |
|
598 | // We do the call with settings so we can set cache to false. | |
597 | var settings = { |
|
599 | var settings = { | |
598 | processData : false, |
|
600 | processData : false, | |
599 | cache : false, |
|
601 | cache : false, | |
600 | type : "GET", |
|
602 | type : "GET", | |
601 | dataType : "json", |
|
603 | dataType : "json", | |
602 | success : function (data, status, xhr) { |
|
604 | success : function (data, status, xhr) { | |
603 | that.fromJSON(data); |
|
605 | that.fromJSON(data); | |
604 | that.filename = filename; |
|
606 | that.filename = filename; | |
605 | that.kernel.restart(); |
|
607 | that.kernel.restart(); | |
606 | } |
|
608 | } | |
607 | }; |
|
609 | }; | |
608 | $.ajax("/notebooks/" + filename, settings); |
|
610 | $.ajax("/notebooks/" + filename, settings); | |
609 | } |
|
611 | } | |
610 |
|
612 | |||
611 | IPython.Notebook = Notebook; |
|
613 | IPython.Notebook = Notebook; | |
612 |
|
614 | |||
613 | return IPython; |
|
615 | return IPython; | |
614 |
|
616 | |||
615 | }(IPython)); |
|
617 | }(IPython)); | |
616 |
|
618 |
General Comments 0
You need to be logged in to leave comments.
Login now