##// END OF EJS Templates
Minor fixes to fonts and spacing.
Brian Granger -
Show More
@@ -1,58 +1,60 b''
1 .CodeMirror {
1 .CodeMirror {
2 overflow: hidden; /* Changed from auto to remove scrollbar */
2 overflow: hidden; /* Changed from auto to remove scrollbar */
3 height: auto; /* Changed to auto to autogrow */
3 height: auto; /* Changed to auto to autogrow */
4 /* line-height: 1em;*/
4 line-height: 1em;
5 font-family: monospace;
5 font-family: monospace;
6 }
6 }
7
7
8 .CodeMirror-gutter {
8 .CodeMirror-gutter {
9 position: absolute; left: 0; top: 0;
9 position: absolute; left: 0; top: 0;
10 background-color: #f7f7f7;
10 background-color: #f7f7f7;
11 border-right: 1px solid #eee;
11 border-right: 1px solid #eee;
12 min-width: 2em;
12 min-width: 2em;
13 height: 100%;
13 height: 100%;
14 }
14 }
15 .CodeMirror-gutter-text {
15 .CodeMirror-gutter-text {
16 color: #aaa;
16 color: #aaa;
17 text-align: right;
17 text-align: right;
18 padding: .4em .2em .4em .4em;
18 padding: .4em .2em .4em .4em;
19 }
19 }
20 .CodeMirror-lines {
20 .CodeMirror-lines {
21 /* Changed from 0.4em, but this gives us jitters upon typing, but I found
21 /* Changed from 0.4em, but this gives us jitters upon typing, but I found
22 that removing .CodeMirror line-height: 1em above, it goes away. */
22 that removing .CodeMirror line-height: 1em above, it goes away.
23 padding: 0em;
23 For now I am using the default of 0.4em, but I have added a 0.4em
24 padding to prompts to match this.*/
25 padding: 0.4em;
24 }
26 }
25
27
26 .CodeMirror pre {
28 .CodeMirror pre {
27 -moz-border-radius: 0;
29 -moz-border-radius: 0;
28 -webkit-border-radius: 0;
30 -webkit-border-radius: 0;
29 -o-border-radius: 0;
31 -o-border-radius: 0;
30 border-radius: 0;
32 border-radius: 0;
31 border-width: 0; margin: 0; padding: 0; background: transparent;
33 border-width: 0; margin: 0; padding: 0; background: transparent;
32 font-family: inherit;
34 font-family: inherit;
33 }
35 }
34
36
35 .CodeMirror textarea {
37 .CodeMirror textarea {
36 font-family: monospace !important;
38 font-family: monospace !important;
37 }
39 }
38
40
39 .CodeMirror-cursor {
41 .CodeMirror-cursor {
40 z-index: 10;
42 z-index: 10;
41 position: absolute;
43 position: absolute;
42 visibility: hidden;
44 visibility: hidden;
43 border-left: 1px solid black !important;
45 border-left: 1px solid black !important;
44 }
46 }
45 .CodeMirror-focused .CodeMirror-cursor {
47 .CodeMirror-focused .CodeMirror-cursor {
46 visibility: visible;
48 visibility: visible;
47 }
49 }
48
50
49 span.CodeMirror-selected {
51 span.CodeMirror-selected {
50 background: #ccc !important;
52 background: #ccc !important;
51 color: HighlightText !important;
53 color: HighlightText !important;
52 }
54 }
53 .CodeMirror-focused span.CodeMirror-selected {
55 .CodeMirror-focused span.CodeMirror-selected {
54 background: Highlight !important;
56 background: Highlight !important;
55 }
57 }
56
58
57 .CodeMirror-matchingbracket {color: #0f0 !important;}
59 .CodeMirror-matchingbracket {color: #0f0 !important;}
58 .CodeMirror-nonmatchingbracket {color: #f22 !important;}
60 .CodeMirror-nonmatchingbracket {color: #f22 !important;}
@@ -1,267 +1,268 b''
1 /**
1 /**
2 * HTML5 ✰ Boilerplate
2 * HTML5 ✰ Boilerplate
3 *
3 *
4 * style.css contains a reset, font normalization and some base styles.
4 * style.css contains a reset, font normalization and some base styles.
5 *
5 *
6 * Credit is left where credit is due.
6 * Credit is left where credit is due.
7 * Much inspiration was taken from these projects:
7 * Much inspiration was taken from these projects:
8 * - yui.yahooapis.com/2.8.1/build/base/base.css
8 * - yui.yahooapis.com/2.8.1/build/base/base.css
9 * - camendesign.com/design/
9 * - camendesign.com/design/
10 * - praegnanz.de/weblog/htmlcssjs-kickstart
10 * - praegnanz.de/weblog/htmlcssjs-kickstart
11 */
11 */
12
12
13
13
14 /**
14 /**
15 * html5doctor.com Reset Stylesheet (Eric Meyer's Reset Reloaded + HTML5 baseline)
15 * html5doctor.com Reset Stylesheet (Eric Meyer's Reset Reloaded + HTML5 baseline)
16 * v1.6.1 2010-09-17 | Authors: Eric Meyer & Richard Clark
16 * v1.6.1 2010-09-17 | Authors: Eric Meyer & Richard Clark
17 * html5doctor.com/html-5-reset-stylesheet/
17 * html5doctor.com/html-5-reset-stylesheet/
18 */
18 */
19
19
20 html, body, div, span, object, iframe,
20 html, body, div, span, object, iframe,
21 h1, h2, h3, h4, h5, h6, p, blockquote, pre,
21 h1, h2, h3, h4, h5, h6, p, blockquote, pre,
22 abbr, address, cite, code, del, dfn, em, img, ins, kbd, q, samp,
22 abbr, address, cite, code, del, dfn, em, img, ins, kbd, q, samp,
23 small, strong, sub, sup, var, b, i, dl, dt, dd, ol, ul, li,
23 small, strong, sub, sup, var, b, i, dl, dt, dd, ol, ul, li,
24 fieldset, form, label, legend,
24 fieldset, form, label, legend,
25 table, caption, tbody, tfoot, thead, tr, th, td,
25 table, caption, tbody, tfoot, thead, tr, th, td,
26 article, aside, canvas, details, figcaption, figure,
26 article, aside, canvas, details, figcaption, figure,
27 footer, header, hgroup, menu, nav, section, summary,
27 footer, header, hgroup, menu, nav, section, summary,
28 time, mark, audio, video {
28 time, mark, audio, video {
29 margin: 0;
29 margin: 0;
30 padding: 0;
30 padding: 0;
31 border: 0;
31 border: 0;
32 /* font-size: 100%;*/
32 /* font-size: 100%;*/
33 font: inherit;
33 font: inherit;
34 vertical-align: baseline;
34 vertical-align: baseline;
35 }
35 }
36
36
37 article, aside, details, figcaption, figure,
37 article, aside, details, figcaption, figure,
38 footer, header, hgroup, menu, nav, section {
38 footer, header, hgroup, menu, nav, section {
39 display: block;
39 display: block;
40 }
40 }
41
41
42 blockquote, q { quotes: none; }
42 blockquote, q { quotes: none; }
43
43
44 blockquote:before, blockquote:after,
44 blockquote:before, blockquote:after,
45 q:before, q:after { content: ""; content: none; }
45 q:before, q:after { content: ""; content: none; }
46
46
47 ins { background-color: #ff9; color: #000; text-decoration: none; }
47 ins { background-color: #ff9; color: #000; text-decoration: none; }
48
48
49 mark { background-color: #ff9; color: #000; font-style: italic; font-weight: bold; }
49 mark { background-color: #ff9; color: #000; font-style: italic; font-weight: bold; }
50
50
51 del { text-decoration: line-through; }
51 del { text-decoration: line-through; }
52
52
53 abbr[title], dfn[title] { border-bottom: 1px dotted; cursor: help; }
53 abbr[title], dfn[title] { border-bottom: 1px dotted; cursor: help; }
54
54
55 table { border-collapse: collapse; border-spacing: 0; }
55 table { border-collapse: collapse; border-spacing: 0; }
56
56
57 hr { display: block; height: 1px; border: 0; border-top: 1px solid #ccc; margin: 1em 0; padding: 0; }
57 hr { display: block; height: 1px; border: 0; border-top: 1px solid #ccc; margin: 1em 0; padding: 0; }
58
58
59 input, select { vertical-align: middle; }
59 input, select { vertical-align: middle; }
60
60
61 body {
61 body {
62 background-color: white;
62 background-color: white;
63 /* This won't propagate to all children so we also set it below */
63 /* This won't propagate to all children so we also set it below */
64 font-size: 10pt;
64 font-size: 11pt;
65 /* This makes sure that the body covers the entire window and needs to
65 /* This makes sure that the body covers the entire window and needs to
66 be in a different element than the display: box in wrapper below */
66 be in a different element than the display: box in wrapper below */
67 position: absolute;
67 position: absolute;
68 left: 0px;
68 left: 0px;
69 right: 0px;
69 right: 0px;
70 top: 0px;
70 top: 0px;
71 bottom: 0px;
71 bottom: 0px;
72 overflow: hidden;
72 overflow: hidden;
73 }
73 }
74
74
75
75
76 span#ipython_notebook h1 {
76 span#ipython_notebook h1 {
77 font-family: Verdana, "Helvetica Neue", Arial, Helvetica, Geneva, sans-serif;
77 font-family: Verdana, "Helvetica Neue", Arial, Helvetica, Geneva, sans-serif;
78 font-size: 22pt;
78 font-size: 22pt;
79 height: 35px;
79 height: 35px;
80 padding: 5px;
80 padding: 5px;
81 margin: 0px;
81 margin: 0px;
82
82
83 }
83 }
84
84
85 span#kernel_status {
85 span#kernel_status {
86 position: absolute;
86 position: absolute;
87 top: 12%;
87 top: 12%;
88 right: 10px;
88 right: 10px;
89 font-weight: bold;
89 font-weight: bold;
90 }
90 }
91
91
92 .status_idle {
92 .status_idle {
93 color: gray;
93 color: gray;
94 }
94 }
95
95
96 .status_busy {
96 .status_busy {
97 color: red;
97 color: red;
98 }
98 }
99
99
100 .status_restarting {
100 .status_restarting {
101 color: black;
101 color: black;
102 }
102 }
103
103
104 div#notebook_app {
104 div#notebook_app {
105 width: 100%;
105 width: 100%;
106 position: relative;
106 position: relative;
107 }
107 }
108
108
109 div#left_panel {
109 div#left_panel {
110 overflow-y: auto;
110 overflow-y: auto;
111 top: 0px;
111 top: 0px;
112 left: 0px;
112 left: 0px;
113 margin: 0px;
113 margin: 0px;
114 padding: 0px;
114 padding: 0px;
115 position: absolute;
115 position: absolute;
116 }
116 }
117
117
118 h3.section_header {
118 h3.section_header {
119 padding: 5px;
119 padding: 5px;
120 }
120 }
121
121
122 div.section_content {
122 div.section_content {
123 padding: 5px;
123 padding: 5px;
124 }
124 }
125
125
126
126
127 span.button_label {
127 span.button_label {
128 padding: 0.3em 1em;
128 padding: 0.3em 1em;
129 font-size: 0.8em;
129 font-size: 0.8em;
130 }
130 }
131
131
132 .ui-button .ui-button-text {
132 .ui-button .ui-button-text {
133 padding: 0.3em 0.9em;
133 padding: 0.3em 0.9em;
134 font-size: 0.7em;
134 font-size: 0.7em;
135 }
135 }
136
136
137 div#left_panel_splitter {
137 div#left_panel_splitter {
138 width: 8px;
138 width: 8px;
139 top: 0px;
139 top: 0px;
140 left: 202px;
140 left: 202px;
141 margin: 0px;
141 margin: 0px;
142 padding: 0px;
142 padding: 0px;
143 position: absolute;
143 position: absolute;
144 }
144 }
145
145
146 div#notebook_panel {
146 div#notebook_panel {
147 /* The L margin will be set in the Javascript code*/
147 /* The L margin will be set in the Javascript code*/
148 margin: 0px 0px 0px 0px;
148 margin: 0px 0px 0px 0px;
149 padding: 0px;
149 padding: 0px;
150 }
150 }
151
151
152 div#notebook {
152 div#notebook {
153 overflow-y: scroll;
153 overflow-y: scroll;
154 overflow-x: auto;
154 overflow-x: auto;
155 width: 100%;
155 width: 100%;
156 padding: 0px 15px 0px 15px;
156 padding: 0px 15px 0px 15px;
157 margin: 0px
157 margin: 0px
158 background-color: white;
158 background-color: white;
159 font-size: 10pt;
159 font-size: 11pt;
160 }
160 }
161
161
162 div#pager_splitter {
162 div#pager_splitter {
163 height: 8px;
163 height: 8px;
164 }
164 }
165
165
166 div#pager {
166 div#pager {
167 padding: 15px;
167 padding: 15px;
168 overflow: auto;
168 overflow: auto;
169 }
169 }
170
170
171 .monospace-font {
171 .monospace-font {
172 font-family: monospace;
172 font-family: monospace;
173 font-size: 10pt;
173 font-size: 11pt;
174 }
174 }
175
175
176 div.cell {
176 div.cell {
177 width: 100%;
177 width: 100%;
178 padding: 5px;
178 padding: 5px;
179 /* This acts as a spacer between cells, that is outside the border */
179 /* This acts as a spacer between cells, that is outside the border */
180 margin: 15px 0px 15px 0px;
180 margin: 15px 0px 15px 0px;
181 }
181 }
182
182
183 div.code_cell {
183 div.code_cell {
184 background-color: white;
184 background-color: white;
185 }
185 }
186
186
187 div.prompt {
187 div.prompt {
188 width: 90px;
188 width: 90px;
189 padding: 0px;
189 padding: 0.4em;
190 margin: 0px;
190 margin: 0px;
191 line-height: 1em;
191 }
192 }
192
193
193 div.input_prompt {
194 div.input_prompt {
194 color: navy;
195 color: navy;
195 }
196 }
196
197
197 div.output {
198 div.output {
198 /* This is a spacer between the input and output of each cell */
199 /* This is a spacer between the input and output of each cell */
199 margin-top: 15px;
200 margin-top: 15px;
200 }
201 }
201
202
202 div.output_prompt {
203 div.output_prompt {
203 color: darkred;
204 color: darkred;
204 }
205 }
205
206
206 div.output_area {
207 div.output_area {
207 text-align: left;
208 text-align: left;
208 color: black;
209 color: black;
209 }
210 }
210
211
211 div.output_latex {
212 div.output_latex {
212 /* Slightly bigger than the rest of the notebook */
213 /* Slightly bigger than the rest of the notebook */
213 font-size: 11pt;
214 font-size: 12pt;
214 }
215 }
215
216
216 div.output_png {
217 div.output_png {
217 }
218 }
218
219
219 div.text_cell {
220 div.text_cell {
220 background-color: white;
221 background-color: white;
221 }
222 }
222
223
223 textarea.text_cell_input {
224 textarea.text_cell_input {
224 /* Slightly bigger than the rest of the notebook */
225 /* Slightly bigger than the rest of the notebook */
225 font-size: 11pt;
226 font-size: 12pt;
226 outline: none;
227 outline: none;
227 resize: none;
228 resize: none;
228 width: inherit;
229 width: inherit;
229 border-style: none;
230 border-style: none;
230 padding: 0px;
231 padding: 0px;
231 margin: 0px;
232 margin: 0px;
232 color: black;
233 color: black;
233 }
234 }
234
235
235 div.text_cell_render {
236 div.text_cell_render {
236 font-family: "Helvetica Neue", Arial, Helvetica, Geneva, sans-serif;
237 font-family: "Helvetica Neue", Arial, Helvetica, Geneva, sans-serif;
237 /* Slightly bigger than the rest of the notebook */
238 /* Slightly bigger than the rest of the notebook */
238 font-size: 11pt;
239 font-size: 12pt;
239 outline: none;
240 outline: none;
240 resize: none;
241 resize: none;
241 width: inherit;
242 width: inherit;
242 border-style: none;
243 border-style: none;
243 padding: 5px;
244 padding: 5px;
244 color: black;
245 color: black;
245 }
246 }
246
247
247 div.text_cell_render em {font-style: italic;}
248 div.text_cell_render em {font-style: italic;}
248 div.text_cell_render strong {font-weight: bold;}
249 div.text_cell_render strong {font-weight: bold;}
249 div.text_cell_render u {text-decoration: underline;}
250 div.text_cell_render u {text-decoration: underline;}
250 div.text_cell_render :link { text-decoration: underline }
251 div.text_cell_render :link { text-decoration: underline }
251 div.text_cell_render :visited { text-decoration: underline }
252 div.text_cell_render :visited { text-decoration: underline }
252 div.text_cell_render h1 {font-size: 2.0em; margin: .67em 0; font-weight: bold;}
253 div.text_cell_render h1 {font-size: 2.0em; margin: .67em 0; font-weight: bold;}
253 div.text_cell_render h2 {font-size: 1.5em; margin: .75em 0; font-weight: bold;}
254 div.text_cell_render h2 {font-size: 1.5em; margin: .75em 0; font-weight: bold;}
254 div.text_cell_render h3 {font-size: 1.17em; margin: .83em 0; font-weight: bold;}
255 div.text_cell_render h3 {font-size: 1.17em; margin: .83em 0; font-weight: bold;}
255 div.text_cell_render h4 {margin: 1.12em 0; font-weight: bold;}
256 div.text_cell_render h4 {margin: 1.12em 0; font-weight: bold;}
256 div.text_cell_render h5 {font-size: .83em; margin: 1.5em 0; font-weight: bold;}
257 div.text_cell_render h5 {font-size: .83em; margin: 1.5em 0; font-weight: bold;}
257 div.text_cell_render h6 {font-size: .75em; margin: 1.67em 0; font-weight: bold;}
258 div.text_cell_render h6 {font-size: .75em; margin: 1.67em 0; font-weight: bold;}
258 div.text_cell_render ul {list-style:disc; margin-left: 40px;}
259 div.text_cell_render ul {list-style:disc; margin-left: 40px;}
259 div.text_cell_render ul ul {list-style:square; margin-left: 40px;}
260 div.text_cell_render ul ul {list-style:square; margin-left: 40px;}
260 div.text_cell_render ul ul ul {list-style:circle; margin-left: 40px;}
261 div.text_cell_render ul ul ul {list-style:circle; margin-left: 40px;}
261 div.text_cell_render ol {list-style:upper-roman; margin-left: 40px;}
262 div.text_cell_render ol {list-style:upper-roman; margin-left: 40px;}
262 div.text_cell_render ol ol {list-style:upper-alpha;}
263 div.text_cell_render ol ol {list-style:upper-alpha;}
263 div.text_cell_render ol ol ol {list-style:decimal;}
264 div.text_cell_render ol ol ol {list-style:decimal;}
264 div.text_cell_render ol ol ol ol {list-style:lower-alpha;}
265 div.text_cell_render ol ol ol ol {list-style:lower-alpha;}
265 div.text_cell_render ol ol ol ol ol {list-style:lower-roman;}
266 div.text_cell_render ol ol ol ol ol {list-style:lower-roman;}
266
267
267
268
@@ -1,590 +1,590 b''
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 // The focus is not quite working here.
60 // The focus is not quite working here.
61 var cell = that.selected_cell();
61 var cell = that.selected_cell();
62 var cell_index = that.find_cell_index(cell);
62 var cell_index = that.find_cell_index(cell);
63 // TODO: the logic here needs to be moved into appropriate
63 // TODO: the logic here needs to be moved into appropriate
64 // methods of Notebook.
64 // methods of Notebook.
65 if (cell instanceof IPython.CodeCell) {
65 if (cell instanceof IPython.CodeCell) {
66 event.preventDefault();
66 event.preventDefault();
67 cell.clear_output();
67 cell.clear_output();
68 var code = cell.get_code();
68 var code = cell.get_code();
69 if (that.notebook_load_re.test(code)) {
69 if (that.notebook_load_re.test(code)) {
70 var code_parts = code.split(' ');
70 var code_parts = code.split(' ');
71 if (code_parts.length === 3) {
71 if (code_parts.length === 3) {
72 that.load_notebook(code_parts[2]);
72 that.load_notebook(code_parts[2]);
73 };
73 };
74 } else if (that.notebook_save_re.test(code)) {
74 } else if (that.notebook_save_re.test(code)) {
75 var code_parts = code.split(' ');
75 var code_parts = code.split(' ');
76 if (code_parts.length === 3) {
76 if (code_parts.length === 3) {
77 that.save_notebook(code_parts[2]);
77 that.save_notebook(code_parts[2]);
78 } else {
78 } else {
79 that.save_notebook()
79 that.save_notebook()
80 };
80 };
81 } else {
81 } else {
82 var msg_id = that.kernel.execute(cell.get_code());
82 var msg_id = that.kernel.execute(cell.get_code());
83 that.msg_cell_map[msg_id] = cell.cell_id;
83 that.msg_cell_map[msg_id] = cell.cell_id;
84 };
84 };
85 } else if (cell instanceof IPython.TextCell) {
85 } else if (cell instanceof IPython.TextCell) {
86 event.preventDefault();
86 event.preventDefault();
87 cell.render();
87 cell.render();
88 }
88 }
89 if (cell_index === (that.ncells()-1)) {
89 if (cell_index === (that.ncells()-1)) {
90 that.insert_code_cell_after();
90 that.insert_code_cell_after();
91 // If we are adding a new cell at the end, scroll down to show it.
91 // If we are adding a new cell at the end, scroll down to show it.
92 that.scroll_to_bottom();
92 that.scroll_to_bottom();
93 } else {
93 } else {
94 that.select(cell_index+1);
94 that.select(cell_index+1);
95 };
95 };
96 };
96 };
97 });
97 });
98
98
99 this.element.bind('collapse_pager', function () {
99 this.element.bind('collapse_pager', function () {
100 var app_height = $('div#notebook_app').height(); // content height
100 var app_height = $('div#notebook_app').height(); // content height
101 var splitter_height = $('div#pager_splitter').outerHeight(true);
101 var splitter_height = $('div#pager_splitter').outerHeight(true);
102 var new_height = app_height - splitter_height;
102 var new_height = app_height - splitter_height;
103 that.element.animate({height : new_height + 'px'}, 'fast');
103 that.element.animate({height : new_height + 'px'}, 'fast');
104 });
104 });
105
105
106 this.element.bind('expand_pager', function () {
106 this.element.bind('expand_pager', function () {
107 var app_height = $('div#notebook_app').height(); // content height
107 var app_height = $('div#notebook_app').height(); // content height
108 var splitter_height = $('div#pager_splitter').outerHeight(true);
108 var splitter_height = $('div#pager_splitter').outerHeight(true);
109 var pager_height = $('div#pager').outerHeight(true);
109 var pager_height = $('div#pager').outerHeight(true);
110 var new_height = app_height - pager_height - splitter_height;
110 var new_height = app_height - pager_height - splitter_height;
111 that.element.animate({height : new_height + 'px'}, 'fast');
111 that.element.animate({height : new_height + 'px'}, 'fast');
112 });
112 });
113
113
114 this.element.bind('collapse_left_panel', function () {
114 this.element.bind('collapse_left_panel', function () {
115 var splitter_width = $('div#left_panel_splitter').outerWidth(true);
115 var splitter_width = $('div#left_panel_splitter').outerWidth(true);
116 var new_margin = splitter_width;
116 var new_margin = splitter_width;
117 $('div#notebook_panel').animate({marginLeft : new_margin + 'px'}, 'fast');
117 $('div#notebook_panel').animate({marginLeft : new_margin + 'px'}, 'fast');
118 });
118 });
119
119
120 this.element.bind('expand_left_panel', function () {
120 this.element.bind('expand_left_panel', function () {
121 var splitter_width = $('div#left_panel_splitter').outerWidth(true);
121 var splitter_width = $('div#left_panel_splitter').outerWidth(true);
122 var left_panel_width = IPython.left_panel.width;
122 var left_panel_width = IPython.left_panel.width;
123 var new_margin = splitter_width + left_panel_width;
123 var new_margin = splitter_width + left_panel_width;
124 $('div#notebook_panel').animate({marginLeft : new_margin + 'px'}, 'fast');
124 $('div#notebook_panel').animate({marginLeft : new_margin + 'px'}, 'fast');
125 });
125 });
126 };
126 };
127
127
128
128
129 Notebook.prototype.scroll_to_bottom = function () {
129 Notebook.prototype.scroll_to_bottom = function () {
130 this.element.animate({scrollTop:this.element.get(0).scrollHeight}, 'slow');
130 this.element.animate({scrollTop:this.element.get(0).scrollHeight}, 0);
131 };
131 };
132
132
133 // Cell indexing, retrieval, etc.
133 // Cell indexing, retrieval, etc.
134
134
135
135
136 Notebook.prototype.cell_elements = function () {
136 Notebook.prototype.cell_elements = function () {
137 return this.element.children("div.cell");
137 return this.element.children("div.cell");
138 }
138 }
139
139
140
140
141 Notebook.prototype.ncells = function (cell) {
141 Notebook.prototype.ncells = function (cell) {
142 return this.cell_elements().length;
142 return this.cell_elements().length;
143 }
143 }
144
144
145
145
146 // TODO: we are often calling cells as cells()[i], which we should optimize
146 // TODO: we are often calling cells as cells()[i], which we should optimize
147 // to cells(i) or a new method.
147 // to cells(i) or a new method.
148 Notebook.prototype.cells = function () {
148 Notebook.prototype.cells = function () {
149 return this.cell_elements().toArray().map(function (e) {
149 return this.cell_elements().toArray().map(function (e) {
150 return $(e).data("cell");
150 return $(e).data("cell");
151 });
151 });
152 }
152 }
153
153
154
154
155 Notebook.prototype.find_cell_index = function (cell) {
155 Notebook.prototype.find_cell_index = function (cell) {
156 var result = null;
156 var result = null;
157 this.cell_elements().filter(function (index) {
157 this.cell_elements().filter(function (index) {
158 if ($(this).data("cell") === cell) {
158 if ($(this).data("cell") === cell) {
159 result = index;
159 result = index;
160 };
160 };
161 });
161 });
162 return result;
162 return result;
163 };
163 };
164
164
165
165
166 Notebook.prototype.index_or_selected = function (index) {
166 Notebook.prototype.index_or_selected = function (index) {
167 return index || this.selected_index() || 0;
167 return index || this.selected_index() || 0;
168 }
168 }
169
169
170
170
171 Notebook.prototype.select = function (index) {
171 Notebook.prototype.select = function (index) {
172 if (index !== undefined && index >= 0 && index < this.ncells()) {
172 if (index !== undefined && index >= 0 && index < this.ncells()) {
173 if (this.selected_index() !== null) {
173 if (this.selected_index() !== null) {
174 this.selected_cell().unselect();
174 this.selected_cell().unselect();
175 };
175 };
176 this.cells()[index].select();
176 this.cells()[index].select();
177 if (index === (this.ncells()-1)) {
177 if (index === (this.ncells()-1)) {
178 this.scroll_to_bottom();
178 this.scroll_to_bottom();
179 };
179 };
180 };
180 };
181 return this;
181 return this;
182 };
182 };
183
183
184
184
185 Notebook.prototype.select_next = function () {
185 Notebook.prototype.select_next = function () {
186 var index = this.selected_index();
186 var index = this.selected_index();
187 if (index !== null && index >= 0 && (index+1) < this.ncells()) {
187 if (index !== null && index >= 0 && (index+1) < this.ncells()) {
188 this.select(index+1);
188 this.select(index+1);
189 };
189 };
190 return this;
190 return this;
191 };
191 };
192
192
193
193
194 Notebook.prototype.select_prev = function () {
194 Notebook.prototype.select_prev = function () {
195 var index = this.selected_index();
195 var index = this.selected_index();
196 if (index !== null && index >= 0 && (index-1) < this.ncells()) {
196 if (index !== null && index >= 0 && (index-1) < this.ncells()) {
197 this.select(index-1);
197 this.select(index-1);
198 };
198 };
199 return this;
199 return this;
200 };
200 };
201
201
202
202
203 Notebook.prototype.selected_index = function () {
203 Notebook.prototype.selected_index = function () {
204 var result = null;
204 var result = null;
205 this.cell_elements().filter(function (index) {
205 this.cell_elements().filter(function (index) {
206 if ($(this).data("cell").selected === true) {
206 if ($(this).data("cell").selected === true) {
207 result = index;
207 result = index;
208 };
208 };
209 });
209 });
210 return result;
210 return result;
211 };
211 };
212
212
213
213
214 Notebook.prototype.cell_for_msg = function (msg_id) {
214 Notebook.prototype.cell_for_msg = function (msg_id) {
215 var cell_id = this.msg_cell_map[msg_id];
215 var cell_id = this.msg_cell_map[msg_id];
216 var result = null;
216 var result = null;
217 this.cell_elements().filter(function (index) {
217 this.cell_elements().filter(function (index) {
218 cell = $(this).data("cell");
218 cell = $(this).data("cell");
219 if (cell.cell_id === cell_id) {
219 if (cell.cell_id === cell_id) {
220 result = cell;
220 result = cell;
221 };
221 };
222 });
222 });
223 return result;
223 return result;
224 };
224 };
225
225
226
226
227 Notebook.prototype.selected_cell = function () {
227 Notebook.prototype.selected_cell = function () {
228 return this.cell_elements().eq(this.selected_index()).data("cell");
228 return this.cell_elements().eq(this.selected_index()).data("cell");
229 }
229 }
230
230
231
231
232 // Cell insertion, deletion and moving.
232 // Cell insertion, deletion and moving.
233
233
234
234
235 Notebook.prototype.delete_cell = function (index) {
235 Notebook.prototype.delete_cell = function (index) {
236 var i = index || this.selected_index();
236 var i = index || this.selected_index();
237 if (i !== null && i >= 0 && i < this.ncells()) {
237 if (i !== null && i >= 0 && i < this.ncells()) {
238 this.cell_elements().eq(i).remove();
238 this.cell_elements().eq(i).remove();
239 if (i === (this.ncells())) {
239 if (i === (this.ncells())) {
240 this.select(i-1);
240 this.select(i-1);
241 } else {
241 } else {
242 this.select(i);
242 this.select(i);
243 };
243 };
244 };
244 };
245 return this;
245 return this;
246 };
246 };
247
247
248
248
249 Notebook.prototype.append_cell = function (cell) {
249 Notebook.prototype.append_cell = function (cell) {
250 this.element.find('div.end_space').before(cell.element);
250 this.element.find('div.end_space').before(cell.element);
251 return this;
251 return this;
252 };
252 };
253
253
254
254
255 Notebook.prototype.insert_cell_after = function (cell, index) {
255 Notebook.prototype.insert_cell_after = function (cell, index) {
256 var ncells = this.ncells();
256 var ncells = this.ncells();
257 if (ncells === 0) {
257 if (ncells === 0) {
258 this.append_cell(cell);
258 this.append_cell(cell);
259 return this;
259 return this;
260 };
260 };
261 if (index >= 0 && index < ncells) {
261 if (index >= 0 && index < ncells) {
262 this.cell_elements().eq(index).after(cell.element);
262 this.cell_elements().eq(index).after(cell.element);
263 };
263 };
264 return this
264 return this
265 };
265 };
266
266
267
267
268 Notebook.prototype.insert_cell_before = function (cell, index) {
268 Notebook.prototype.insert_cell_before = function (cell, index) {
269 var ncells = this.ncells();
269 var ncells = this.ncells();
270 if (ncells === 0) {
270 if (ncells === 0) {
271 this.append_cell(cell);
271 this.append_cell(cell);
272 return this;
272 return this;
273 };
273 };
274 if (index >= 0 && index < ncells) {
274 if (index >= 0 && index < ncells) {
275 this.cell_elements().eq(index).before(cell.element);
275 this.cell_elements().eq(index).before(cell.element);
276 };
276 };
277 return this;
277 return this;
278 };
278 };
279
279
280
280
281 Notebook.prototype.move_cell_up = function (index) {
281 Notebook.prototype.move_cell_up = function (index) {
282 var i = index || this.selected_index();
282 var i = index || this.selected_index();
283 if (i !== null && i < this.ncells() && i > 0) {
283 if (i !== null && i < this.ncells() && i > 0) {
284 var pivot = this.cell_elements().eq(i-1);
284 var pivot = this.cell_elements().eq(i-1);
285 var tomove = this.cell_elements().eq(i);
285 var tomove = this.cell_elements().eq(i);
286 if (pivot !== null && tomove !== null) {
286 if (pivot !== null && tomove !== null) {
287 tomove.detach();
287 tomove.detach();
288 pivot.before(tomove);
288 pivot.before(tomove);
289 this.select(i-1);
289 this.select(i-1);
290 };
290 };
291 };
291 };
292 return this;
292 return this;
293 }
293 }
294
294
295
295
296 Notebook.prototype.move_cell_down = function (index) {
296 Notebook.prototype.move_cell_down = function (index) {
297 var i = index || this.selected_index();
297 var i = index || this.selected_index();
298 if (i !== null && i < (this.ncells()-1) && i >= 0) {
298 if (i !== null && i < (this.ncells()-1) && i >= 0) {
299 var pivot = this.cell_elements().eq(i+1)
299 var pivot = this.cell_elements().eq(i+1)
300 var tomove = this.cell_elements().eq(i)
300 var tomove = this.cell_elements().eq(i)
301 if (pivot !== null && tomove !== null) {
301 if (pivot !== null && tomove !== null) {
302 tomove.detach();
302 tomove.detach();
303 pivot.after(tomove);
303 pivot.after(tomove);
304 this.select(i+1);
304 this.select(i+1);
305 };
305 };
306 };
306 };
307 return this;
307 return this;
308 }
308 }
309
309
310
310
311 Notebook.prototype.sort_cells = function () {
311 Notebook.prototype.sort_cells = function () {
312 var ncells = this.ncells();
312 var ncells = this.ncells();
313 var sindex = this.selected_index();
313 var sindex = this.selected_index();
314 var swapped;
314 var swapped;
315 do {
315 do {
316 swapped = false
316 swapped = false
317 for (var i=1; i<ncells; i++) {
317 for (var i=1; i<ncells; i++) {
318 current = this.cell_elements().eq(i).data("cell");
318 current = this.cell_elements().eq(i).data("cell");
319 previous = this.cell_elements().eq(i-1).data("cell");
319 previous = this.cell_elements().eq(i-1).data("cell");
320 if (previous.input_prompt_number > current.input_prompt_number) {
320 if (previous.input_prompt_number > current.input_prompt_number) {
321 this.move_cell_up(i);
321 this.move_cell_up(i);
322 swapped = true;
322 swapped = true;
323 };
323 };
324 };
324 };
325 } while (swapped);
325 } while (swapped);
326 this.select(sindex);
326 this.select(sindex);
327 return this;
327 return this;
328 };
328 };
329
329
330
330
331 Notebook.prototype.insert_code_cell_before = function (index) {
331 Notebook.prototype.insert_code_cell_before = function (index) {
332 // TODO: Bounds check for i
332 // TODO: Bounds check for i
333 var i = this.index_or_selected(index);
333 var i = this.index_or_selected(index);
334 var cell = new IPython.CodeCell(this);
334 var cell = new IPython.CodeCell(this);
335 cell.set_input_prompt(this.next_prompt_number);
335 cell.set_input_prompt(this.next_prompt_number);
336 this.next_prompt_number = this.next_prompt_number + 1;
336 this.next_prompt_number = this.next_prompt_number + 1;
337 this.insert_cell_before(cell, i);
337 this.insert_cell_before(cell, i);
338 this.select(this.find_cell_index(cell));
338 this.select(this.find_cell_index(cell));
339 return this;
339 return this;
340 }
340 }
341
341
342
342
343 Notebook.prototype.insert_code_cell_after = function (index) {
343 Notebook.prototype.insert_code_cell_after = function (index) {
344 // TODO: Bounds check for i
344 // TODO: Bounds check for i
345 var i = this.index_or_selected(index);
345 var i = this.index_or_selected(index);
346 var cell = new IPython.CodeCell(this);
346 var cell = new IPython.CodeCell(this);
347 cell.set_input_prompt(this.next_prompt_number);
347 cell.set_input_prompt(this.next_prompt_number);
348 this.next_prompt_number = this.next_prompt_number + 1;
348 this.next_prompt_number = this.next_prompt_number + 1;
349 this.insert_cell_after(cell, i);
349 this.insert_cell_after(cell, i);
350 this.select(this.find_cell_index(cell));
350 this.select(this.find_cell_index(cell));
351 return this;
351 return this;
352 }
352 }
353
353
354
354
355 Notebook.prototype.insert_text_cell_before = function (index) {
355 Notebook.prototype.insert_text_cell_before = function (index) {
356 // TODO: Bounds check for i
356 // TODO: Bounds check for i
357 var i = this.index_or_selected(index);
357 var i = this.index_or_selected(index);
358 var cell = new IPython.TextCell(this);
358 var cell = new IPython.TextCell(this);
359 cell.config_mathjax();
359 cell.config_mathjax();
360 this.insert_cell_before(cell, i);
360 this.insert_cell_before(cell, i);
361 this.select(this.find_cell_index(cell));
361 this.select(this.find_cell_index(cell));
362 return this;
362 return this;
363 }
363 }
364
364
365
365
366 Notebook.prototype.insert_text_cell_after = function (index) {
366 Notebook.prototype.insert_text_cell_after = function (index) {
367 // TODO: Bounds check for i
367 // TODO: Bounds check for i
368 var i = this.index_or_selected(index);
368 var i = this.index_or_selected(index);
369 var cell = new IPython.TextCell(this);
369 var cell = new IPython.TextCell(this);
370 cell.config_mathjax();
370 cell.config_mathjax();
371 this.insert_cell_after(cell, i);
371 this.insert_cell_after(cell, i);
372 this.select(this.find_cell_index(cell));
372 this.select(this.find_cell_index(cell));
373 return this;
373 return this;
374 }
374 }
375
375
376
376
377 Notebook.prototype.text_to_code = function (index) {
377 Notebook.prototype.text_to_code = function (index) {
378 // TODO: Bounds check for i
378 // TODO: Bounds check for i
379 var i = this.index_or_selected(index);
379 var i = this.index_or_selected(index);
380 var source_element = this.cell_elements().eq(i);
380 var source_element = this.cell_elements().eq(i);
381 var source_cell = source_element.data("cell");
381 var source_cell = source_element.data("cell");
382 if (source_cell instanceof IPython.TextCell) {
382 if (source_cell instanceof IPython.TextCell) {
383 this.insert_code_cell_after(i);
383 this.insert_code_cell_after(i);
384 var target_cell = this.cells()[i+1];
384 var target_cell = this.cells()[i+1];
385 target_cell.set_code(source_cell.get_text());
385 target_cell.set_code(source_cell.get_text());
386 source_element.remove();
386 source_element.remove();
387 };
387 };
388 };
388 };
389
389
390
390
391 Notebook.prototype.code_to_text = function (index) {
391 Notebook.prototype.code_to_text = function (index) {
392 // TODO: Bounds check for i
392 // TODO: Bounds check for i
393 var i = this.index_or_selected(index);
393 var i = this.index_or_selected(index);
394 var source_element = this.cell_elements().eq(i);
394 var source_element = this.cell_elements().eq(i);
395 var source_cell = source_element.data("cell");
395 var source_cell = source_element.data("cell");
396 if (source_cell instanceof IPython.CodeCell) {
396 if (source_cell instanceof IPython.CodeCell) {
397 this.insert_text_cell_after(i);
397 this.insert_text_cell_after(i);
398 var target_cell = this.cells()[i+1];
398 var target_cell = this.cells()[i+1];
399 var text = source_cell.get_code();
399 var text = source_cell.get_code();
400 if (text === "") {text = target_cell.placeholder;};
400 if (text === "") {text = target_cell.placeholder;};
401 target_cell.set_text(text);
401 target_cell.set_text(text);
402 source_element.remove();
402 source_element.remove();
403 target_cell.edit();
403 target_cell.edit();
404 };
404 };
405 };
405 };
406
406
407
407
408 // Cell collapsing
408 // Cell collapsing
409
409
410 Notebook.prototype.collapse = function (index) {
410 Notebook.prototype.collapse = function (index) {
411 var i = this.index_or_selected(index);
411 var i = this.index_or_selected(index);
412 this.cells()[i].collapse();
412 this.cells()[i].collapse();
413 };
413 };
414
414
415
415
416 Notebook.prototype.expand = function (index) {
416 Notebook.prototype.expand = function (index) {
417 var i = this.index_or_selected(index);
417 var i = this.index_or_selected(index);
418 this.cells()[i].expand();
418 this.cells()[i].expand();
419 };
419 };
420
420
421
421
422 // Kernel related things
422 // Kernel related things
423
423
424 Notebook.prototype.start_kernel = function () {
424 Notebook.prototype.start_kernel = function () {
425 this.kernel = new IPython.Kernel();
425 this.kernel = new IPython.Kernel();
426 this.kernel.start_kernel($.proxy(this.kernel_started, this));
426 this.kernel.start_kernel($.proxy(this.kernel_started, this));
427 };
427 };
428
428
429
429
430 Notebook.prototype.handle_shell_reply = function (e) {
430 Notebook.prototype.handle_shell_reply = function (e) {
431 reply = $.parseJSON(e.data);
431 reply = $.parseJSON(e.data);
432 var header = reply.header;
432 var header = reply.header;
433 var content = reply.content;
433 var content = reply.content;
434 var msg_type = header.msg_type;
434 var msg_type = header.msg_type;
435 // console.log(reply);
435 // console.log(reply);
436 var cell = this.cell_for_msg(reply.parent_header.msg_id);
436 var cell = this.cell_for_msg(reply.parent_header.msg_id);
437 if (msg_type === "execute_reply") {
437 if (msg_type === "execute_reply") {
438 cell.set_input_prompt(content.execution_count);
438 cell.set_input_prompt(content.execution_count);
439 };
439 };
440 var payload = content.payload || [];
440 var payload = content.payload || [];
441 this.handle_payload(content.payload);
441 this.handle_payload(content.payload);
442 };
442 };
443
443
444
444
445 Notebook.prototype.handle_payload = function (payload) {
445 Notebook.prototype.handle_payload = function (payload) {
446 var l = payload.length;
446 var l = payload.length;
447 if (l > 0) {
447 if (l > 0) {
448 IPython.pager.clear();
448 IPython.pager.clear();
449 IPython.pager.expand();
449 IPython.pager.expand();
450 };
450 };
451 for (var i=0; i<l; i++) {
451 for (var i=0; i<l; i++) {
452 IPython.pager.append_text(payload[i].text);
452 IPython.pager.append_text(payload[i].text);
453 };
453 };
454 };
454 };
455
455
456
456
457 Notebook.prototype.handle_iopub_reply = function (e) {
457 Notebook.prototype.handle_iopub_reply = function (e) {
458 reply = $.parseJSON(e.data);
458 reply = $.parseJSON(e.data);
459 var content = reply.content;
459 var content = reply.content;
460 // console.log(reply);
460 // console.log(reply);
461 var msg_type = reply.header.msg_type;
461 var msg_type = reply.header.msg_type;
462 var cell = this.cell_for_msg(reply.parent_header.msg_id);
462 var cell = this.cell_for_msg(reply.parent_header.msg_id);
463 if (msg_type === "stream") {
463 if (msg_type === "stream") {
464 cell.expand();
464 cell.expand();
465 cell.append_stream(content.data + "\n");
465 cell.append_stream(content.data + "\n");
466 } else if (msg_type === "display_data") {
466 } else if (msg_type === "display_data") {
467 cell.expand();
467 cell.expand();
468 cell.append_display_data(content.data);
468 cell.append_display_data(content.data);
469 } else if (msg_type === "pyout") {
469 } else if (msg_type === "pyout") {
470 cell.expand();
470 cell.expand();
471 cell.append_pyout(content.data, content.execution_count)
471 cell.append_pyout(content.data, content.execution_count)
472 } else if (msg_type === "pyerr") {
472 } else if (msg_type === "pyerr") {
473 cell.expand();
473 cell.expand();
474 cell.append_pyerr(content.ename, content.evalue, content.traceback);
474 cell.append_pyerr(content.ename, content.evalue, content.traceback);
475 } else if (msg_type === "status") {
475 } else if (msg_type === "status") {
476 if (content.execution_state === "busy") {
476 if (content.execution_state === "busy") {
477 this.kernel.status_busy();
477 this.kernel.status_busy();
478 } else if (content.execution_state === "idle") {
478 } else if (content.execution_state === "idle") {
479 this.kernel.status_idle();
479 this.kernel.status_idle();
480 };
480 };
481 }
481 }
482 };
482 };
483
483
484
484
485 Notebook.prototype.kernel_started = function () {
485 Notebook.prototype.kernel_started = function () {
486 console.log("Kernel started: ", this.kernel.kernel_id);
486 console.log("Kernel started: ", this.kernel.kernel_id);
487 this.kernel.shell_channel.onmessage = $.proxy(this.handle_shell_reply,this);
487 this.kernel.shell_channel.onmessage = $.proxy(this.handle_shell_reply,this);
488 this.kernel.iopub_channel.onmessage = $.proxy(this.handle_iopub_reply,this);
488 this.kernel.iopub_channel.onmessage = $.proxy(this.handle_iopub_reply,this);
489 };
489 };
490
490
491
491
492 // Persistance and loading
492 // Persistance and loading
493
493
494
494
495 Notebook.prototype.fromJSON = function (data) {
495 Notebook.prototype.fromJSON = function (data) {
496 var ncells = this.ncells();
496 var ncells = this.ncells();
497 for (var i=0; i<ncells; i++) {
497 for (var i=0; i<ncells; i++) {
498 // Always delete cell 0 as they get renumbered as they are deleted.
498 // Always delete cell 0 as they get renumbered as they are deleted.
499 this.delete_cell(0);
499 this.delete_cell(0);
500 };
500 };
501 var new_cells = data.cells;
501 var new_cells = data.cells;
502 ncells = new_cells.length;
502 ncells = new_cells.length;
503 var cell_data = null;
503 var cell_data = null;
504 for (var i=0; i<ncells; i++) {
504 for (var i=0; i<ncells; i++) {
505 cell_data = new_cells[i];
505 cell_data = new_cells[i];
506 if (cell_data.cell_type == 'code') {
506 if (cell_data.cell_type == 'code') {
507 this.insert_code_cell_after();
507 this.insert_code_cell_after();
508 this.selected_cell().fromJSON(cell_data);
508 this.selected_cell().fromJSON(cell_data);
509 } else if (cell_data.cell_type === 'text') {
509 } else if (cell_data.cell_type === 'text') {
510 this.insert_text_cell_after();
510 this.insert_text_cell_after();
511 this.selected_cell().fromJSON(cell_data);
511 this.selected_cell().fromJSON(cell_data);
512 };
512 };
513 };
513 };
514 };
514 };
515
515
516
516
517 Notebook.prototype.toJSON = function () {
517 Notebook.prototype.toJSON = function () {
518 var cells = this.cells();
518 var cells = this.cells();
519 var ncells = cells.length;
519 var ncells = cells.length;
520 cell_array = new Array(ncells);
520 cell_array = new Array(ncells);
521 for (var i=0; i<ncells; i++) {
521 for (var i=0; i<ncells; i++) {
522 cell_array[i] = cells[i].toJSON();
522 cell_array[i] = cells[i].toJSON();
523 };
523 };
524 json = {
524 json = {
525 cells : cell_array
525 cells : cell_array
526 };
526 };
527 return json
527 return json
528 };
528 };
529
529
530
530
531 Notebook.prototype.test_filename = function (filename) {
531 Notebook.prototype.test_filename = function (filename) {
532 if (this.notebook_filename_re.test(filename)) {
532 if (this.notebook_filename_re.test(filename)) {
533 return true;
533 return true;
534 } else {
534 } else {
535 var bad_filename = $('<div/>');
535 var bad_filename = $('<div/>');
536 bad_filename.html(
536 bad_filename.html(
537 "The filename you entered (" + filename + ") is not valid. Notebook filenames must have the following form: foo.ipynb"
537 "The filename you entered (" + filename + ") is not valid. Notebook filenames must have the following form: foo.ipynb"
538 );
538 );
539 bad_filename.dialog({title: 'Invalid filename', modal: true});
539 bad_filename.dialog({title: 'Invalid filename', modal: true});
540 return false;
540 return false;
541 };
541 };
542 };
542 };
543
543
544 Notebook.prototype.save_notebook = function (filename) {
544 Notebook.prototype.save_notebook = function (filename) {
545 this.filename = filename || this.filename || '';
545 this.filename = filename || this.filename || '';
546 if (this.filename === '') {
546 if (this.filename === '') {
547 var no_filename = $('<div/>');
547 var no_filename = $('<div/>');
548 no_filename.html(
548 no_filename.html(
549 "This notebook has no filename, please specify a filename of the form: foo.ipynb"
549 "This notebook has no filename, please specify a filename of the form: foo.ipynb"
550 );
550 );
551 no_filename.dialog({title: 'Missing filename', modal: true});
551 no_filename.dialog({title: 'Missing filename', modal: true});
552 return;
552 return;
553 }
553 }
554 if (!this.test_filename(this.filename)) {return;}
554 if (!this.test_filename(this.filename)) {return;}
555 var thedata = this.toJSON();
555 var thedata = this.toJSON();
556 var settings = {
556 var settings = {
557 processData : false,
557 processData : false,
558 cache : false,
558 cache : false,
559 type : "PUT",
559 type : "PUT",
560 data : JSON.stringify(thedata),
560 data : JSON.stringify(thedata),
561 success : function (data, status, xhr) {console.log(data);}
561 success : function (data, status, xhr) {console.log(data);}
562 };
562 };
563 $.ajax("/notebooks/" + this.filename, settings);
563 $.ajax("/notebooks/" + this.filename, settings);
564 };
564 };
565
565
566
566
567 Notebook.prototype.load_notebook = function (filename) {
567 Notebook.prototype.load_notebook = function (filename) {
568 if (!this.test_filename(filename)) {return;}
568 if (!this.test_filename(filename)) {return;}
569 var that = this;
569 var that = this;
570 // We do the call with settings so we can set cache to false.
570 // We do the call with settings so we can set cache to false.
571 var settings = {
571 var settings = {
572 processData : false,
572 processData : false,
573 cache : false,
573 cache : false,
574 type : "GET",
574 type : "GET",
575 dataType : "json",
575 dataType : "json",
576 success : function (data, status, xhr) {
576 success : function (data, status, xhr) {
577 that.fromJSON(data);
577 that.fromJSON(data);
578 that.filename = filename;
578 that.filename = filename;
579 that.kernel.restart();
579 that.kernel.restart();
580 }
580 }
581 };
581 };
582 $.ajax("/notebooks/" + filename, settings);
582 $.ajax("/notebooks/" + filename, settings);
583 }
583 }
584
584
585 IPython.Notebook = Notebook;
585 IPython.Notebook = Notebook;
586
586
587 return IPython;
587 return IPython;
588
588
589 }(IPython));
589 }(IPython));
590
590
General Comments 0
You need to be logged in to leave comments. Login now