##// END OF EJS Templates
interpret 'plaintext' cells with their new name 'raw'
MinRK -
Show More
@@ -1,1342 +1,1347 b''
1 1 //----------------------------------------------------------------------------
2 2 // Copyright (C) 2008-2011 The IPython Development Team
3 3 //
4 4 // Distributed under the terms of the BSD License. The full license is in
5 5 // the file COPYING, distributed as part of this software.
6 6 //----------------------------------------------------------------------------
7 7
8 8 //============================================================================
9 9 // Notebook
10 10 //============================================================================
11 11
12 12 var IPython = (function (IPython) {
13 13
14 14 var utils = IPython.utils;
15 15
16 16 var Notebook = function (selector) {
17 17 this.read_only = IPython.read_only;
18 18 this.element = $(selector);
19 19 this.element.scroll();
20 20 this.element.data("notebook", this);
21 21 this.next_prompt_number = 1;
22 22 this.kernel = null;
23 23 this.clipboard = null;
24 24 this.paste_enabled = false;
25 25 this.dirty = false;
26 26 this.msg_cell_map = {};
27 27 this.metadata = {};
28 28 this.control_key_active = false;
29 29 this.notebook_id = null;
30 30 this.notebook_name = null;
31 31 this.notebook_name_blacklist_re = /[\/\\]/;
32 32 this.nbformat = 3 // Increment this when changing the nbformat
33 33 this.style();
34 34 this.create_elements();
35 35 this.bind_events();
36 36 this.set_tooltipontab(true);
37 37 this.set_smartcompleter(true);
38 38 this.set_timebeforetooltip(1200);
39 39 };
40 40
41 41
42 42 Notebook.prototype.style = function () {
43 43 $('div#notebook').addClass('border-box-sizing');
44 44 };
45 45
46 46
47 47 Notebook.prototype.create_elements = function () {
48 48 // We add this end_space div to the end of the notebook div to:
49 49 // i) provide a margin between the last cell and the end of the notebook
50 50 // ii) to prevent the div from scrolling up when the last cell is being
51 51 // edited, but is too low on the page, which browsers will do automatically.
52 52 var that = this;
53 53 var end_space = $('<div/>').addClass('end_space').height("30%");
54 54 end_space.dblclick(function (e) {
55 55 if (that.read_only) return;
56 56 var ncells = that.ncells();
57 57 that.insert_cell_below('code',ncells-1);
58 58 });
59 59 this.element.append(end_space);
60 60 $('div#notebook').addClass('border-box-sizing');
61 61 };
62 62
63 63
64 64 Notebook.prototype.bind_events = function () {
65 65 var that = this;
66 66 $(document).keydown(function (event) {
67 67 // console.log(event);
68 68 if (that.read_only) return true;
69 69
70 70 // Save (CTRL+S) or (AppleKey+S)
71 71 //metaKey = applekey on mac
72 72 if ((event.ctrlKey || event.metaKey) && event.keyCode==83) {
73 73 that.save_notebook();
74 74 event.preventDefault();
75 75 return false;
76 76 } else if (event.which === 27) {
77 77 // Intercept escape at highest level to avoid closing
78 78 // websocket connection with firefox
79 79 event.preventDefault();
80 80 }
81 81 if (event.which === 38 && !event.shiftKey) {
82 82 var cell = that.get_selected_cell();
83 83 if (cell.at_top()) {
84 84 event.preventDefault();
85 85 that.select_prev();
86 86 };
87 87 } else if (event.which === 40 && !event.shiftKey) {
88 88 var cell = that.get_selected_cell();
89 89 if (cell.at_bottom()) {
90 90 event.preventDefault();
91 91 that.select_next();
92 92 };
93 93 } else if (event.which === 13 && event.shiftKey) {
94 94 that.execute_selected_cell();
95 95 return false;
96 96 } else if (event.which === 13 && event.ctrlKey) {
97 97 that.execute_selected_cell({terminal:true});
98 98 return false;
99 99 } else if (event.which === 77 && event.ctrlKey && that.control_key_active == false) {
100 100 that.control_key_active = true;
101 101 return false;
102 102 } else if (event.which === 88 && that.control_key_active) {
103 103 // Cut selected cell = x
104 104 that.cut_cell();
105 105 that.control_key_active = false;
106 106 return false;
107 107 } else if (event.which === 67 && that.control_key_active) {
108 108 // Copy selected cell = c
109 109 that.copy_cell();
110 110 that.control_key_active = false;
111 111 return false;
112 112 } else if (event.which === 86 && that.control_key_active) {
113 113 // Paste selected cell = v
114 114 that.paste_cell();
115 115 that.control_key_active = false;
116 116 return false;
117 117 } else if (event.which === 68 && that.control_key_active) {
118 118 // Delete selected cell = d
119 119 that.delete_cell();
120 120 that.control_key_active = false;
121 121 return false;
122 122 } else if (event.which === 65 && that.control_key_active) {
123 123 // Insert code cell above selected = a
124 124 that.insert_cell_above('code');
125 125 that.control_key_active = false;
126 126 return false;
127 127 } else if (event.which === 66 && that.control_key_active) {
128 128 // Insert code cell below selected = b
129 129 that.insert_cell_below('code');
130 130 that.control_key_active = false;
131 131 return false;
132 132 } else if (event.which === 89 && that.control_key_active) {
133 133 // To code = y
134 134 that.to_code();
135 135 that.control_key_active = false;
136 136 return false;
137 137 } else if (event.which === 77 && that.control_key_active) {
138 138 // To markdown = m
139 139 that.to_markdown();
140 140 that.control_key_active = false;
141 141 return false;
142 142 } else if (event.which === 84 && that.control_key_active) {
143 143 // To Raw = t
144 144 that.to_raw();
145 145 that.control_key_active = false;
146 146 return false;
147 147 } else if (event.which === 49 && that.control_key_active) {
148 148 // To Heading 1 = 1
149 149 that.to_heading(undefined, 1);
150 150 that.control_key_active = false;
151 151 return false;
152 152 } else if (event.which === 50 && that.control_key_active) {
153 153 // To Heading 2 = 2
154 154 that.to_heading(undefined, 2);
155 155 that.control_key_active = false;
156 156 return false;
157 157 } else if (event.which === 51 && that.control_key_active) {
158 158 // To Heading 3 = 3
159 159 that.to_heading(undefined, 3);
160 160 that.control_key_active = false;
161 161 return false;
162 162 } else if (event.which === 52 && that.control_key_active) {
163 163 // To Heading 4 = 4
164 164 that.to_heading(undefined, 4);
165 165 that.control_key_active = false;
166 166 return false;
167 167 } else if (event.which === 53 && that.control_key_active) {
168 168 // To Heading 5 = 5
169 169 that.to_heading(undefined, 5);
170 170 that.control_key_active = false;
171 171 return false;
172 172 } else if (event.which === 54 && that.control_key_active) {
173 173 // To Heading 6 = 6
174 174 that.to_heading(undefined, 6);
175 175 that.control_key_active = false;
176 176 return false;
177 177 } else if (event.which === 79 && that.control_key_active) {
178 178 // Toggle output = o
179 179 that.toggle_output();
180 180 that.control_key_active = false;
181 181 return false;
182 182 } else if (event.which === 83 && that.control_key_active) {
183 183 // Save notebook = s
184 184 that.save_notebook();
185 185 that.control_key_active = false;
186 186 return false;
187 187 } else if (event.which === 74 && that.control_key_active) {
188 188 // Move cell down = j
189 189 that.move_cell_down();
190 190 that.control_key_active = false;
191 191 return false;
192 192 } else if (event.which === 75 && that.control_key_active) {
193 193 // Move cell up = k
194 194 that.move_cell_up();
195 195 that.control_key_active = false;
196 196 return false;
197 197 } else if (event.which === 80 && that.control_key_active) {
198 198 // Select previous = p
199 199 that.select_prev();
200 200 that.control_key_active = false;
201 201 return false;
202 202 } else if (event.which === 78 && that.control_key_active) {
203 203 // Select next = n
204 204 that.select_next();
205 205 that.control_key_active = false;
206 206 return false;
207 207 } else if (event.which === 76 && that.control_key_active) {
208 208 // Toggle line numbers = l
209 209 that.cell_toggle_line_numbers();
210 210 that.control_key_active = false;
211 211 return false;
212 212 } else if (event.which === 73 && that.control_key_active) {
213 213 // Interrupt kernel = i
214 214 that.kernel.interrupt();
215 215 that.control_key_active = false;
216 216 return false;
217 217 } else if (event.which === 190 && that.control_key_active) {
218 218 // Restart kernel = . # matches qt console
219 219 that.restart_kernel();
220 220 that.control_key_active = false;
221 221 return false;
222 222 } else if (event.which === 72 && that.control_key_active) {
223 223 // Show keyboard shortcuts = h
224 224 IPython.quick_help.show_keyboard_shortcuts();
225 225 that.control_key_active = false;
226 226 return false;
227 227 } else if (that.control_key_active) {
228 228 that.control_key_active = false;
229 229 return true;
230 230 };
231 231 return true;
232 232 });
233 233
234 234 this.element.bind('collapse_pager', function () {
235 235 var app_height = $('div#main_app').height(); // content height
236 236 var splitter_height = $('div#pager_splitter').outerHeight(true);
237 237 var new_height = app_height - splitter_height;
238 238 that.element.animate({height : new_height + 'px'}, 'fast');
239 239 });
240 240
241 241 this.element.bind('expand_pager', function () {
242 242 var app_height = $('div#main_app').height(); // content height
243 243 var splitter_height = $('div#pager_splitter').outerHeight(true);
244 244 var pager_height = $('div#pager').outerHeight(true);
245 245 var new_height = app_height - pager_height - splitter_height;
246 246 that.element.animate({height : new_height + 'px'}, 'fast');
247 247 });
248 248
249 249 $(window).bind('beforeunload', function () {
250 250 // TODO: Make killing the kernel configurable.
251 251 var kill_kernel = false;
252 252 if (kill_kernel) {
253 253 that.kernel.kill();
254 254 }
255 255 if (that.dirty && ! that.read_only) {
256 256 return "You have unsaved changes that will be lost if you leave this page.";
257 257 };
258 258 // Null is the *only* return value that will make the browser not
259 259 // pop up the "don't leave" dialog.
260 260 return null;
261 261 });
262 262 };
263 263
264 264
265 265 Notebook.prototype.scroll_to_bottom = function () {
266 266 this.element.animate({scrollTop:this.element.get(0).scrollHeight}, 0);
267 267 };
268 268
269 269
270 270 Notebook.prototype.scroll_to_top = function () {
271 271 this.element.animate({scrollTop:0}, 0);
272 272 };
273 273
274 274
275 275 // Cell indexing, retrieval, etc.
276 276
277 277 Notebook.prototype.get_cell_elements = function () {
278 278 return this.element.children("div.cell");
279 279 };
280 280
281 281
282 282 Notebook.prototype.get_cell_element = function (index) {
283 283 var result = null;
284 284 var e = this.get_cell_elements().eq(index);
285 285 if (e.length !== 0) {
286 286 result = e;
287 287 }
288 288 return result;
289 289 };
290 290
291 291
292 292 Notebook.prototype.ncells = function (cell) {
293 293 return this.get_cell_elements().length;
294 294 };
295 295
296 296
297 297 // TODO: we are often calling cells as cells()[i], which we should optimize
298 298 // to cells(i) or a new method.
299 299 Notebook.prototype.get_cells = function () {
300 300 return this.get_cell_elements().toArray().map(function (e) {
301 301 return $(e).data("cell");
302 302 });
303 303 };
304 304
305 305
306 306 Notebook.prototype.get_cell = function (index) {
307 307 var result = null;
308 308 var ce = this.get_cell_element(index);
309 309 if (ce !== null) {
310 310 result = ce.data('cell');
311 311 }
312 312 return result;
313 313 }
314 314
315 315
316 316 Notebook.prototype.get_next_cell = function (cell) {
317 317 var result = null;
318 318 var index = this.find_cell_index(cell);
319 319 if (index !== null && index < this.ncells()) {
320 320 result = this.get_cell(index+1);
321 321 }
322 322 return result;
323 323 }
324 324
325 325
326 326 Notebook.prototype.get_prev_cell = function (cell) {
327 327 var result = null;
328 328 var index = this.find_cell_index(cell);
329 329 if (index !== null && index > 1) {
330 330 result = this.get_cell(index-1);
331 331 }
332 332 return result;
333 333 }
334 334
335 335 Notebook.prototype.find_cell_index = function (cell) {
336 336 var result = null;
337 337 this.get_cell_elements().filter(function (index) {
338 338 if ($(this).data("cell") === cell) {
339 339 result = index;
340 340 };
341 341 });
342 342 return result;
343 343 };
344 344
345 345
346 346 Notebook.prototype.index_or_selected = function (index) {
347 347 var i;
348 348 if (index === undefined || index === null) {
349 349 i = this.get_selected_index();
350 350 if (i === null) {
351 351 i = 0;
352 352 }
353 353 } else {
354 354 i = index;
355 355 }
356 356 return i;
357 357 };
358 358
359 359
360 360 Notebook.prototype.get_selected_cell = function () {
361 361 var index = this.get_selected_index();
362 362 return this.get_cell(index);
363 363 };
364 364
365 365
366 366 Notebook.prototype.is_valid_cell_index = function (index) {
367 367 if (index !== null && index >= 0 && index < this.ncells()) {
368 368 return true;
369 369 } else {
370 370 return false;
371 371 };
372 372 }
373 373
374 374 Notebook.prototype.get_selected_index = function () {
375 375 var result = null;
376 376 this.get_cell_elements().filter(function (index) {
377 377 if ($(this).data("cell").selected === true) {
378 378 result = index;
379 379 };
380 380 });
381 381 return result;
382 382 };
383 383
384 384
385 385 Notebook.prototype.cell_for_msg = function (msg_id) {
386 386 var cell_id = this.msg_cell_map[msg_id];
387 387 var result = null;
388 388 this.get_cell_elements().filter(function (index) {
389 389 cell = $(this).data("cell");
390 390 if (cell.cell_id === cell_id) {
391 391 result = cell;
392 392 };
393 393 });
394 394 return result;
395 395 };
396 396
397 397
398 398 // Cell selection.
399 399
400 400 Notebook.prototype.select = function (index) {
401 401 if (index !== undefined && index >= 0 && index < this.ncells()) {
402 402 sindex = this.get_selected_index()
403 403 if (sindex !== null && index !== sindex) {
404 404 this.get_cell(sindex).unselect();
405 405 };
406 406 var cell = this.get_cell(index)
407 407 cell.select();
408 408 if (cell.cell_type === 'heading') {
409 409 $([IPython.events]).trigger('selected_cell_type_changed.Notebook',
410 410 {'cell_type':cell.cell_type,level:cell.level}
411 411 );
412 412 } else {
413 413 $([IPython.events]).trigger('selected_cell_type_changed.Notebook',
414 414 {'cell_type':cell.cell_type}
415 415 );
416 416 };
417 417 };
418 418 return this;
419 419 };
420 420
421 421
422 422 Notebook.prototype.select_next = function () {
423 423 var index = this.get_selected_index();
424 424 if (index !== null && index >= 0 && (index+1) < this.ncells()) {
425 425 this.select(index+1);
426 426 };
427 427 return this;
428 428 };
429 429
430 430
431 431 Notebook.prototype.select_prev = function () {
432 432 var index = this.get_selected_index();
433 433 if (index !== null && index >= 0 && (index-1) < this.ncells()) {
434 434 this.select(index-1);
435 435 };
436 436 return this;
437 437 };
438 438
439 439
440 440 // Cell movement
441 441
442 442 Notebook.prototype.move_cell_up = function (index) {
443 443 var i = this.index_or_selected();
444 444 if (i !== null && i < this.ncells() && i > 0) {
445 445 var pivot = this.get_cell_element(i-1);
446 446 var tomove = this.get_cell_element(i);
447 447 if (pivot !== null && tomove !== null) {
448 448 tomove.detach();
449 449 pivot.before(tomove);
450 450 this.select(i-1);
451 451 };
452 452 };
453 453 this.dirty = true;
454 454 return this;
455 455 };
456 456
457 457
458 458 Notebook.prototype.move_cell_down = function (index) {
459 459 var i = this.index_or_selected();
460 460 if (i !== null && i < (this.ncells()-1) && i >= 0) {
461 461 var pivot = this.get_cell_element(i+1);
462 462 var tomove = this.get_cell_element(i);
463 463 if (pivot !== null && tomove !== null) {
464 464 tomove.detach();
465 465 pivot.after(tomove);
466 466 this.select(i+1);
467 467 };
468 468 };
469 469 this.dirty = true;
470 470 return this;
471 471 };
472 472
473 473
474 474 Notebook.prototype.sort_cells = function () {
475 475 // This is not working right now. Calling this will actually crash
476 476 // the browser. I think there is an infinite loop in here...
477 477 var ncells = this.ncells();
478 478 var sindex = this.get_selected_index();
479 479 var swapped;
480 480 do {
481 481 swapped = false;
482 482 for (var i=1; i<ncells; i++) {
483 483 current = this.get_cell(i);
484 484 previous = this.get_cell(i-1);
485 485 if (previous.input_prompt_number > current.input_prompt_number) {
486 486 this.move_cell_up(i);
487 487 swapped = true;
488 488 };
489 489 };
490 490 } while (swapped);
491 491 this.select(sindex);
492 492 return this;
493 493 };
494 494
495 495 // Insertion, deletion.
496 496
497 497 Notebook.prototype.delete_cell = function (index) {
498 498 var i = this.index_or_selected(index);
499 499 if (this.is_valid_cell_index(i)) {
500 500 var ce = this.get_cell_element(i);
501 501 ce.remove();
502 502 if (i === (this.ncells())) {
503 503 this.select(i-1);
504 504 } else {
505 505 this.select(i);
506 506 };
507 507 this.dirty = true;
508 508 };
509 509 return this;
510 510 };
511 511
512 512
513 513 Notebook.prototype.insert_cell_below = function (type, index) {
514 514 // type = ('code','html','markdown')
515 515 // index = cell index or undefined to insert below selected
516 516 index = this.index_or_selected(index);
517 517 var cell = null;
518 518 if (this.ncells() === 0 || this.is_valid_cell_index(index)) {
519 519 if (type === 'code') {
520 520 cell = new IPython.CodeCell(this);
521 521 cell.set_input_prompt();
522 522 } else if (type === 'markdown') {
523 523 cell = new IPython.MarkdownCell(this);
524 524 } else if (type === 'html') {
525 525 cell = new IPython.HTMLCell(this);
526 526 } else if (type === 'raw') {
527 527 cell = new IPython.RawCell(this);
528 528 } else if (type === 'heading') {
529 529 cell = new IPython.HeadingCell(this);
530 530 };
531 531 if (cell !== null) {
532 532 if (this.ncells() === 0) {
533 533 this.element.find('div.end_space').before(cell.element);
534 534 } else if (this.is_valid_cell_index(index)) {
535 535 this.get_cell_element(index).after(cell.element);
536 536 };
537 537 cell.render();
538 538 this.select(this.find_cell_index(cell));
539 539 this.dirty = true;
540 540 return cell;
541 541 };
542 542 };
543 543 return cell;
544 544 };
545 545
546 546
547 547 Notebook.prototype.insert_cell_above = function (type, index) {
548 548 // type = ('code','html','markdown')
549 549 // index = cell index or undefined to insert above selected
550 550 index = this.index_or_selected(index);
551 551 var cell = null;
552 552 if (this.ncells() === 0 || this.is_valid_cell_index(index)) {
553 553 if (type === 'code') {
554 554 cell = new IPython.CodeCell(this);
555 555 cell.set_input_prompt();
556 556 } else if (type === 'markdown') {
557 557 cell = new IPython.MarkdownCell(this);
558 558 } else if (type === 'html') {
559 559 cell = new IPython.HTMLCell(this);
560 560 } else if (type === 'raw') {
561 561 cell = new IPython.RawCell(this);
562 562 } else if (type === 'heading') {
563 563 cell = new IPython.HeadingCell(this);
564 564 };
565 565 if (cell !== null) {
566 566 if (this.ncells() === 0) {
567 567 this.element.find('div.end_space').before(cell.element);
568 568 } else if (this.is_valid_cell_index(index)) {
569 569 this.get_cell_element(index).before(cell.element);
570 570 };
571 571 cell.render();
572 572 this.select(this.find_cell_index(cell));
573 573 this.dirty = true;
574 574 return cell;
575 575 };
576 576 };
577 577 return cell;
578 578 };
579 579
580 580
581 581 Notebook.prototype.to_code = function (index) {
582 582 var i = this.index_or_selected(index);
583 583 if (this.is_valid_cell_index(i)) {
584 584 var source_element = this.get_cell_element(i);
585 585 var source_cell = source_element.data("cell");
586 586 if (!(source_cell instanceof IPython.CodeCell)) {
587 587 target_cell = this.insert_cell_below('code',i);
588 588 var text = source_cell.get_text();
589 589 if (text === source_cell.placeholder) {
590 590 text = '';
591 591 }
592 592 target_cell.set_text(text);
593 593 source_element.remove();
594 594 this.dirty = true;
595 595 };
596 596 };
597 597 };
598 598
599 599
600 600 Notebook.prototype.to_markdown = function (index) {
601 601 var i = this.index_or_selected(index);
602 602 if (this.is_valid_cell_index(i)) {
603 603 var source_element = this.get_cell_element(i);
604 604 var source_cell = source_element.data("cell");
605 605 if (!(source_cell instanceof IPython.MarkdownCell)) {
606 606 target_cell = this.insert_cell_below('markdown',i);
607 607 var text = source_cell.get_text();
608 608 if (text === source_cell.placeholder) {
609 609 text = '';
610 610 };
611 611 // The edit must come before the set_text.
612 612 target_cell.edit();
613 613 target_cell.set_text(text);
614 614 source_element.remove();
615 615 this.dirty = true;
616 616 };
617 617 };
618 618 };
619 619
620 620
621 621 Notebook.prototype.to_html = function (index) {
622 622 var i = this.index_or_selected(index);
623 623 if (this.is_valid_cell_index(i)) {
624 624 var source_element = this.get_cell_element(i);
625 625 var source_cell = source_element.data("cell");
626 626 var target_cell = null;
627 627 if (!(source_cell instanceof IPython.HTMLCell)) {
628 628 target_cell = this.insert_cell_below('html',i);
629 629 var text = source_cell.get_text();
630 630 if (text === source_cell.placeholder) {
631 631 text = '';
632 632 };
633 633 // The edit must come before the set_text.
634 634 target_cell.edit();
635 635 target_cell.set_text(text);
636 636 source_element.remove();
637 637 this.dirty = true;
638 638 };
639 639 };
640 640 };
641 641
642 642
643 643 Notebook.prototype.to_raw = function (index) {
644 644 var i = this.index_or_selected(index);
645 645 if (this.is_valid_cell_index(i)) {
646 646 var source_element = this.get_cell_element(i);
647 647 var source_cell = source_element.data("cell");
648 648 var target_cell = null;
649 649 if (!(source_cell instanceof IPython.RawCell)) {
650 650 target_cell = this.insert_cell_below('raw',i);
651 651 var text = source_cell.get_text();
652 652 if (text === source_cell.placeholder) {
653 653 text = '';
654 654 };
655 655 // The edit must come before the set_text.
656 656 target_cell.edit();
657 657 target_cell.set_text(text);
658 658 source_element.remove();
659 659 this.dirty = true;
660 660 };
661 661 };
662 662 };
663 663
664 664
665 665 Notebook.prototype.to_heading = function (index, level) {
666 666 level = level || 1;
667 667 var i = this.index_or_selected(index);
668 668 if (this.is_valid_cell_index(i)) {
669 669 var source_element = this.get_cell_element(i);
670 670 var source_cell = source_element.data("cell");
671 671 var target_cell = null;
672 672 if (source_cell instanceof IPython.HeadingCell) {
673 673 source_cell.set_level(level);
674 674 } else {
675 675 target_cell = this.insert_cell_below('heading',i);
676 676 var text = source_cell.get_text();
677 677 if (text === source_cell.placeholder) {
678 678 text = '';
679 679 };
680 680 // The edit must come before the set_text.
681 681 target_cell.set_level(level);
682 682 target_cell.edit();
683 683 target_cell.set_text(text);
684 684 source_element.remove();
685 685 this.dirty = true;
686 686 };
687 687 $([IPython.events]).trigger('selected_cell_type_changed.Notebook',
688 688 {'cell_type':'heading',level:level}
689 689 );
690 690 };
691 691 };
692 692
693 693
694 694 // Cut/Copy/Paste
695 695
696 696 Notebook.prototype.enable_paste = function () {
697 697 var that = this;
698 698 if (!this.paste_enabled) {
699 699 $('#paste_cell').removeClass('ui-state-disabled')
700 700 .on('click', function () {that.paste_cell();});
701 701 $('#paste_cell_above').removeClass('ui-state-disabled')
702 702 .on('click', function () {that.paste_cell_above();});
703 703 $('#paste_cell_below').removeClass('ui-state-disabled')
704 704 .on('click', function () {that.paste_cell_below();});
705 705 this.paste_enabled = true;
706 706 };
707 707 };
708 708
709 709
710 710 Notebook.prototype.disable_paste = function () {
711 711 if (this.paste_enabled) {
712 712 $('#paste_cell').addClass('ui-state-disabled').off('click');
713 713 $('#paste_cell_above').addClass('ui-state-disabled').off('click');
714 714 $('#paste_cell_below').addClass('ui-state-disabled').off('click');
715 715 this.paste_enabled = false;
716 716 };
717 717 };
718 718
719 719
720 720 Notebook.prototype.cut_cell = function () {
721 721 this.copy_cell();
722 722 this.delete_cell();
723 723 }
724 724
725 725 Notebook.prototype.copy_cell = function () {
726 726 var cell = this.get_selected_cell();
727 727 this.clipboard = cell.toJSON();
728 728 this.enable_paste();
729 729 };
730 730
731 731
732 732 Notebook.prototype.paste_cell = function () {
733 733 if (this.clipboard !== null && this.paste_enabled) {
734 734 var cell_data = this.clipboard;
735 735 var new_cell = this.insert_cell_above(cell_data.cell_type);
736 736 new_cell.fromJSON(cell_data);
737 737 old_cell = this.get_next_cell(new_cell);
738 738 this.delete_cell(this.find_cell_index(old_cell));
739 739 this.select(this.find_cell_index(new_cell));
740 740 };
741 741 };
742 742
743 743
744 744 Notebook.prototype.paste_cell_above = function () {
745 745 if (this.clipboard !== null && this.paste_enabled) {
746 746 var cell_data = this.clipboard;
747 747 var new_cell = this.insert_cell_above(cell_data.cell_type);
748 748 new_cell.fromJSON(cell_data);
749 749 };
750 750 };
751 751
752 752
753 753 Notebook.prototype.paste_cell_below = function () {
754 754 if (this.clipboard !== null && this.paste_enabled) {
755 755 var cell_data = this.clipboard;
756 756 var new_cell = this.insert_cell_below(cell_data.cell_type);
757 757 new_cell.fromJSON(cell_data);
758 758 };
759 759 };
760 760
761 761
762 762 // Split/merge
763 763
764 764 Notebook.prototype.split_cell = function () {
765 765 // Todo: implement spliting for other cell types.
766 766 var cell = this.get_selected_cell();
767 767 if (cell.is_splittable()) {
768 768 texta = cell.get_pre_cursor();
769 769 textb = cell.get_post_cursor();
770 770 if (cell instanceof IPython.CodeCell) {
771 771 cell.set_text(texta);
772 772 var new_cell = this.insert_cell_below('code');
773 773 new_cell.set_text(textb);
774 774 } else if (cell instanceof IPython.MarkdownCell) {
775 775 cell.set_text(texta);
776 776 cell.render();
777 777 var new_cell = this.insert_cell_below('markdown');
778 778 new_cell.edit(); // editor must be visible to call set_text
779 779 new_cell.set_text(textb);
780 780 new_cell.render();
781 781 } else if (cell instanceof IPython.HTMLCell) {
782 782 cell.set_text(texta);
783 783 cell.render();
784 784 var new_cell = this.insert_cell_below('html');
785 785 new_cell.edit(); // editor must be visible to call set_text
786 786 new_cell.set_text(textb);
787 787 new_cell.render();
788 788 };
789 789 };
790 790 };
791 791
792 792
793 793 Notebook.prototype.merge_cell_above = function () {
794 794 var index = this.get_selected_index();
795 795 var cell = this.get_cell(index);
796 796 if (index > 0) {
797 797 upper_cell = this.get_cell(index-1);
798 798 upper_text = upper_cell.get_text();
799 799 text = cell.get_text();
800 800 if (cell instanceof IPython.CodeCell) {
801 801 cell.set_text(upper_text+'\n'+text);
802 802 } else if (cell instanceof IPython.MarkdownCell || cell instanceof IPython.HTMLCell) {
803 803 cell.edit();
804 804 cell.set_text(upper_text+'\n'+text);
805 805 cell.render();
806 806 };
807 807 this.delete_cell(index-1);
808 808 this.select(this.find_cell_index(cell));
809 809 };
810 810 };
811 811
812 812
813 813 Notebook.prototype.merge_cell_below = function () {
814 814 var index = this.get_selected_index();
815 815 var cell = this.get_cell(index);
816 816 if (index < this.ncells()-1) {
817 817 lower_cell = this.get_cell(index+1);
818 818 lower_text = lower_cell.get_text();
819 819 text = cell.get_text();
820 820 if (cell instanceof IPython.CodeCell) {
821 821 cell.set_text(text+'\n'+lower_text);
822 822 } else if (cell instanceof IPython.MarkdownCell || cell instanceof IPython.HTMLCell) {
823 823 cell.edit();
824 824 cell.set_text(text+'\n'+lower_text);
825 825 cell.render();
826 826 };
827 827 this.delete_cell(index+1);
828 828 this.select(this.find_cell_index(cell));
829 829 };
830 830 };
831 831
832 832
833 833 // Cell collapsing and output clearing
834 834
835 835 Notebook.prototype.collapse = function (index) {
836 836 var i = this.index_or_selected(index);
837 837 this.get_cell(i).collapse();
838 838 this.dirty = true;
839 839 };
840 840
841 841
842 842 Notebook.prototype.expand = function (index) {
843 843 var i = this.index_or_selected(index);
844 844 this.get_cell(i).expand();
845 845 this.dirty = true;
846 846 };
847 847
848 848
849 849 Notebook.prototype.toggle_output = function (index) {
850 850 var i = this.index_or_selected(index);
851 851 this.get_cell(i).toggle_output();
852 852 this.dirty = true;
853 853 };
854 854
855 855
856 856 Notebook.prototype.set_timebeforetooltip = function (time) {
857 857 this.time_before_tooltip = time;
858 858 };
859 859
860 860
861 861 Notebook.prototype.set_tooltipontab = function (state) {
862 862 this.tooltip_on_tab = state;
863 863 };
864 864
865 865
866 866 Notebook.prototype.set_smartcompleter = function (state) {
867 867 this.smart_completer = state;
868 868 };
869 869
870 870
871 871 Notebook.prototype.clear_all_output = function () {
872 872 var ncells = this.ncells();
873 873 var cells = this.get_cells();
874 874 for (var i=0; i<ncells; i++) {
875 875 if (cells[i] instanceof IPython.CodeCell) {
876 876 cells[i].clear_output(true,true,true);
877 877 }
878 878 };
879 879 this.dirty = true;
880 880 };
881 881
882 882
883 883 // Other cell functions: line numbers, ...
884 884
885 885 Notebook.prototype.cell_toggle_line_numbers = function() {
886 886 this.get_selected_cell().toggle_line_numbers();
887 887 };
888 888
889 889 // Kernel related things
890 890
891 891 Notebook.prototype.start_kernel = function () {
892 892 this.kernel = new IPython.Kernel();
893 893 this.kernel.start(this.notebook_id, $.proxy(this.kernel_started, this));
894 894 };
895 895
896 896
897 897 Notebook.prototype.restart_kernel = function () {
898 898 var that = this;
899 899 var dialog = $('<div/>');
900 900 dialog.html('Do you want to restart the current kernel? You will lose all variables defined in it.');
901 901 $(document).append(dialog);
902 902 dialog.dialog({
903 903 resizable: false,
904 904 modal: true,
905 905 title: "Restart kernel or continue running?",
906 906 closeText: '',
907 907 buttons : {
908 908 "Restart": function () {
909 909 that.kernel.restart($.proxy(that.kernel_started, that));
910 910 $(this).dialog('close');
911 911 },
912 912 "Continue running": function () {
913 913 $(this).dialog('close');
914 914 }
915 915 }
916 916 });
917 917 };
918 918
919 919
920 920 Notebook.prototype.kernel_started = function () {
921 921 console.log("Kernel started: ", this.kernel.kernel_id);
922 922 this.kernel.shell_channel.onmessage = $.proxy(this.handle_shell_reply,this);
923 923 this.kernel.iopub_channel.onmessage = $.proxy(this.handle_iopub_reply,this);
924 924 };
925 925
926 926
927 927 Notebook.prototype.handle_shell_reply = function (e) {
928 928 reply = $.parseJSON(e.data);
929 929 var header = reply.header;
930 930 var content = reply.content;
931 931 var msg_type = header.msg_type;
932 932 // console.log(reply);
933 933 var cell = this.cell_for_msg(reply.parent_header.msg_id);
934 934 if (msg_type === "execute_reply") {
935 935 cell.set_input_prompt(content.execution_count);
936 936 cell.element.removeClass("running");
937 937 this.dirty = true;
938 938 } else if (msg_type === "complete_reply") {
939 939 cell.finish_completing(content.matched_text, content.matches);
940 940 } else if (msg_type === "object_info_reply"){
941 941 //console.log('back from object_info_request : ')
942 942 rep = reply.content;
943 943 if(rep.found)
944 944 {
945 945 cell.finish_tooltip(rep);
946 946 }
947 947 } else {
948 948 //console.log("unknown reply:"+msg_type);
949 949 }
950 950 // when having a rely from object_info_reply,
951 951 // no payload so no nned to handle it
952 952 if(typeof(content.payload)!='undefined') {
953 953 var payload = content.payload || [];
954 954 this.handle_payload(cell, payload);
955 955 }
956 956 };
957 957
958 958
959 959 Notebook.prototype.handle_payload = function (cell, payload) {
960 960 var l = payload.length;
961 961 for (var i=0; i<l; i++) {
962 962 if (payload[i].source === 'IPython.zmq.page.page') {
963 963 if (payload[i].text.trim() !== '') {
964 964 IPython.pager.clear();
965 965 IPython.pager.expand();
966 966 IPython.pager.append_text(payload[i].text);
967 967 }
968 968 } else if (payload[i].source === 'IPython.zmq.zmqshell.ZMQInteractiveShell.set_next_input') {
969 969 var index = this.find_cell_index(cell);
970 970 var new_cell = this.insert_cell_below('code',index);
971 971 new_cell.set_text(payload[i].text);
972 972 this.dirty = true;
973 973 }
974 974 };
975 975 };
976 976
977 977
978 978 Notebook.prototype.handle_iopub_reply = function (e) {
979 979 reply = $.parseJSON(e.data);
980 980 var content = reply.content;
981 981 // console.log(reply);
982 982 var msg_type = reply.header.msg_type;
983 983 var cell = this.cell_for_msg(reply.parent_header.msg_id);
984 984 if (msg_type !== 'status' && !cell){
985 985 // message not from this notebook, but should be attached to a cell
986 986 // console.log("Received IOPub message not caused by one of my cells");
987 987 // console.log(reply);
988 988 return;
989 989 }
990 990 var output_types = ['stream','display_data','pyout','pyerr'];
991 991 if (output_types.indexOf(msg_type) >= 0) {
992 992 this.handle_output(cell, msg_type, content);
993 993 } else if (msg_type === 'status') {
994 994 if (content.execution_state === 'busy') {
995 995 $([IPython.events]).trigger('status_busy.Kernel');
996 996 } else if (content.execution_state === 'idle') {
997 997 $([IPython.events]).trigger('status_idle.Kernel');
998 998 } else if (content.execution_state === 'dead') {
999 999 this.handle_status_dead();
1000 1000 };
1001 1001 } else if (msg_type === 'clear_output') {
1002 1002 cell.clear_output(content.stdout, content.stderr, content.other);
1003 1003 };
1004 1004 };
1005 1005
1006 1006
1007 1007 Notebook.prototype.handle_status_dead = function () {
1008 1008 var that = this;
1009 1009 this.kernel.stop_channels();
1010 1010 var dialog = $('<div/>');
1011 1011 dialog.html('The kernel has died, would you like to restart it? If you do not restart the kernel, you will be able to save the notebook, but running code will not work until the notebook is reopened.');
1012 1012 $(document).append(dialog);
1013 1013 dialog.dialog({
1014 1014 resizable: false,
1015 1015 modal: true,
1016 1016 title: "Dead kernel",
1017 1017 buttons : {
1018 1018 "Restart": function () {
1019 1019 that.start_kernel();
1020 1020 $(this).dialog('close');
1021 1021 },
1022 1022 "Continue running": function () {
1023 1023 $(this).dialog('close');
1024 1024 }
1025 1025 }
1026 1026 });
1027 1027 };
1028 1028
1029 1029
1030 1030 Notebook.prototype.handle_output = function (cell, msg_type, content) {
1031 1031 var json = {};
1032 1032 json.output_type = msg_type;
1033 1033 if (msg_type === "stream") {
1034 1034 json.text = content.data;
1035 1035 json.stream = content.name;
1036 1036 } else if (msg_type === "display_data") {
1037 1037 json = this.convert_mime_types(json, content.data);
1038 1038 } else if (msg_type === "pyout") {
1039 1039 json.prompt_number = content.execution_count;
1040 1040 json = this.convert_mime_types(json, content.data);
1041 1041 } else if (msg_type === "pyerr") {
1042 1042 json.ename = content.ename;
1043 1043 json.evalue = content.evalue;
1044 1044 json.traceback = content.traceback;
1045 1045 };
1046 1046 // append with dynamic=true
1047 1047 cell.append_output(json, true);
1048 1048 this.dirty = true;
1049 1049 };
1050 1050
1051 1051
1052 1052 Notebook.prototype.convert_mime_types = function (json, data) {
1053 1053 if (data['text/plain'] !== undefined) {
1054 1054 json.text = data['text/plain'];
1055 1055 };
1056 1056 if (data['text/html'] !== undefined) {
1057 1057 json.html = data['text/html'];
1058 1058 };
1059 1059 if (data['image/svg+xml'] !== undefined) {
1060 1060 json.svg = data['image/svg+xml'];
1061 1061 };
1062 1062 if (data['image/png'] !== undefined) {
1063 1063 json.png = data['image/png'];
1064 1064 };
1065 1065 if (data['image/jpeg'] !== undefined) {
1066 1066 json.jpeg = data['image/jpeg'];
1067 1067 };
1068 1068 if (data['text/latex'] !== undefined) {
1069 1069 json.latex = data['text/latex'];
1070 1070 };
1071 1071 if (data['application/json'] !== undefined) {
1072 1072 json.json = data['application/json'];
1073 1073 };
1074 1074 if (data['application/javascript'] !== undefined) {
1075 1075 json.javascript = data['application/javascript'];
1076 1076 }
1077 1077 return json;
1078 1078 };
1079 1079
1080 1080
1081 1081 Notebook.prototype.execute_selected_cell = function (options) {
1082 1082 // add_new: should a new cell be added if we are at the end of the nb
1083 1083 // terminal: execute in terminal mode, which stays in the current cell
1084 1084 default_options = {terminal: false, add_new: true};
1085 1085 $.extend(default_options, options);
1086 1086 var that = this;
1087 1087 var cell = that.get_selected_cell();
1088 1088 var cell_index = that.find_cell_index(cell);
1089 1089 if (cell instanceof IPython.CodeCell) {
1090 1090 cell.clear_output(true, true, true);
1091 1091 cell.set_input_prompt('*');
1092 1092 cell.element.addClass("running");
1093 1093 var code = cell.get_text();
1094 1094 var msg_id = that.kernel.execute(cell.get_text());
1095 1095 that.msg_cell_map[msg_id] = cell.cell_id;
1096 1096 } else if (cell instanceof IPython.HTMLCell) {
1097 1097 cell.render();
1098 1098 }
1099 1099 if (default_options.terminal) {
1100 1100 cell.select_all();
1101 1101 } else {
1102 1102 if ((cell_index === (that.ncells()-1)) && default_options.add_new) {
1103 1103 that.insert_cell_below('code');
1104 1104 // If we are adding a new cell at the end, scroll down to show it.
1105 1105 that.scroll_to_bottom();
1106 1106 } else {
1107 1107 that.select(cell_index+1);
1108 1108 };
1109 1109 };
1110 1110 this.dirty = true;
1111 1111 };
1112 1112
1113 1113
1114 1114 Notebook.prototype.execute_all_cells = function () {
1115 1115 var ncells = this.ncells();
1116 1116 for (var i=0; i<ncells; i++) {
1117 1117 this.select(i);
1118 1118 this.execute_selected_cell({add_new:false});
1119 1119 };
1120 1120 this.scroll_to_bottom();
1121 1121 };
1122 1122
1123 1123
1124 1124 Notebook.prototype.request_tool_tip = function (cell,func) {
1125 1125 // Feel free to shorten this logic if you are better
1126 1126 // than me in regEx
1127 1127 // basicaly you shoul be able to get xxx.xxx.xxx from
1128 1128 // something(range(10), kwarg=smth) ; xxx.xxx.xxx( firstarg, rand(234,23), kwarg1=2,
1129 1129 // remove everything between matchin bracket (need to iterate)
1130 1130 matchBracket = /\([^\(\)]+\)/g;
1131 1131 oldfunc = func;
1132 1132 func = func.replace(matchBracket,"");
1133 1133 while( oldfunc != func )
1134 1134 {
1135 1135 oldfunc = func;
1136 1136 func = func.replace(matchBracket,"");
1137 1137 }
1138 1138 // remove everythin after last open bracket
1139 1139 endBracket = /\([^\(]*$/g;
1140 1140 func = func.replace(endBracket,"");
1141 1141 var re = /[a-zA-Z._]+$/g;
1142 1142 var msg_id = this.kernel.object_info_request(re.exec(func));
1143 1143 if(typeof(msg_id)!='undefined'){
1144 1144 this.msg_cell_map[msg_id] = cell.cell_id;
1145 1145 }
1146 1146 };
1147 1147
1148 1148 Notebook.prototype.complete_cell = function (cell, line, cursor_pos) {
1149 1149 var msg_id = this.kernel.complete(line, cursor_pos);
1150 1150 this.msg_cell_map[msg_id] = cell.cell_id;
1151 1151 };
1152 1152
1153 1153
1154 1154 // Persistance and loading
1155 1155
1156 1156 Notebook.prototype.get_notebook_id = function () {
1157 1157 return this.notebook_id;
1158 1158 };
1159 1159
1160 1160
1161 1161 Notebook.prototype.get_notebook_name = function () {
1162 1162 return this.notebook_name;
1163 1163 };
1164 1164
1165 1165
1166 1166 Notebook.prototype.set_notebook_name = function (name) {
1167 1167 this.notebook_name = name;
1168 1168 };
1169 1169
1170 1170
1171 1171 Notebook.prototype.test_notebook_name = function (nbname) {
1172 1172 nbname = nbname || '';
1173 1173 if (this.notebook_name_blacklist_re.test(nbname) == false && nbname.length>0) {
1174 1174 return true;
1175 1175 } else {
1176 1176 return false;
1177 1177 };
1178 1178 };
1179 1179
1180 1180
1181 1181 Notebook.prototype.fromJSON = function (data) {
1182 1182 var ncells = this.ncells();
1183 1183 var i;
1184 1184 for (i=0; i<ncells; i++) {
1185 1185 // Always delete cell 0 as they get renumbered as they are deleted.
1186 1186 this.delete_cell(0);
1187 1187 };
1188 1188 // Save the metadata and name.
1189 1189 this.metadata = data.metadata;
1190 1190 this.notebook_name = data.metadata.name;
1191 1191 // Only handle 1 worksheet for now.
1192 1192 var worksheet = data.worksheets[0];
1193 1193 if (worksheet !== undefined) {
1194 1194 var new_cells = worksheet.cells;
1195 1195 ncells = new_cells.length;
1196 1196 var cell_data = null;
1197 1197 var new_cell = null;
1198 1198 for (i=0; i<ncells; i++) {
1199 1199 cell_data = new_cells[i];
1200 // handle short-lived plaintext name for raw cells
1201 if (cell_data.cell_type === 'plaintext'){
1202 cell_data.cell_type = 'raw';
1203 }
1204
1200 1205 new_cell = this.insert_cell_below(cell_data.cell_type);
1201 1206 new_cell.fromJSON(cell_data);
1202 1207 };
1203 1208 };
1204 1209 };
1205 1210
1206 1211
1207 1212 Notebook.prototype.toJSON = function () {
1208 1213 var cells = this.get_cells();
1209 1214 var ncells = cells.length;
1210 1215 cell_array = new Array(ncells);
1211 1216 for (var i=0; i<ncells; i++) {
1212 1217 cell_array[i] = cells[i].toJSON();
1213 1218 };
1214 1219 data = {
1215 1220 // Only handle 1 worksheet for now.
1216 1221 worksheets : [{cells:cell_array}],
1217 1222 metadata : this.metadata
1218 1223 };
1219 1224 return data;
1220 1225 };
1221 1226
1222 1227 Notebook.prototype.save_notebook = function () {
1223 1228 // We may want to move the name/id/nbformat logic inside toJSON?
1224 1229 var data = this.toJSON();
1225 1230 data.metadata.name = this.notebook_name;
1226 1231 data.nbformat = this.nbformat;
1227 1232 // We do the call with settings so we can set cache to false.
1228 1233 var settings = {
1229 1234 processData : false,
1230 1235 cache : false,
1231 1236 type : "PUT",
1232 1237 data : JSON.stringify(data),
1233 1238 headers : {'Content-Type': 'application/json'},
1234 1239 success : $.proxy(this.save_notebook_success,this),
1235 1240 error : $.proxy(this.save_notebook_error,this)
1236 1241 };
1237 1242 $([IPython.events]).trigger('notebook_saving.Notebook');
1238 1243 var url = $('body').data('baseProjectUrl') + 'notebooks/' + this.notebook_id;
1239 1244 $.ajax(url, settings);
1240 1245 };
1241 1246
1242 1247
1243 1248 Notebook.prototype.save_notebook_success = function (data, status, xhr) {
1244 1249 this.dirty = false;
1245 1250 $([IPython.events]).trigger('notebook_saved.Notebook');
1246 1251 };
1247 1252
1248 1253
1249 1254 Notebook.prototype.save_notebook_error = function (xhr, status, error_msg) {
1250 1255 $([IPython.events]).trigger('notebook_save_failed.Notebook');
1251 1256 };
1252 1257
1253 1258
1254 1259 Notebook.prototype.load_notebook = function (notebook_id) {
1255 1260 var that = this;
1256 1261 this.notebook_id = notebook_id;
1257 1262 // We do the call with settings so we can set cache to false.
1258 1263 var settings = {
1259 1264 processData : false,
1260 1265 cache : false,
1261 1266 type : "GET",
1262 1267 dataType : "json",
1263 1268 success : $.proxy(this.load_notebook_success,this),
1264 1269 error : $.proxy(this.load_notebook_error,this),
1265 1270 };
1266 1271 $([IPython.events]).trigger('notebook_loading.Notebook');
1267 1272 var url = $('body').data('baseProjectUrl') + 'notebooks/' + this.notebook_id;
1268 1273 $.ajax(url, settings);
1269 1274 };
1270 1275
1271 1276
1272 1277 Notebook.prototype.load_notebook_success = function (data, status, xhr) {
1273 1278 this.fromJSON(data);
1274 1279 if (this.ncells() === 0) {
1275 1280 this.insert_cell_below('code');
1276 1281 };
1277 1282 this.dirty = false;
1278 1283 if (! this.read_only) {
1279 1284 this.start_kernel();
1280 1285 }
1281 1286 this.select(0);
1282 1287 this.scroll_to_top();
1283 1288 if (data.orig_nbformat !== undefined && data.nbformat !== data.orig_nbformat) {
1284 1289 msg = "This notebook has been converted from an older " +
1285 1290 "notebook format (v"+data.orig_nbformat+") to the current notebook " +
1286 1291 "format (v"+data.nbformat+"). The next time you save this notebook, the " +
1287 1292 "newer notebook format will be used and older verions of IPython " +
1288 1293 "may not be able to read it. To keep the older version, close the " +
1289 1294 "notebook without saving it.";
1290 1295 var dialog = $('<div/>');
1291 1296 dialog.html(msg);
1292 1297 this.element.append(dialog);
1293 1298 dialog.dialog({
1294 1299 resizable: false,
1295 1300 modal: true,
1296 1301 title: "Notebook converted",
1297 1302 closeText: "",
1298 1303 close: function(event, ui) {$(this).dialog('destroy').remove();},
1299 1304 buttons : {
1300 1305 "OK": function () {
1301 1306 $(this).dialog('close');
1302 1307 }
1303 1308 },
1304 1309 width: 400
1305 1310 });
1306 1311 }
1307 1312 $([IPython.events]).trigger('notebook_loaded.Notebook');
1308 1313 };
1309 1314
1310 1315
1311 1316 Notebook.prototype.load_notebook_error = function (xhr, textStatus, errorThrow) {
1312 1317 if (xhr.status === 500) {
1313 1318 msg = "An error occurred while loading this notebook. Most likely " +
1314 1319 "this notebook is in a newer format than is supported by this " +
1315 1320 "version of IPython. This version can load notebook formats " +
1316 1321 "v"+this.nbformat+" or earlier.";
1317 1322 var dialog = $('<div/>');
1318 1323 dialog.html(msg);
1319 1324 this.element.append(dialog);
1320 1325 dialog.dialog({
1321 1326 resizable: false,
1322 1327 modal: true,
1323 1328 title: "Error loading notebook",
1324 1329 closeText: "",
1325 1330 close: function(event, ui) {$(this).dialog('destroy').remove();},
1326 1331 buttons : {
1327 1332 "OK": function () {
1328 1333 $(this).dialog('close');
1329 1334 }
1330 1335 },
1331 1336 width: 400
1332 1337 });
1333 1338 }
1334 1339 }
1335 1340
1336 1341 IPython.Notebook = Notebook;
1337 1342
1338 1343
1339 1344 return IPython;
1340 1345
1341 1346 }(IPython));
1342 1347
@@ -1,194 +1,197 b''
1 1 """The basic dict based notebook format.
2 2
3 3 The Python representation of a notebook is a nested structure of
4 4 dictionary subclasses that support attribute access
5 5 (IPython.utils.ipstruct.Struct). The functions in this module are merely
6 6 helpers to build the structs in the right form.
7 7
8 8 Authors:
9 9
10 10 * Brian Granger
11 11 """
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Copyright (C) 2008-2011 The IPython Development Team
15 15 #
16 16 # Distributed under the terms of the BSD License. The full license is in
17 17 # the file COPYING, distributed as part of this software.
18 18 #-----------------------------------------------------------------------------
19 19
20 20 #-----------------------------------------------------------------------------
21 21 # Imports
22 22 #-----------------------------------------------------------------------------
23 23
24 24 import pprint
25 25 import uuid
26 26
27 27 from IPython.utils.ipstruct import Struct
28 28
29 29 #-----------------------------------------------------------------------------
30 30 # Code
31 31 #-----------------------------------------------------------------------------
32 32
33 33 # Change this when incrementing the nbformat version
34 34 nbformat = 3
35 35
36 36 class NotebookNode(Struct):
37 37 pass
38 38
39 39
40 40 def from_dict(d):
41 41 if isinstance(d, dict):
42 42 newd = NotebookNode()
43 43 for k,v in d.items():
44 44 newd[k] = from_dict(v)
45 45 return newd
46 46 elif isinstance(d, (tuple, list)):
47 47 return [from_dict(i) for i in d]
48 48 else:
49 49 return d
50 50
51 51
52 52 def new_output(output_type=None, output_text=None, output_png=None,
53 53 output_html=None, output_svg=None, output_latex=None, output_json=None,
54 54 output_javascript=None, output_jpeg=None, prompt_number=None,
55 55 etype=None, evalue=None, traceback=None):
56 56 """Create a new code cell with input and output"""
57 57 output = NotebookNode()
58 58 if output_type is not None:
59 59 output.output_type = unicode(output_type)
60 60
61 61 if output_type != 'pyerr':
62 62 if output_text is not None:
63 63 output.text = unicode(output_text)
64 64 if output_png is not None:
65 65 output.png = bytes(output_png)
66 66 if output_jpeg is not None:
67 67 output.jpeg = bytes(output_jpeg)
68 68 if output_html is not None:
69 69 output.html = unicode(output_html)
70 70 if output_svg is not None:
71 71 output.svg = unicode(output_svg)
72 72 if output_latex is not None:
73 73 output.latex = unicode(output_latex)
74 74 if output_json is not None:
75 75 output.json = unicode(output_json)
76 76 if output_javascript is not None:
77 77 output.javascript = unicode(output_javascript)
78 78
79 79 if output_type == u'pyout':
80 80 if prompt_number is not None:
81 81 output.prompt_number = int(prompt_number)
82 82
83 83 if output_type == u'pyerr':
84 84 if etype is not None:
85 85 output.etype = unicode(etype)
86 86 if evalue is not None:
87 87 output.evalue = unicode(evalue)
88 88 if traceback is not None:
89 89 output.traceback = [unicode(frame) for frame in list(traceback)]
90 90
91 91 return output
92 92
93 93
94 94 def new_code_cell(input=None, prompt_number=None, outputs=None,
95 95 language=u'python', collapsed=False):
96 96 """Create a new code cell with input and output"""
97 97 cell = NotebookNode()
98 98 cell.cell_type = u'code'
99 99 if language is not None:
100 100 cell.language = unicode(language)
101 101 if input is not None:
102 102 cell.input = unicode(input)
103 103 if prompt_number is not None:
104 104 cell.prompt_number = int(prompt_number)
105 105 if outputs is None:
106 106 cell.outputs = []
107 107 else:
108 108 cell.outputs = outputs
109 109 if collapsed is not None:
110 110 cell.collapsed = bool(collapsed)
111 111
112 112 return cell
113 113
114 114 def new_text_cell(cell_type, source=None, rendered=None):
115 115 """Create a new text cell."""
116 116 cell = NotebookNode()
117 # handle short-lived plaintext name for raw cells
118 if cell_type == 'plaintext':
119 cell_type = 'raw'
117 120 if source is not None:
118 121 cell.source = unicode(source)
119 122 if rendered is not None:
120 123 cell.rendered = unicode(rendered)
121 124 cell.cell_type = cell_type
122 125 return cell
123 126
124 127
125 128 def new_heading_cell(source=None, rendered=None, level=1):
126 129 """Create a new section cell with a given integer level."""
127 130 cell = NotebookNode()
128 131 cell.cell_type = u'heading'
129 132 if source is not None:
130 133 cell.source = unicode(source)
131 134 if rendered is not None:
132 135 cell.rendered = unicode(rendered)
133 136 cell.level = int(level)
134 137 return cell
135 138
136 139
137 140 def new_worksheet(name=None, cells=None):
138 141 """Create a worksheet by name with with a list of cells."""
139 142 ws = NotebookNode()
140 143 if name is not None:
141 144 ws.name = unicode(name)
142 145 if cells is None:
143 146 ws.cells = []
144 147 else:
145 148 ws.cells = list(cells)
146 149 return ws
147 150
148 151
149 152 def new_notebook(metadata=None, worksheets=None):
150 153 """Create a notebook by name, id and a list of worksheets."""
151 154 nb = NotebookNode()
152 155 nb.nbformat = nbformat
153 156 if worksheets is None:
154 157 nb.worksheets = []
155 158 else:
156 159 nb.worksheets = list(worksheets)
157 160 if metadata is None:
158 161 nb.metadata = new_metadata()
159 162 else:
160 163 nb.metadata = NotebookNode(metadata)
161 164 return nb
162 165
163 166
164 167 def new_metadata(name=None, authors=None, license=None, created=None,
165 168 modified=None, gistid=None):
166 169 """Create a new metadata node."""
167 170 metadata = NotebookNode()
168 171 if name is not None:
169 172 metadata.name = unicode(name)
170 173 if authors is not None:
171 174 metadata.authors = list(authors)
172 175 if created is not None:
173 176 metadata.created = unicode(created)
174 177 if modified is not None:
175 178 metadata.modified = unicode(modified)
176 179 if license is not None:
177 180 metadata.license = unicode(license)
178 181 if gistid is not None:
179 182 metadata.gistid = unicode(gistid)
180 183 return metadata
181 184
182 185 def new_author(name=None, email=None, affiliation=None, url=None):
183 186 """Create a new author."""
184 187 author = NotebookNode()
185 188 if name is not None:
186 189 author.name = unicode(name)
187 190 if email is not None:
188 191 author.email = unicode(email)
189 192 if affiliation is not None:
190 193 author.affiliation = unicode(affiliation)
191 194 if url is not None:
192 195 author.url = unicode(url)
193 196 return author
194 197
@@ -1,200 +1,200 b''
1 1 """Read and write notebooks as regular .py files.
2 2
3 3 Authors:
4 4
5 5 * Brian Granger
6 6 """
7 7
8 8 #-----------------------------------------------------------------------------
9 9 # Copyright (C) 2008-2011 The IPython Development Team
10 10 #
11 11 # Distributed under the terms of the BSD License. The full license is in
12 12 # the file COPYING, distributed as part of this software.
13 13 #-----------------------------------------------------------------------------
14 14
15 15 #-----------------------------------------------------------------------------
16 16 # Imports
17 17 #-----------------------------------------------------------------------------
18 18
19 19 import re
20 20 from .rwbase import NotebookReader, NotebookWriter
21 21 from .nbbase import (
22 22 new_code_cell, new_text_cell, new_worksheet,
23 23 new_notebook, new_heading_cell, nbformat
24 24 )
25 25
26 26 #-----------------------------------------------------------------------------
27 27 # Code
28 28 #-----------------------------------------------------------------------------
29 29
30 30 _encoding_declaration_re = re.compile(r"^#.*coding[:=]\s*([-\w.]+)")
31 31
32 32 class PyReaderError(Exception):
33 33 pass
34 34
35 35
36 36 class PyReader(NotebookReader):
37 37
38 38 def reads(self, s, **kwargs):
39 39 return self.to_notebook(s,**kwargs)
40 40
41 41 def to_notebook(self, s, **kwargs):
42 42 lines = s.splitlines()
43 43 cells = []
44 44 cell_lines = []
45 45 kwargs = {}
46 46 state = u'codecell'
47 47 for line in lines:
48 48 if line.startswith(u'# <nbformat>') or _encoding_declaration_re.match(line):
49 49 pass
50 50 elif line.startswith(u'# <codecell>'):
51 51 cell = self.new_cell(state, cell_lines, **kwargs)
52 52 if cell is not None:
53 53 cells.append(cell)
54 54 state = u'codecell'
55 55 cell_lines = []
56 56 kwargs = {}
57 57 elif line.startswith(u'# <htmlcell>'):
58 58 cell = self.new_cell(state, cell_lines, **kwargs)
59 59 if cell is not None:
60 60 cells.append(cell)
61 61 state = u'htmlcell'
62 62 cell_lines = []
63 63 kwargs = {}
64 64 elif line.startswith(u'# <markdowncell>'):
65 65 cell = self.new_cell(state, cell_lines, **kwargs)
66 66 if cell is not None:
67 67 cells.append(cell)
68 68 state = u'markdowncell'
69 69 cell_lines = []
70 70 kwargs = {}
71 elif line.startswith(u'# <rawcell>'):
71 elif line.startswith(u'# <rawcell>') or line.startswith(u'# <plaintextcell>'):
72 72 cell = self.new_cell(state, cell_lines, **kwargs)
73 73 if cell is not None:
74 74 cells.append(cell)
75 75 state = u'rawcell'
76 76 cell_lines = []
77 77 kwargs = {}
78 78 elif line.startswith(u'# <headingcell'):
79 79 cell = self.new_cell(state, cell_lines, **kwargs)
80 80 if cell is not None:
81 81 cells.append(cell)
82 82 cell_lines = []
83 83 m = re.match(r'# <headingcell level=(?P<level>\d)>',line)
84 84 if m is not None:
85 85 state = u'headingcell'
86 86 kwargs = {}
87 87 kwargs['level'] = int(m.group('level'))
88 88 else:
89 89 state = u'codecell'
90 90 kwargs = {}
91 91 cell_lines = []
92 92 else:
93 93 cell_lines.append(line)
94 94 if cell_lines and state == u'codecell':
95 95 cell = self.new_cell(state, cell_lines)
96 96 if cell is not None:
97 97 cells.append(cell)
98 98 ws = new_worksheet(cells=cells)
99 99 nb = new_notebook(worksheets=[ws])
100 100 return nb
101 101
102 102 def new_cell(self, state, lines, **kwargs):
103 103 if state == u'codecell':
104 104 input = u'\n'.join(lines)
105 105 input = input.strip(u'\n')
106 106 if input:
107 107 return new_code_cell(input=input)
108 108 elif state == u'htmlcell':
109 109 text = self._remove_comments(lines)
110 110 if text:
111 111 return new_text_cell(u'html',source=text)
112 112 elif state == u'markdowncell':
113 113 text = self._remove_comments(lines)
114 114 if text:
115 115 return new_text_cell(u'markdown',source=text)
116 116 elif state == u'rawcell':
117 117 text = self._remove_comments(lines)
118 118 if text:
119 119 return new_text_cell(u'raw',source=text)
120 120 elif state == u'headingcell':
121 121 text = self._remove_comments(lines)
122 122 level = kwargs.get('level',1)
123 123 if text:
124 124 return new_heading_cell(source=text,level=level)
125 125
126 126 def _remove_comments(self, lines):
127 127 new_lines = []
128 128 for line in lines:
129 129 if line.startswith(u'#'):
130 130 new_lines.append(line[2:])
131 131 else:
132 132 new_lines.append(line)
133 133 text = u'\n'.join(new_lines)
134 134 text = text.strip(u'\n')
135 135 return text
136 136
137 137 def split_lines_into_blocks(self, lines):
138 138 if len(lines) == 1:
139 139 yield lines[0]
140 140 raise StopIteration()
141 141 import ast
142 142 source = '\n'.join(lines)
143 143 code = ast.parse(source)
144 144 starts = [x.lineno-1 for x in code.body]
145 145 for i in range(len(starts)-1):
146 146 yield '\n'.join(lines[starts[i]:starts[i+1]]).strip('\n')
147 147 yield '\n'.join(lines[starts[-1]:]).strip('\n')
148 148
149 149
150 150 class PyWriter(NotebookWriter):
151 151
152 152 def writes(self, nb, **kwargs):
153 153 lines = [u'# -*- coding: utf-8 -*-']
154 154 lines.extend([u'# <nbformat>%i</nbformat>' % nbformat,''])
155 155 for ws in nb.worksheets:
156 156 for cell in ws.cells:
157 157 if cell.cell_type == u'code':
158 158 input = cell.get(u'input')
159 159 if input is not None:
160 160 lines.extend([u'# <codecell>',u''])
161 161 lines.extend(input.splitlines())
162 162 lines.append(u'')
163 163 elif cell.cell_type == u'html':
164 164 input = cell.get(u'source')
165 165 if input is not None:
166 166 lines.extend([u'# <htmlcell>',u''])
167 167 lines.extend([u'# ' + line for line in input.splitlines()])
168 168 lines.append(u'')
169 169 elif cell.cell_type == u'markdown':
170 170 input = cell.get(u'source')
171 171 if input is not None:
172 172 lines.extend([u'# <markdowncell>',u''])
173 173 lines.extend([u'# ' + line for line in input.splitlines()])
174 174 lines.append(u'')
175 175 elif cell.cell_type == u'raw':
176 176 input = cell.get(u'source')
177 177 if input is not None:
178 178 lines.extend([u'# <rawcell>',u''])
179 179 lines.extend([u'# ' + line for line in input.splitlines()])
180 180 lines.append(u'')
181 181 elif cell.cell_type == u'heading':
182 182 input = cell.get(u'source')
183 183 level = cell.get(u'level',1)
184 184 if input is not None:
185 185 lines.extend([u'# <headingcell level=%s>' % level,u''])
186 186 lines.extend([u'# ' + line for line in input.splitlines()])
187 187 lines.append(u'')
188 188 lines.append('')
189 189 return unicode('\n'.join(lines))
190 190
191 191
192 192 _reader = PyReader()
193 193 _writer = PyWriter()
194 194
195 195 reads = _reader.reads
196 196 read = _reader.read
197 197 to_notebook = _reader.to_notebook
198 198 write = _writer.write
199 199 writes = _writer.writes
200 200
General Comments 0
You need to be logged in to leave comments. Login now