##// END OF EJS Templates
Status monitoring added to notebook....
Brian Granger -
Show More
@@ -1,202 +1,221 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 /* Normalize monospace sizing:
61 /* Normalize monospace sizing:
62 en.wikipedia.org/wiki/MediaWiki_talk:Common.css/Archive_11#Teletype_style_fix_for_Chrome */
62 en.wikipedia.org/wiki/MediaWiki_talk:Common.css/Archive_11#Teletype_style_fix_for_Chrome */
63 pre, code, kbd, samp { font-family: monospace, sans-serif; }
63 pre, code, kbd, samp { font-family: monospace, sans-serif; }
64
64
65
65
66
66
67 body {
67 body {
68 background-color: white;
68 background-color: white;
69 font-size: 10pt;
69 font-size: 10pt;
70 }
70 }
71
71
72 span#ipython_notebook h1 {
72 span#ipython_notebook h1 {
73 font-family: "Helvetica Neue", Arial, Helvetica, Geneva, sans-serif;
73 font-family: "Helvetica Neue", Arial, Helvetica, Geneva, sans-serif;
74 font-size: 32pt;
74 font-size: 32pt;
75 padding: 10px;
75 padding: 10px;
76 margin: 10px;
76 margin: 10px;
77 }
77 }
78
78
79 div#toolbar {
79 div#toolbar {
80 width: 100%;
80 width: 100%;
81 height: auto;
81 height: auto;
82 border-bottom-width: 2px;
82 border-bottom-width: 2px;
83 border-bottom-style: solid;
83 border-bottom-style: solid;
84 border-bottom-color: black;
84 border-bottom-color: black;
85 padding: 5px;
85 padding: 5px;
86 }
86 }
87
87
88 span#kernel_status {
89 position: absolute;
90 top: 12%;
91 right: 10px;
92 font-weight: bold;
93 }
94
95 .status_idle {
96 color: gray;
97 }
98
99 .status_busy {
100 color: red;
101 }
102
103 .status_restarting {
104 color: black;
105 }
106
88 div.notebook {
107 div.notebook {
89 width: 790px;
108 width: 790px;
90 height: 650px; /*We might have to detect window height for this*/
109 height: 650px; /*We might have to detect window height for this*/
91 overflow: auto;
110 overflow: auto;
92 margin-left: auto;
111 margin-left: auto;
93 margin-right: auto;
112 margin-right: auto;
94 padding-top: 5px;
113 padding-top: 5px;
95 padding-bottom: 5px;
114 padding-bottom: 5px;
96 background-color: white;
115 background-color: white;
97 }
116 }
98
117
99 div.cell {
118 div.cell {
100 width: 740px;
119 width: 740px;
101 margin: 5px auto 5px 5px;
120 margin: 5px auto 5px 5px;
102 padding: 5px;
121 padding: 5px;
103 position: relative;
122 position: relative;
104 display: table;
123 display: table;
105 }
124 }
106
125
107 div.code_cell {
126 div.code_cell {
108 background-color: white;
127 background-color: white;
109 }
128 }
110
129
111 div.prompt {
130 div.prompt {
112 vertical-align: top;
131 vertical-align: top;
113 display: table-cell;
132 display: table-cell;
114 width: 80px;
133 width: 80px;
115 padding: 0px;
134 padding: 0px;
116 margin: 0px;
135 margin: 0px;
117 font-family: Menlo, "Courier New", Courier, mono;
136 font-family: Menlo, "Courier New", Courier, mono;
118 font-weight: normal;
137 font-weight: normal;
119 font-style: normal;
138 font-style: normal;
120 }
139 }
121
140
122 div.input {
141 div.input {
123 display: table-row;
142 display: table-row;
124 padding: 0px;
143 padding: 0px;
125 margin: 0px;
144 margin: 0px;
126 }
145 }
127
146
128 div.input_prompt {
147 div.input_prompt {
129 color: blue;
148 color: blue;
130 }
149 }
131
150
132 textarea.input_area {
151 textarea.input_area {
133 text-align: left;
152 text-align: left;
134 font-family: Menlo, "Courier New", Courier, mono;
153 font-family: Menlo, "Courier New", Courier, mono;
135 font-size: inherit;
154 font-size: inherit;
136 border-style: none;
155 border-style: none;
137 display: table-cell;
156 display: table-cell;
138 padding: 0px;
157 padding: 0px;
139 margin: 0px;
158 margin: 0px;
140 overflow: auto;
159 overflow: auto;
141 font-weight: normal;
160 font-weight: normal;
142 font-style: normal;
161 font-style: normal;
143 width: 650px;
162 width: 650px;
144 outline: none;
163 outline: none;
145 resize: none;
164 resize: none;
146 }
165 }
147
166
148 div.output {
167 div.output {
149 display: table-row;
168 display: table-row;
150 padding: 0px;
169 padding: 0px;
151 margin: 0px;
170 margin: 0px;
152 }
171 }
153
172
154 div.output_prompt {
173 div.output_prompt {
155 color: red;
174 color: red;
156 }
175 }
157
176
158 div.output_area {
177 div.output_area {
159 text-align: left;
178 text-align: left;
160 font-family: Menlo, "Courier New", Courier, mono;
179 font-family: Menlo, "Courier New", Courier, mono;
161 padding: 0px;
180 padding: 0px;
162 margin: 0px;
181 margin: 0px;
163 display: table-cell;
182 display: table-cell;
164 width: 650px;
183 width: 650px;
165 }
184 }
166
185
167 div.text_cell {
186 div.text_cell {
168 background-color: white;
187 background-color: white;
169 }
188 }
170
189
171 textarea.text_cell_input {
190 textarea.text_cell_input {
172 font-family: "Helvetica Neue", Arial, Helvetica, Geneva, sans-serif;
191 font-family: "Helvetica Neue", Arial, Helvetica, Geneva, sans-serif;
173 font-size: inherit;
192 font-size: inherit;
174 outline: none;
193 outline: none;
175 resize: none;
194 resize: none;
176 width: inherit;
195 width: inherit;
177 border-style: none;
196 border-style: none;
178 padding: 0;
197 padding: 0;
179 margin: 0;
198 margin: 0;
180 color: black;
199 color: black;
181 }
200 }
182
201
183 div.text_cell_render {
202 div.text_cell_render {
184 font-family: "Helvetica Neue", Arial, Helvetica, Geneva, sans-serif;
203 font-family: "Helvetica Neue", Arial, Helvetica, Geneva, sans-serif;
185 font-size: inherit;
204 font-size: inherit;
186 outline: none;
205 outline: none;
187 resize: none;
206 resize: none;
188 width: inherit;
207 width: inherit;
189 border-style: none;
208 border-style: none;
190 padding: 0;
209 padding: 0;
191 margin: 0;
210 margin: 0;
192 color: black;
211 color: black;
193 }
212 }
194
213
195 div.text_cell_render em {
214 div.text_cell_render em {
196 font-style: italic;
215 font-style: italic;
197 }
216 }
198
217
199 div.text_cell_render strong {
218 div.text_cell_render strong {
200 font-weight: bold;
219 font-weight: bold;
201 }
220 }
202
221
@@ -1,833 +1,864 b''
1 var IPYTHON = {};
1 var IPYTHON = {};
2
2
3
3
4
4
5 // $.get("/notebooks/number2.nb",function (data, status, xhr) {console.log(data);});
5 // $.get("/notebooks/number2.nb",function (data, status, xhr) {console.log(data);});
6 //
6 //
7 // settings = {
7 // settings = {
8 // processData : false,
8 // processData : false,
9 // cache : false,
9 // cache : false,
10 // type : "DELETE",
10 // type : "DELETE",
11 // success : function (data, status, xhr) {console.log(data);}
11 // success : function (data, status, xhr) {console.log(data);}
12 // }
12 // }
13 // $.ajax("/notebooks/number2.nb",settings)
13 // $.ajax("/notebooks/number2.nb",settings)
14 //
14 //
15 // settings = {
15 // settings = {
16 // processData : false,
16 // processData : false,
17 // cache : false,
17 // cache : false,
18 // type : "PUT",
18 // type : "PUT",
19 // success : function (data, status, xhr) {console.log(data);}
19 // success : function (data, status, xhr) {console.log(data);}
20 // }
20 // }
21 // $.ajax("/notebooks/number2.nb",settings)
21 // $.ajax("/notebooks/number2.nb",settings)
22
22
23
23
24 //============================================================================
24 //============================================================================
25 // Utilities
25 // Utilities
26 //============================================================================
26 //============================================================================
27
27
28
28
29 var uuid = function () {
29 var uuid = function () {
30 // http://www.ietf.org/rfc/rfc4122.txt
30 // http://www.ietf.org/rfc/rfc4122.txt
31 var s = [];
31 var s = [];
32 var hexDigits = "0123456789ABCDEF";
32 var hexDigits = "0123456789ABCDEF";
33 for (var i = 0; i < 32; i++) {
33 for (var i = 0; i < 32; i++) {
34 s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
34 s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
35 }
35 }
36 s[12] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
36 s[12] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
37 s[16] = hexDigits.substr((s[16] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
37 s[16] = hexDigits.substr((s[16] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
38
38
39 var uuid = s.join("");
39 var uuid = s.join("");
40 return uuid;
40 return uuid;
41 };
41 };
42
42
43
43
44 //Fix raw text to parse correctly in crazy XML
44 //Fix raw text to parse correctly in crazy XML
45 function xmlencode(string) {
45 function xmlencode(string) {
46 return string.replace(/\&/g,'&'+'amp;')
46 return string.replace(/\&/g,'&'+'amp;')
47 .replace(/</g,'&'+'lt;')
47 .replace(/</g,'&'+'lt;')
48 .replace(/>/g,'&'+'gt;')
48 .replace(/>/g,'&'+'gt;')
49 .replace(/\'/g,'&'+'apos;')
49 .replace(/\'/g,'&'+'apos;')
50 .replace(/\"/g,'&'+'quot;')
50 .replace(/\"/g,'&'+'quot;')
51 .replace(/`/g,'&'+'#96;')
51 .replace(/`/g,'&'+'#96;')
52 }
52 }
53
53
54 //Map from terminal commands to CSS classes
54 //Map from terminal commands to CSS classes
55 attrib = {
55 attrib = {
56 "30":"cblack", "31":"cred",
56 "30":"cblack", "31":"cred",
57 "32":"cgreen", "33":"cyellow",
57 "32":"cgreen", "33":"cyellow",
58 "34":"cblue", "36":"ccyan",
58 "34":"cblue", "36":"ccyan",
59 "37":"cwhite", "01":"cbold"}
59 "37":"cwhite", "01":"cbold"}
60
60
61 //Fixes escaped console commands, IE colors. Turns them into HTML
61 //Fixes escaped console commands, IE colors. Turns them into HTML
62 function fixConsole(txt) {
62 function fixConsole(txt) {
63 txt = xmlencode(txt)
63 txt = xmlencode(txt)
64 var re = /\033\[([\d;]*?)m/
64 var re = /\033\[([\d;]*?)m/
65 var opened = false
65 var opened = false
66 var cmds = []
66 var cmds = []
67 var opener = ""
67 var opener = ""
68 var closer = ""
68 var closer = ""
69
69
70 while (re.test(txt)) {
70 while (re.test(txt)) {
71 var cmds = txt.match(re)[1].split(";")
71 var cmds = txt.match(re)[1].split(";")
72 closer = opened?"</span>":""
72 closer = opened?"</span>":""
73 opened = cmds.length > 1 || cmds[0] != 0
73 opened = cmds.length > 1 || cmds[0] != 0
74 var rep = []
74 var rep = []
75 for (var i in cmds)
75 for (var i in cmds)
76 if (typeof(attrib[cmds[i]]) != "undefined")
76 if (typeof(attrib[cmds[i]]) != "undefined")
77 rep.push(attrib[cmds[i]])
77 rep.push(attrib[cmds[i]])
78 opener = rep.length > 0?"<span class=\""+rep.join(" ")+"\">":""
78 opener = rep.length > 0?"<span class=\""+rep.join(" ")+"\">":""
79 txt = txt.replace(re, closer + opener)
79 txt = txt.replace(re, closer + opener)
80 }
80 }
81 if (opened) txt += "</span>"
81 if (opened) txt += "</span>"
82 return txt.trim()
82 return txt.trim()
83 }
83 }
84
84
85 //============================================================================
85 //============================================================================
86 // Notebook
86 // Notebook
87 //============================================================================
87 //============================================================================
88
88
89
89
90 var Notebook = function (selector) {
90 var Notebook = function (selector) {
91 this.element = $(selector);
91 this.element = $(selector);
92 this.element.scroll();
92 this.element.scroll();
93 this.element.data("notebook", this);
93 this.element.data("notebook", this);
94 this.next_prompt_number = 1;
94 this.next_prompt_number = 1;
95 this.kernel = null;
95 this.kernel = null;
96 this.msg_cell_map = {};
96 this.msg_cell_map = {};
97 this.bind_events();
97 this.bind_events();
98 this.start_kernel();
98 this.start_kernel();
99 };
99 };
100
100
101
101
102 Notebook.prototype.bind_events = function () {
102 Notebook.prototype.bind_events = function () {
103 var that = this;
103 var that = this;
104 $(document).keydown(function (event) {
104 $(document).keydown(function (event) {
105 console.log(event);
105 // console.log(event);
106 if (event.which == 38 && event.shiftKey) {
106 if (event.which == 38 && event.shiftKey) {
107 event.preventDefault();
107 event.preventDefault();
108 that.select_prev();
108 that.select_prev();
109 } else if (event.which == 40 && event.shiftKey) {
109 } else if (event.which == 40 && event.shiftKey) {
110 event.preventDefault();
110 event.preventDefault();
111 that.select_next();
111 that.select_next();
112 } else if (event.which == 13 && event.shiftKey) {
112 } else if (event.which == 13 && event.shiftKey) {
113 // The focus is not quite working here.
113 // The focus is not quite working here.
114 var cell = that.selected_cell();
114 var cell = that.selected_cell();
115 var cell_index = that.find_cell_index(cell);
115 var cell_index = that.find_cell_index(cell);
116 if (cell instanceof CodeCell) {
116 if (cell instanceof CodeCell) {
117 event.preventDefault();
117 event.preventDefault();
118 cell.clear_output();
118 cell.clear_output();
119 var msg_id = that.kernel.execute(cell.get_code());
119 var msg_id = that.kernel.execute(cell.get_code());
120 that.msg_cell_map[msg_id] = cell.cell_id;
120 that.msg_cell_map[msg_id] = cell.cell_id;
121 if (cell_index === (that.ncells()-1)) {
121 if (cell_index === (that.ncells()-1)) {
122 that.insert_code_cell_after();
122 that.insert_code_cell_after();
123 } else {
123 } else {
124 that.select(cell_index+1);
124 that.select(cell_index+1);
125 };
125 };
126 }
126 }
127 } else if (event.which == 9) {
127 } else if (event.which == 9) {
128 event.preventDefault();
128 event.preventDefault();
129 var cell = that.selected_cell();
129 var cell = that.selected_cell();
130 if (cell instanceof CodeCell) {
130 if (cell instanceof CodeCell) {
131 var ta = cell.element.find("textarea.input_area");
131 var ta = cell.element.find("textarea.input_area");
132 ta.val(ta.val() + " ");
132 ta.val(ta.val() + " ");
133 };
133 };
134 };
134 };
135 });
135 });
136 };
136 };
137
137
138
138
139 // Cell indexing, retrieval, etc.
139 // Cell indexing, retrieval, etc.
140
140
141
141
142 Notebook.prototype.cell_elements = function () {
142 Notebook.prototype.cell_elements = function () {
143 return this.element.children("div.cell");
143 return this.element.children("div.cell");
144 }
144 }
145
145
146
146
147 Notebook.prototype.ncells = function (cell) {
147 Notebook.prototype.ncells = function (cell) {
148 return this.cell_elements().length;
148 return this.cell_elements().length;
149 }
149 }
150
150
151
151
152 // TODO: we are often calling cells as cells()[i], which we should optimize
152 // TODO: we are often calling cells as cells()[i], which we should optimize
153 // to cells(i) or a new method.
153 // to cells(i) or a new method.
154 Notebook.prototype.cells = function () {
154 Notebook.prototype.cells = function () {
155 return this.cell_elements().toArray().map(function (e) {
155 return this.cell_elements().toArray().map(function (e) {
156 return $(e).data("cell");
156 return $(e).data("cell");
157 });
157 });
158 }
158 }
159
159
160
160
161 Notebook.prototype.find_cell_index = function (cell) {
161 Notebook.prototype.find_cell_index = function (cell) {
162 var result = null;
162 var result = null;
163 this.cell_elements().filter(function (index) {
163 this.cell_elements().filter(function (index) {
164 if ($(this).data("cell") === cell) {
164 if ($(this).data("cell") === cell) {
165 result = index;
165 result = index;
166 };
166 };
167 });
167 });
168 return result;
168 return result;
169 };
169 };
170
170
171
171
172 Notebook.prototype.index_or_selected = function (index) {
172 Notebook.prototype.index_or_selected = function (index) {
173 return index || this.selected_index() || 0;
173 return index || this.selected_index() || 0;
174 }
174 }
175
175
176
176
177 Notebook.prototype.select = function (index) {
177 Notebook.prototype.select = function (index) {
178 if (index !== undefined && index >= 0 && index < this.ncells()) {
178 if (index !== undefined && index >= 0 && index < this.ncells()) {
179 if (this.selected_index() !== null) {
179 if (this.selected_index() !== null) {
180 this.selected_cell().unselect();
180 this.selected_cell().unselect();
181 };
181 };
182 this.cells()[index].select();
182 this.cells()[index].select();
183 };
183 };
184 return this;
184 return this;
185 };
185 };
186
186
187
187
188 Notebook.prototype.select_next = function () {
188 Notebook.prototype.select_next = function () {
189 var index = this.selected_index();
189 var index = this.selected_index();
190 if (index !== null && index >= 0 && (index+1) < this.ncells()) {
190 if (index !== null && index >= 0 && (index+1) < this.ncells()) {
191 this.select(index+1);
191 this.select(index+1);
192 };
192 };
193 return this;
193 return this;
194 };
194 };
195
195
196
196
197 Notebook.prototype.select_prev = function () {
197 Notebook.prototype.select_prev = function () {
198 var index = this.selected_index();
198 var index = this.selected_index();
199 if (index !== null && index >= 0 && (index-1) < this.ncells()) {
199 if (index !== null && index >= 0 && (index-1) < this.ncells()) {
200 this.select(index-1);
200 this.select(index-1);
201 };
201 };
202 return this;
202 return this;
203 };
203 };
204
204
205
205
206 Notebook.prototype.selected_index = function () {
206 Notebook.prototype.selected_index = function () {
207 var result = null;
207 var result = null;
208 this.cell_elements().filter(function (index) {
208 this.cell_elements().filter(function (index) {
209 if ($(this).data("cell").selected === true) {
209 if ($(this).data("cell").selected === true) {
210 result = index;
210 result = index;
211 };
211 };
212 });
212 });
213 return result;
213 return result;
214 };
214 };
215
215
216
216
217 Notebook.prototype.cell_for_msg = function (msg_id) {
217 Notebook.prototype.cell_for_msg = function (msg_id) {
218 var cell_id = this.msg_cell_map[msg_id];
218 var cell_id = this.msg_cell_map[msg_id];
219 var result = null;
219 var result = null;
220 this.cell_elements().filter(function (index) {
220 this.cell_elements().filter(function (index) {
221 cell = $(this).data("cell");
221 cell = $(this).data("cell");
222 if (cell.cell_id === cell_id) {
222 if (cell.cell_id === cell_id) {
223 result = cell;
223 result = cell;
224 };
224 };
225 });
225 });
226 return result;
226 return result;
227 };
227 };
228
228
229
229
230 Notebook.prototype.selected_cell = function () {
230 Notebook.prototype.selected_cell = function () {
231 return this.cell_elements().eq(this.selected_index()).data("cell");
231 return this.cell_elements().eq(this.selected_index()).data("cell");
232 }
232 }
233
233
234
234
235 // Cell insertion, deletion and moving.
235 // Cell insertion, deletion and moving.
236
236
237
237
238 Notebook.prototype.delete_cell = function (index) {
238 Notebook.prototype.delete_cell = function (index) {
239 var i = index || this.selected_index();
239 var i = index || this.selected_index();
240 if (i !== null && i >= 0 && i < this.ncells()) {
240 if (i !== null && i >= 0 && i < this.ncells()) {
241 this.cell_elements().eq(i).remove();
241 this.cell_elements().eq(i).remove();
242 if (i === (this.ncells())) {
242 if (i === (this.ncells())) {
243 this.select(i-1);
243 this.select(i-1);
244 } else {
244 } else {
245 this.select(i);
245 this.select(i);
246 };
246 };
247 };
247 };
248 return this;
248 return this;
249 };
249 };
250
250
251
251
252 Notebook.prototype.append_cell = function (cell) {
252 Notebook.prototype.append_cell = function (cell) {
253 this.element.append(cell.element);
253 this.element.append(cell.element);
254 return this;
254 return this;
255 };
255 };
256
256
257
257
258 Notebook.prototype.insert_cell_after = function (cell, index) {
258 Notebook.prototype.insert_cell_after = function (cell, index) {
259 var ncells = this.ncells();
259 var ncells = this.ncells();
260 if (ncells === 0) {
260 if (ncells === 0) {
261 this.append_cell(cell);
261 this.append_cell(cell);
262 return this;
262 return this;
263 };
263 };
264 if (index >= 0 && index < ncells) {
264 if (index >= 0 && index < ncells) {
265 this.cell_elements().eq(index).after(cell.element);
265 this.cell_elements().eq(index).after(cell.element);
266 };
266 };
267 return this
267 return this
268 };
268 };
269
269
270
270
271 Notebook.prototype.insert_cell_before = function (cell, index) {
271 Notebook.prototype.insert_cell_before = function (cell, index) {
272 var ncells = this.ncells();
272 var ncells = this.ncells();
273 if (ncells === 0) {
273 if (ncells === 0) {
274 this.append_cell(cell);
274 this.append_cell(cell);
275 return this;
275 return this;
276 };
276 };
277 if (index >= 0 && index < ncells) {
277 if (index >= 0 && index < ncells) {
278 this.cell_elements().eq(index).before(cell.element);
278 this.cell_elements().eq(index).before(cell.element);
279 };
279 };
280 return this;
280 return this;
281 };
281 };
282
282
283
283
284 Notebook.prototype.move_cell_up = function (index) {
284 Notebook.prototype.move_cell_up = function (index) {
285 var i = index || this.selected_index();
285 var i = index || this.selected_index();
286 if (i !== null && i < this.ncells() && i > 0) {
286 if (i !== null && i < this.ncells() && i > 0) {
287 var pivot = this.cell_elements().eq(i-1);
287 var pivot = this.cell_elements().eq(i-1);
288 var tomove = this.cell_elements().eq(i);
288 var tomove = this.cell_elements().eq(i);
289 if (pivot !== null && tomove !== null) {
289 if (pivot !== null && tomove !== null) {
290 tomove.detach();
290 tomove.detach();
291 pivot.before(tomove);
291 pivot.before(tomove);
292 this.select(i-1);
292 this.select(i-1);
293 };
293 };
294 };
294 };
295 return this;
295 return this;
296 }
296 }
297
297
298
298
299 Notebook.prototype.move_cell_down = function (index) {
299 Notebook.prototype.move_cell_down = function (index) {
300 var i = index || this.selected_index();
300 var i = index || this.selected_index();
301 if (i !== null && i < (this.ncells()-1) && i >= 0) {
301 if (i !== null && i < (this.ncells()-1) && i >= 0) {
302 var pivot = this.cell_elements().eq(i+1)
302 var pivot = this.cell_elements().eq(i+1)
303 var tomove = this.cell_elements().eq(i)
303 var tomove = this.cell_elements().eq(i)
304 if (pivot !== null && tomove !== null) {
304 if (pivot !== null && tomove !== null) {
305 tomove.detach();
305 tomove.detach();
306 pivot.after(tomove);
306 pivot.after(tomove);
307 this.select(i+1);
307 this.select(i+1);
308 };
308 };
309 };
309 };
310 return this;
310 return this;
311 }
311 }
312
312
313
313
314 Notebook.prototype.sort_cells = function () {
314 Notebook.prototype.sort_cells = function () {
315 var ncells = this.ncells();
315 var ncells = this.ncells();
316 var sindex = this.selected_index();
316 var sindex = this.selected_index();
317 var swapped;
317 var swapped;
318 do {
318 do {
319 swapped = false
319 swapped = false
320 for (var i=1; i<ncells; i++) {
320 for (var i=1; i<ncells; i++) {
321 current = this.cell_elements().eq(i).data("cell");
321 current = this.cell_elements().eq(i).data("cell");
322 previous = this.cell_elements().eq(i-1).data("cell");
322 previous = this.cell_elements().eq(i-1).data("cell");
323 if (previous.input_prompt_number > current.input_prompt_number) {
323 if (previous.input_prompt_number > current.input_prompt_number) {
324 this.move_cell_up(i);
324 this.move_cell_up(i);
325 swapped = true;
325 swapped = true;
326 };
326 };
327 };
327 };
328 } while (swapped);
328 } while (swapped);
329 this.select(sindex);
329 this.select(sindex);
330 return this;
330 return this;
331 };
331 };
332
332
333
333
334 Notebook.prototype.insert_code_cell_before = function (index) {
334 Notebook.prototype.insert_code_cell_before = function (index) {
335 // TODO: Bounds check for i
335 // TODO: Bounds check for i
336 var i = this.index_or_selected(index);
336 var i = this.index_or_selected(index);
337 var cell = new CodeCell(this);
337 var cell = new CodeCell(this);
338 cell.set_input_prompt(this.next_prompt_number);
338 cell.set_input_prompt(this.next_prompt_number);
339 this.next_prompt_number = this.next_prompt_number + 1;
339 this.next_prompt_number = this.next_prompt_number + 1;
340 this.insert_cell_before(cell, i);
340 this.insert_cell_before(cell, i);
341 this.select(this.find_cell_index(cell));
341 this.select(this.find_cell_index(cell));
342 return this;
342 return this;
343 }
343 }
344
344
345
345
346 Notebook.prototype.insert_code_cell_after = function (index) {
346 Notebook.prototype.insert_code_cell_after = function (index) {
347 // TODO: Bounds check for i
347 // TODO: Bounds check for i
348 var i = this.index_or_selected(index);
348 var i = this.index_or_selected(index);
349 var cell = new CodeCell(this);
349 var cell = new CodeCell(this);
350 cell.set_input_prompt(this.next_prompt_number);
350 cell.set_input_prompt(this.next_prompt_number);
351 this.next_prompt_number = this.next_prompt_number + 1;
351 this.next_prompt_number = this.next_prompt_number + 1;
352 this.insert_cell_after(cell, i);
352 this.insert_cell_after(cell, i);
353 this.select(this.find_cell_index(cell));
353 this.select(this.find_cell_index(cell));
354 return this;
354 return this;
355 }
355 }
356
356
357
357
358 Notebook.prototype.insert_text_cell_before = function (index) {
358 Notebook.prototype.insert_text_cell_before = function (index) {
359 // TODO: Bounds check for i
359 // TODO: Bounds check for i
360 var i = this.index_or_selected(index);
360 var i = this.index_or_selected(index);
361 var cell = new TextCell(this);
361 var cell = new TextCell(this);
362 cell.config_mathjax();
362 cell.config_mathjax();
363 this.insert_cell_before(cell, i);
363 this.insert_cell_before(cell, i);
364 this.select(this.find_cell_index(cell));
364 this.select(this.find_cell_index(cell));
365 return this;
365 return this;
366 }
366 }
367
367
368
368
369 Notebook.prototype.insert_text_cell_after = function (index) {
369 Notebook.prototype.insert_text_cell_after = function (index) {
370 // TODO: Bounds check for i
370 // TODO: Bounds check for i
371 var i = this.index_or_selected(index);
371 var i = this.index_or_selected(index);
372 var cell = new TextCell(this);
372 var cell = new TextCell(this);
373 cell.config_mathjax();
373 cell.config_mathjax();
374 this.insert_cell_after(cell, i);
374 this.insert_cell_after(cell, i);
375 this.select(this.find_cell_index(cell));
375 this.select(this.find_cell_index(cell));
376 return this;
376 return this;
377 }
377 }
378
378
379
379
380 Notebook.prototype.text_to_code = function (index) {
380 Notebook.prototype.text_to_code = function (index) {
381 // TODO: Bounds check for i
381 // TODO: Bounds check for i
382 var i = this.index_or_selected(index);
382 var i = this.index_or_selected(index);
383 var source_element = this.cell_elements().eq(i);
383 var source_element = this.cell_elements().eq(i);
384 var source_cell = source_element.data("cell");
384 var source_cell = source_element.data("cell");
385 if (source_cell instanceof TextCell) {
385 if (source_cell instanceof TextCell) {
386 this.insert_code_cell_after(i);
386 this.insert_code_cell_after(i);
387 var target_cell = this.cells()[i+1];
387 var target_cell = this.cells()[i+1];
388 var text = source_element.find("textarea.text_cell_input").val();
388 var text = source_element.find("textarea.text_cell_input").val();
389 target_cell.element.find("textarea.input_area").val(text);
389 target_cell.element.find("textarea.input_area").val(text);
390 source_element.remove();
390 source_element.remove();
391 };
391 };
392 };
392 };
393
393
394
394
395 Notebook.prototype.code_to_text = function (index) {
395 Notebook.prototype.code_to_text = function (index) {
396 // TODO: Bounds check for i
396 // TODO: Bounds check for i
397 var i = this.index_or_selected(index);
397 var i = this.index_or_selected(index);
398 var source_element = this.cell_elements().eq(i);
398 var source_element = this.cell_elements().eq(i);
399 var source_cell = source_element.data("cell");
399 var source_cell = source_element.data("cell");
400 if (source_cell instanceof CodeCell) {
400 if (source_cell instanceof CodeCell) {
401 this.insert_text_cell_after(i);
401 this.insert_text_cell_after(i);
402 var target_cell = this.cells()[i+1];
402 var target_cell = this.cells()[i+1];
403 var text = source_element.find("textarea.input_area").val();
403 var text = source_element.find("textarea.input_area").val();
404 if (text === "") {text = target_cell.placeholder;};
404 if (text === "") {text = target_cell.placeholder;};
405 target_cell.element.find("textarea.text_cell_input").val(text);
405 target_cell.element.find("textarea.text_cell_input").val(text);
406 target_cell.element.find("textarea.text_cell_input").html(text);
406 target_cell.element.find("textarea.text_cell_input").html(text);
407 target_cell.element.find("div.text_cell_render").html(text);
407 target_cell.element.find("div.text_cell_render").html(text);
408
408
409 source_element.remove();
409 source_element.remove();
410 };
410 };
411 };
411 };
412
412
413
413
414 // Cell collapsing
414 // Cell collapsing
415
415
416 Notebook.prototype.collapse = function (index) {
416 Notebook.prototype.collapse = function (index) {
417 var i = this.index_or_selected(index);
417 var i = this.index_or_selected(index);
418 this.cells()[i].collapse();
418 this.cells()[i].collapse();
419 };
419 };
420
420
421
421
422 Notebook.prototype.expand = function (index) {
422 Notebook.prototype.expand = function (index) {
423 var i = this.index_or_selected(index);
423 var i = this.index_or_selected(index);
424 this.cells()[i].expand();
424 this.cells()[i].expand();
425 };
425 };
426
426
427
427
428 // Kernel related things
428 // Kernel related things
429
429
430 Notebook.prototype.start_kernel = function () {
430 Notebook.prototype.start_kernel = function () {
431 this.kernel = new Kernel();
431 this.kernel = new Kernel();
432 this.kernel.start_kernel(this._kernel_started, this);
432 this.kernel.start_kernel(this._kernel_started, this);
433 };
433 };
434
434
435
435
436 Notebook.prototype._kernel_started = function () {
436 Notebook.prototype._kernel_started = function () {
437 console.log("Kernel started: ", this.kernel.kernel_id);
437 console.log("Kernel started: ", this.kernel.kernel_id);
438 var that = this;
438 var that = this;
439
439
440 this.kernel.shell_channel.onmessage = function (e) {
440 this.kernel.shell_channel.onmessage = function (e) {
441 reply = $.parseJSON(e.data);
441 reply = $.parseJSON(e.data);
442 console.log(reply);
442 console.log(reply);
443 var msg_type = reply.msg_type;
443 var msg_type = reply.msg_type;
444 var cell = that.cell_for_msg(reply.parent_header.msg_id);
444 var cell = that.cell_for_msg(reply.parent_header.msg_id);
445 if (msg_type === "execute_reply") {
445 if (msg_type === "execute_reply") {
446 cell.set_prompt(reply.content.execution_count);
446 cell.set_prompt(reply.content.execution_count);
447 };
447 };
448 };
448 };
449
449
450 this.kernel.iopub_channel.onmessage = function (e) {
450 this.kernel.iopub_channel.onmessage = function (e) {
451 reply = $.parseJSON(e.data);
451 reply = $.parseJSON(e.data);
452 console.log(reply);
452 console.log(reply);
453 var msg_type = reply.msg_type;
453 var msg_type = reply.msg_type;
454 var cell = that.cell_for_msg(reply.parent_header.msg_id);
454 var cell = that.cell_for_msg(reply.parent_header.msg_id);
455 if (msg_type === "stream") {
455 if (msg_type === "stream") {
456 cell.expand();
456 cell.expand();
457 cell.append_stream(reply.content.data + "\n");
457 cell.append_stream(reply.content.data + "\n");
458 } else if (msg_type === "pyout" || msg_type === "display_data") {
458 } else if (msg_type === "pyout" || msg_type === "display_data") {
459 cell.expand();
459 cell.expand();
460 cell.append_display_data(reply.content.data);
460 cell.append_display_data(reply.content.data);
461 } else if (msg_type === "status") {
462 if (reply.content.execution_state === "busy") {
463 that.kernel.status_busy();
464 } else if (reply.content.execution_state === "idle") {
465 that.kernel.status_idle();
466 };
461 };
467 };
462 };
468 };
463 };
469 };
464
470
465
471
466 Notebook.prototype._handle_execute_reply = function (reply, cell) {
472 Notebook.prototype._handle_execute_reply = function (reply, cell) {
467 cell.set_prompt(reply.content.execution_count);
473 cell.set_prompt(reply.content.execution_count);
468 };
474 };
469
475
470
476
471 //============================================================================
477 //============================================================================
472 // Cell
478 // Cell
473 //============================================================================
479 //============================================================================
474
480
475
481
476 var Cell = function (notebook) {
482 var Cell = function (notebook) {
477 this.notebook = notebook;
483 this.notebook = notebook;
478 this.selected = false;
484 this.selected = false;
479 this.element;
485 this.element;
480 this.create_element();
486 this.create_element();
481 if (this.element !== undefined) {
487 if (this.element !== undefined) {
482 this.element.data("cell", this);
488 this.element.data("cell", this);
483 this.bind_events();
489 this.bind_events();
484 }
490 }
485 this.cell_id = uuid();
491 this.cell_id = uuid();
486 };
492 };
487
493
488
494
489 Cell.prototype.select = function () {
495 Cell.prototype.select = function () {
490 this.element.addClass('ui-widget-content ui-corner-all');
496 this.element.addClass('ui-widget-content ui-corner-all');
491 this.selected = true;
497 this.selected = true;
492 // TODO: we need t test across browsers to see if both of these are needed.
498 // TODO: we need t test across browsers to see if both of these are needed.
493 // In the meantime, there should not be any harm in having them both.
499 // In the meantime, there should not be any harm in having them both.
494 this.element.find('textarea').trigger('focusin');
500 this.element.find('textarea').trigger('focusin');
495 this.element.find('textarea').trigger('focus');
501 this.element.find('textarea').trigger('focus');
496 };
502 };
497
503
498
504
499 Cell.prototype.unselect = function () {
505 Cell.prototype.unselect = function () {
500 this.element.removeClass('ui-widget-content ui-corner-all');
506 this.element.removeClass('ui-widget-content ui-corner-all');
501 this.selected = false;
507 this.selected = false;
502 };
508 };
503
509
504
510
505 Cell.prototype.bind_events = function () {
511 Cell.prototype.bind_events = function () {
506 var that = this;
512 var that = this;
507 var nb = that.notebook
513 var nb = that.notebook
508 that.element.click(function (event) {
514 that.element.click(function (event) {
509 if (that.selected === false) {
515 if (that.selected === false) {
510 nb.select(nb.find_cell_index(that));
516 nb.select(nb.find_cell_index(that));
511 };
517 };
512 });
518 });
513 that.element.focusin(function (event) {
519 that.element.focusin(function (event) {
514 if (that.selected === false) {
520 if (that.selected === false) {
515 nb.select(nb.find_cell_index(that));
521 nb.select(nb.find_cell_index(that));
516 };
522 };
517 });
523 });
518 };
524 };
519
525
520
526
521 // Subclasses must implement create_element.
527 // Subclasses must implement create_element.
522 Cell.prototype.create_element = function () {};
528 Cell.prototype.create_element = function () {};
523
529
524
530
525 //============================================================================
531 //============================================================================
526 // CodeCell
532 // CodeCell
527 //============================================================================
533 //============================================================================
528
534
529
535
530 var CodeCell = function (notebook) {
536 var CodeCell = function (notebook) {
531 Cell.apply(this, arguments);
537 Cell.apply(this, arguments);
532 this.input_prompt_number = ' ';
538 this.input_prompt_number = ' ';
533 this.output_prompt_number = ' ';
539 this.output_prompt_number = ' ';
534 };
540 };
535
541
536
542
537 CodeCell.prototype = new Cell();
543 CodeCell.prototype = new Cell();
538
544
539
545
540 CodeCell.prototype.create_element = function () {
546 CodeCell.prototype.create_element = function () {
541 var cell = $('<div></div>').addClass('cell code_cell')
547 var cell = $('<div></div>').addClass('cell code_cell')
542 var input = $('<div></div>').addClass('input').append(
548 var input = $('<div></div>').addClass('input').append(
543 $('<div/>').addClass('prompt input_prompt')
549 $('<div/>').addClass('prompt input_prompt')
544 ).append(
550 ).append(
545 $('<textarea/>').addClass('input_area').
551 $('<textarea/>').addClass('input_area').
546 attr('rows',1).
552 attr('rows',1).
547 attr('cols',80).
553 attr('cols',80).
548 attr('wrap','hard').
554 attr('wrap','hard').
549 autoGrow()
555 autoGrow()
550 );
556 );
551 var output = $('<div></div>').addClass('output').append(
557 var output = $('<div></div>').addClass('output').append(
552 $('<div/>').addClass('prompt output_prompt')
558 $('<div/>').addClass('prompt output_prompt')
553 ).append(
559 ).append(
554 $('<div/>').addClass('output_area')
560 $('<div/>').addClass('output_area')
555 );
561 );
556 cell.append(input).append(output);
562 cell.append(input).append(output);
557 this.element = cell;
563 this.element = cell;
558 this.collapse()
564 this.collapse()
559 };
565 };
560
566
561
567
562 CodeCell.prototype.append_stream = function (data) {
568 CodeCell.prototype.append_stream = function (data) {
563 var data_list = data.split("\n");
569 var data_list = data.split("\n");
564 console.log(data_list);
570 console.log(data_list);
565 if (data_list.length > 0) {
571 if (data_list.length > 0) {
566 for (var i=0; i<data_list.length; i++) {
572 for (var i=0; i<data_list.length; i++) {
567 console.log(i, data_list[i]);
573 console.log(i, data_list[i]);
568 var toinsert = fixConsole(data_list[i]);
574 var toinsert = fixConsole(data_list[i]);
569 this.element.find("div.output_area").append($("<p>").append(toinsert));
575 this.element.find("div.output_area").append($("<p>").append(toinsert));
570 };
576 };
571 }
577 }
572 };
578 };
573
579
574
580
575 CodeCell.prototype.append_display_data = function (data) {
581 CodeCell.prototype.append_display_data = function (data) {
576 if (data["image/svg+xml"] !== undefined) {
582 if (data["image/svg+xml"] !== undefined) {
577 this.append_svg(data["image/svg+xml"]);
583 this.append_svg(data["image/svg+xml"]);
578 } else if (data["text/plain"] !== undefined) {
584 } else if (data["text/plain"] !== undefined) {
579 console.log(data["text/plain"]);
585 console.log(data["text/plain"]);
580 this.append_stream(data["text/plain"]);
586 this.append_stream(data["text/plain"]);
581 };
587 };
582 };
588 };
583
589
584 CodeCell.prototype.append_svg = function (svg) {
590 CodeCell.prototype.append_svg = function (svg) {
585 this.element.find("div.output_area").append(svg);
591 this.element.find("div.output_area").append(svg);
586 };
592 };
587
593
588
594
589 CodeCell.prototype.clear_output = function () {
595 CodeCell.prototype.clear_output = function () {
590 this.element.find("div.output_area").html("");
596 this.element.find("div.output_area").html("");
591 };
597 };
592
598
593
599
594 CodeCell.prototype.collapse = function () {
600 CodeCell.prototype.collapse = function () {
595 this.element.find('div.output').hide();
601 this.element.find('div.output').hide();
596 };
602 };
597
603
598
604
599 CodeCell.prototype.expand = function () {
605 CodeCell.prototype.expand = function () {
600 this.element.find('div.output').show();
606 this.element.find('div.output').show();
601 };
607 };
602
608
603
609
604 CodeCell.prototype.set_prompt = function (number) {
610 CodeCell.prototype.set_prompt = function (number) {
605 this.set_input_prompt(number);
611 this.set_input_prompt(number);
606 this.set_output_prompt(number);
612 this.set_output_prompt(number);
607 };
613 };
608
614
609 CodeCell.prototype.set_input_prompt = function (number) {
615 CodeCell.prototype.set_input_prompt = function (number) {
610 var n = number || ' ';
616 var n = number || ' ';
611 this.input_prompt_number = n
617 this.input_prompt_number = n
612 this.element.find('div.input_prompt').html('In&nbsp;[' + n + ']:');
618 this.element.find('div.input_prompt').html('In&nbsp;[' + n + ']:');
613 };
619 };
614
620
615
621
616 CodeCell.prototype.set_output_prompt = function (number) {
622 CodeCell.prototype.set_output_prompt = function (number) {
617 var n = number || ' ';
623 var n = number || ' ';
618 this.output_prompt_number = n
624 this.output_prompt_number = n
619 this.element.find('div.output_prompt').html('Out[' + n + ']:');
625 this.element.find('div.output_prompt').html('Out[' + n + ']:');
620 };
626 };
621
627
622
628
623 CodeCell.prototype.get_code = function () {
629 CodeCell.prototype.get_code = function () {
624 return this.element.find("textarea.input_area").val();
630 return this.element.find("textarea.input_area").val();
625 };
631 };
626
632
627 //============================================================================
633 //============================================================================
628 // TextCell
634 // TextCell
629 //============================================================================
635 //============================================================================
630
636
631
637
632 var TextCell = function (notebook) {
638 var TextCell = function (notebook) {
633 Cell.apply(this, arguments);
639 Cell.apply(this, arguments);
634 this.placeholder = "Type <strong>HTML</strong> and LaTeX: $\\alpha^2$"
640 this.placeholder = "Type <strong>HTML</strong> and LaTeX: $\\alpha^2$"
635 };
641 };
636
642
637
643
638 TextCell.prototype = new Cell();
644 TextCell.prototype = new Cell();
639
645
640
646
641 TextCell.prototype.create_element = function () {
647 TextCell.prototype.create_element = function () {
642 var cell = $("<div>").addClass('cell text_cell').
648 var cell = $("<div>").addClass('cell text_cell').
643 append(
649 append(
644 $("<textarea>" + this.placeholder + "</textarea>").
650 $("<textarea>" + this.placeholder + "</textarea>").
645 addClass('text_cell_input').
651 addClass('text_cell_input').
646 attr('rows',1).
652 attr('rows',1).
647 attr('cols',80).
653 attr('cols',80).
648 autoGrow()
654 autoGrow()
649 ).append(
655 ).append(
650 $('<div></div>').addClass('text_cell_render')
656 $('<div></div>').addClass('text_cell_render')
651 )
657 )
652 this.element = cell;
658 this.element = cell;
653 };
659 };
654
660
655
661
656 TextCell.prototype.select = function () {
662 TextCell.prototype.select = function () {
657 this.edit();
663 this.edit();
658 Cell.prototype.select.apply(this);
664 Cell.prototype.select.apply(this);
659 };
665 };
660
666
661
667
662 TextCell.prototype.edit = function () {
668 TextCell.prototype.edit = function () {
663 var text_cell = this.element;
669 var text_cell = this.element;
664 var input = text_cell.find("textarea.text_cell_input");
670 var input = text_cell.find("textarea.text_cell_input");
665 var output = text_cell.find("div.text_cell_render");
671 var output = text_cell.find("div.text_cell_render");
666 output.hide();
672 output.hide();
667 input.show().trigger('focus');
673 input.show().trigger('focus');
668 };
674 };
669
675
670
676
671 TextCell.prototype.render = function () {
677 TextCell.prototype.render = function () {
672 var text_cell = this.element;
678 var text_cell = this.element;
673 var input = text_cell.find("textarea.text_cell_input");
679 var input = text_cell.find("textarea.text_cell_input");
674 var output = text_cell.find("div.text_cell_render");
680 var output = text_cell.find("div.text_cell_render");
675 var text = input.val();
681 var text = input.val();
676 if (text === "") {
682 if (text === "") {
677 text = this.placeholder;
683 text = this.placeholder;
678 input.val(text);
684 input.val(text);
679 };
685 };
680 output.html(text)
686 output.html(text)
681 input.html(text);
687 input.html(text);
682 MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
688 MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
683 input.hide();
689 input.hide();
684 output.show();
690 output.show();
685 };
691 };
686
692
687
693
688 TextCell.prototype.config_mathjax = function () {
694 TextCell.prototype.config_mathjax = function () {
689 var text_cell = this.element;
695 var text_cell = this.element;
690 var that = this;
696 var that = this;
691 text_cell.click(function () {
697 text_cell.click(function () {
692 that.edit();
698 that.edit();
693 }).focusout(function () {
699 }).focusout(function () {
694 that.render();
700 that.render();
695 });
701 });
696
702
697 text_cell.trigger("focusout");
703 text_cell.trigger("focusout");
698 };
704 };
699
705
700
706
701 //============================================================================
707 //============================================================================
702 // On document ready
708 // On document ready
703 //============================================================================
709 //============================================================================
704
710
705
711
706 var Kernel = function () {
712 var Kernel = function () {
707 this.kernel_id = null;
713 this.kernel_id = null;
708 this.base_url = "/kernels";
714 this.base_url = "/kernels";
709 this.kernel_url = null;
715 this.kernel_url = null;
710 };
716 };
711
717
712
718
713 Kernel.prototype.get_msg = function (msg_type, content) {
719 Kernel.prototype.get_msg = function (msg_type, content) {
714 var msg = {
720 var msg = {
715 header : {
721 header : {
716 msg_id : uuid(),
722 msg_id : uuid(),
717 username : "bgranger",
723 username : "bgranger",
718 session: this.session_id
724 session: this.session_id
719 },
725 },
720 msg_type : msg_type,
726 msg_type : msg_type,
721 content : content,
727 content : content,
722 parent_header : {}
728 parent_header : {}
723 };
729 };
724 return msg;
730 return msg;
725 }
731 }
726
732
727 Kernel.prototype.start_kernel = function (callback, context) {
733 Kernel.prototype.start_kernel = function (callback, context) {
728 var that = this;
734 var that = this;
729 $.post(this.base_url,
735 $.post(this.base_url,
730 function (kernel_id) {
736 function (kernel_id) {
731 that._handle_start_kernel(kernel_id, callback, context);
737 that._handle_start_kernel(kernel_id, callback, context);
732 },
738 },
733 'json'
739 'json'
734 );
740 );
735 };
741 };
736
742
737
743
738 Kernel.prototype._handle_start_kernel = function (kernel_id, callback, context) {
744 Kernel.prototype._handle_start_kernel = function (kernel_id, callback, context) {
739 this.kernel_id = kernel_id;
745 this.kernel_id = kernel_id;
740 this.kernel_url = this.base_url + "/" + this.kernel_id;
746 this.kernel_url = this.base_url + "/" + this.kernel_id;
741 this._start_channels();
747 this._start_channels();
742 callback.call(context);
748 callback.call(context);
743 };
749 };
744
750
745
751
746 Kernel.prototype._start_channels = function () {
752 Kernel.prototype._start_channels = function () {
747 var ws_url = "ws://127.0.0.1:8888" + this.kernel_url;
753 var ws_url = "ws://127.0.0.1:8888" + this.kernel_url;
748 this.shell_channel = new WebSocket(ws_url + "/shell");
754 this.shell_channel = new WebSocket(ws_url + "/shell");
749 this.iopub_channel = new WebSocket(ws_url + "/iopub");
755 this.iopub_channel = new WebSocket(ws_url + "/iopub");
750 }
756 }
751
757
752
758
753 Kernel.prototype.execute = function (code) {
759 Kernel.prototype.execute = function (code) {
754 var content = {
760 var content = {
755 code : code,
761 code : code,
756 silent : false,
762 silent : false,
757 user_variables : [],
763 user_variables : [],
758 user_expressions : {}
764 user_expressions : {}
759 };
765 };
760 var msg = this.get_msg("execute_request", content);
766 var msg = this.get_msg("execute_request", content);
761 this.shell_channel.send(JSON.stringify(msg));
767 this.shell_channel.send(JSON.stringify(msg));
762 return msg.header.msg_id;
768 return msg.header.msg_id;
763 }
769 }
764
770
765
771
766 Kernel.prototype.interrupt = function () {
772 Kernel.prototype.interrupt = function () {
767 $.post(this.kernel_url + "/interrupt");
773 $.post(this.kernel_url + "/interrupt");
768 };
774 };
769
775
770
776
771 Kernel.prototype.restart = function () {
777 Kernel.prototype.restart = function () {
778 this.status_restarting();
772 url = this.kernel_url + "/restart"
779 url = this.kernel_url + "/restart"
773 var that = this;
780 var that = this;
774 $.post(url, function (kernel_id) {
781 $.post(url, function (kernel_id) {
775 console.log("Kernel restarted: " + kernel_id);
782 console.log("Kernel restarted: " + kernel_id);
776 that.kernel_id = kernel_id;
783 that.kernel_id = kernel_id;
777 that.kernel_url = that.base_url + "/" + that.kernel_id;
784 that.kernel_url = that.base_url + "/" + that.kernel_id;
785 that.status_idle();
778 }, 'json');
786 }, 'json');
779 };
787 };
780
788
781
789
790 Kernel.prototype.status_busy = function () {
791 $("#kernel_status").removeClass("status_idle");
792 $("#kernel_status").removeClass("status_restarting");
793 $("#kernel_status").addClass("status_busy");
794 $("#kernel_status").text("Busy");
795 };
796
797
798 Kernel.prototype.status_idle = function () {
799 $("#kernel_status").removeClass("status_busy");
800 $("#kernel_status").removeClass("status_restarting");
801 $("#kernel_status").addClass("status_idle");
802 $("#kernel_status").text("Idle");
803 };
804
805 Kernel.prototype.status_restarting = function () {
806 $("#kernel_status").removeClass("status_busy");
807 $("#kernel_status").removeClass("status_idle");
808 $("#kernel_status").addClass("status_restarting");
809 $("#kernel_status").text("Restarting");
810 };
811
782 //============================================================================
812 //============================================================================
783 // On document ready
813 // On document ready
784 //============================================================================
814 //============================================================================
785
815
786
816
787 $(document).ready(function () {
817 $(document).ready(function () {
788
818
789 MathJax.Hub.Config({
819 MathJax.Hub.Config({
790 tex2jax: {
820 tex2jax: {
791 inlineMath: [ ['$','$'], ["\\(","\\)"] ],
821 inlineMath: [ ['$','$'], ["\\(","\\)"] ],
792 displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
822 displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
793 }
823 }
794 });
824 });
795
825
796 IPYTHON.notebook = new Notebook('div.notebook');
826 IPYTHON.notebook = new Notebook('div.notebook');
797 IPYTHON.notebook.insert_code_cell_after();
827 IPYTHON.notebook.insert_code_cell_after();
798
828
799 $("#menu_tabs").tabs();
829 $("#menu_tabs").tabs();
800
830
801 $("#help_toolbar").buttonset();
831 $("#help_toolbar").buttonset();
802
832
803 $("#kernel_toolbar").buttonset();
833 $("#kernel_toolbar").buttonset();
804 $("#interrupt_kernel").click(function () {IPYTHON.notebook.kernel.interrupt();});
834 $("#interrupt_kernel").click(function () {IPYTHON.notebook.kernel.interrupt();});
805 $("#restart_kernel").click(function () {IPYTHON.notebook.kernel.restart();});
835 $("#restart_kernel").click(function () {IPYTHON.notebook.kernel.restart();});
836 $("#kernel_status").addClass("status_idle");
806
837
807 $("#move_cell").buttonset();
838 $("#move_cell").buttonset();
808 $("#move_up").button("option", "icons", {primary:"ui-icon-arrowthick-1-n"});
839 $("#move_up").button("option", "icons", {primary:"ui-icon-arrowthick-1-n"});
809 $("#move_up").button("option", "text", false);
840 $("#move_up").button("option", "text", false);
810 $("#move_up").click(function () {IPYTHON.notebook.move_cell_up();});
841 $("#move_up").click(function () {IPYTHON.notebook.move_cell_up();});
811 $("#move_down").button("option", "icons", {primary:"ui-icon-arrowthick-1-s"});
842 $("#move_down").button("option", "icons", {primary:"ui-icon-arrowthick-1-s"});
812 $("#move_down").button("option", "text", false);
843 $("#move_down").button("option", "text", false);
813 $("#move_down").click(function () {IPYTHON.notebook.move_cell_down();});
844 $("#move_down").click(function () {IPYTHON.notebook.move_cell_down();});
814
845
815 $("#insert_delete").buttonset();
846 $("#insert_delete").buttonset();
816 $("#insert_cell_before").click(function () {IPYTHON.notebook.insert_code_cell_before();});
847 $("#insert_cell_before").click(function () {IPYTHON.notebook.insert_code_cell_before();});
817 $("#insert_cell_after").click(function () {IPYTHON.notebook.insert_code_cell_after();});
848 $("#insert_cell_after").click(function () {IPYTHON.notebook.insert_code_cell_after();});
818 $("#delete_cell").button("option", "icons", {primary:"ui-icon-closethick"});
849 $("#delete_cell").button("option", "icons", {primary:"ui-icon-closethick"});
819 $("#delete_cell").button("option", "text", false);
850 $("#delete_cell").button("option", "text", false);
820 $("#delete_cell").click(function () {IPYTHON.notebook.delete_cell();});
851 $("#delete_cell").click(function () {IPYTHON.notebook.delete_cell();});
821
852
822 $("#cell_type").buttonset();
853 $("#cell_type").buttonset();
823 $("#to_code").click(function () {IPYTHON.notebook.text_to_code();});
854 $("#to_code").click(function () {IPYTHON.notebook.text_to_code();});
824 $("#to_text").click(function () {IPYTHON.notebook.code_to_text();});
855 $("#to_text").click(function () {IPYTHON.notebook.code_to_text();});
825
856
826 $("#sort").buttonset();
857 $("#sort").buttonset();
827 $("#sort_cells").click(function () {IPYTHON.notebook.sort_cells();});
858 $("#sort_cells").click(function () {IPYTHON.notebook.sort_cells();});
828
859
829 $("#toggle").buttonset();
860 $("#toggle").buttonset();
830 $("#collapse").click(function () {IPYTHON.notebook.collapse();});
861 $("#collapse").click(function () {IPYTHON.notebook.collapse();});
831 $("#expand").click(function () {IPYTHON.notebook.expand();});
862 $("#expand").click(function () {IPYTHON.notebook.expand();});
832
863
833 }); No newline at end of file
864 });
@@ -1,87 +1,88 b''
1 <!DOCTYPE HTML>
1 <!DOCTYPE HTML>
2 <html>
2 <html>
3
3
4 <head>
4 <head>
5 <meta charset="utf-8">
5 <meta charset="utf-8">
6
6
7 <title>IPython Notebook</title>
7 <title>IPython Notebook</title>
8
8
9 <link rel="stylesheet" href="static/css/notebook.css" type="text/css" />
9 <link rel="stylesheet" href="static/css/notebook.css" type="text/css" />
10
10
11 <link rel="stylesheet" href="static/jquery/css/themes/aristo/jquery-wijmo.css" type="text/css" />
11 <link rel="stylesheet" href="static/jquery/css/themes/aristo/jquery-wijmo.css" type="text/css" />
12 <!-- <link rel="stylesheet" href="static/jquery/css/themes/rocket/jquery-wijmo.css" type="text/css" /> -->
12 <!-- <link rel="stylesheet" href="static/jquery/css/themes/rocket/jquery-wijmo.css" type="text/css" /> -->
13 <!-- <link rel="stylesheet" href="static/jquery/css/themes/smoothness/jquery-ui-1.8.11.custom.css" type="text/css" /> -->
13 <!-- <link rel="stylesheet" href="static/jquery/css/themes/smoothness/jquery-ui-1.8.11.custom.css" type="text/css" /> -->
14
14
15 <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" charset="utf-8"></script>
15 <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" charset="utf-8"></script>
16
16
17 </head>
17 </head>
18
18
19 <body>
19 <body>
20
20
21 <div id="header">
21 <div id="header">
22 <span id="ipython_notebook"><h1>IPython Notebook</h1></span>
22 <span id="ipython_notebook"><h1>IPython Notebook</h1></span>
23 </div>
23 </div>
24
24
25
25
26 <div id="tools">
26 <div id="tools">
27
27
28 <div id="menu_tabs">
28 <div id="menu_tabs">
29 <span id="kernel_status">Idle</span>
29 <ul>
30 <ul>
30 <li><a href="#cell_tab">Cell</a></li>
31 <li><a href="#cell_tab">Cell</a></li>
31 <li><a href="#kernel_tab">Kernel</a></li>
32 <li><a href="#kernel_tab">Kernel</a></li>
32 <li><a href="#help_tab">Help</a></li>
33 <li><a href="#help_tab">Help</a></li>
33 </ul>
34 </ul>
34 <div id="cell_tab">
35 <div id="cell_tab">
35 <span id="cell_toolbar">
36 <span id="cell_toolbar">
36 <span id="move_cell">
37 <span id="move_cell">
37 <button id="move_up">Move up</button>
38 <button id="move_up">Move up</button>
38 <button id="move_down">Move down</button>
39 <button id="move_down">Move down</button>
39 </span>
40 </span>
40 <span id="insert_delete">
41 <span id="insert_delete">
41 <button id="insert_cell_before">Before</button>
42 <button id="insert_cell_before">Before</button>
42 <button id="insert_cell_after">After</button>
43 <button id="insert_cell_after">After</button>
43 <button id="delete_cell">Delete</button>
44 <button id="delete_cell">Delete</button>
44 </span>
45 </span>
45 <span id="cell_type">
46 <span id="cell_type">
46 <button id="to_code">Code</button>
47 <button id="to_code">Code</button>
47 <button id="to_text">Text</button>
48 <button id="to_text">Text</button>
48 </span>
49 </span>
49 <span id="sort">
50 <span id="sort">
50 <button id="sort_cells">Sort</button>
51 <button id="sort_cells">Sort</button>
51 </span>
52 </span>
52 <span id="toggle">
53 <span id="toggle">
53 <button id="collapse">Collapse</button>
54 <button id="collapse">Collapse</button>
54 <button id="expand">Expand</button>
55 <button id="expand">Expand</button>
55 </span>
56 </span>
56 </span>
57 </span>
57 </div>
58 </div>
58 <div id="kernel_tab">
59 <div id="kernel_tab">
59 <span id="kernel_toolbar">
60 <span id="kernel_toolbar">
60 <button id="interrupt_kernel">Interrupt</button>
61 <button id="interrupt_kernel">Interrupt</button>
61 <button id="restart_kernel">Restart</button>
62 <button id="restart_kernel">Restart</button>
62 </span>
63 </span>
63 </div>
64 </div>
64 <div id="help_tab">
65 <div id="help_tab">
65 <span id="help_toolbar">
66 <span id="help_toolbar">
66 <button><a href="http://docs.python.org" target="_blank">Python</a></button>
67 <button><a href="http://docs.python.org" target="_blank">Python</a></button>
67 <button><a href="http://ipython.github.com/ipython-doc/dev/index.html" target="_blank">IPython</a></button>
68 <button><a href="http://ipython.github.com/ipython-doc/dev/index.html" target="_blank">IPython</a></button>
68 <button><a href="http://matplotlib.sourceforge.net/" target="_blank">Matplotlib</a></button>
69 <button><a href="http://matplotlib.sourceforge.net/" target="_blank">Matplotlib</a></button>
69 <button><a href="http://docs.scipy.org/doc/numpy/reference/" target="_blank">NumPy</a></button>
70 <button><a href="http://docs.scipy.org/doc/numpy/reference/" target="_blank">NumPy</a></button>
70 <button><a href="http://docs.scipy.org/doc/scipy/reference/" target="_blank">SciPy</a></button>
71 <button><a href="http://docs.scipy.org/doc/scipy/reference/" target="_blank">SciPy</a></button>
71 <button><a href="http://docs.sympy.org/dev/index.html" target="_blank">SymPy</a></button>
72 <button><a href="http://docs.sympy.org/dev/index.html" target="_blank">SymPy</a></button>
72 </span>
73 </span>
73 </div>
74 </div>
74 </div>
75 </div>
75
76
76 </div>
77 </div>
77
78
78 <div class="notebook"></div>
79 <div class="notebook"></div>
79
80
80 <script src="static/jquery/js/jquery-1.5.1.min.js" type="text/javascript" charset="utf-8"></script>
81 <script src="static/jquery/js/jquery-1.5.1.min.js" type="text/javascript" charset="utf-8"></script>
81 <script src="static/jquery/js/jquery-ui-1.8.11.custom.min.js" type="text/javascript" charset="utf-8"></script>
82 <script src="static/jquery/js/jquery-ui-1.8.11.custom.min.js" type="text/javascript" charset="utf-8"></script>
82 <script src="static/jquery/js/jquery.autogrow.js" type="text/javascript" charset="utf-8"></script>
83 <script src="static/jquery/js/jquery.autogrow.js" type="text/javascript" charset="utf-8"></script>
83 <script src="static/js/notebook.js" type="text/javascript" charset="utf-8"></script>
84 <script src="static/js/notebook.js" type="text/javascript" charset="utf-8"></script>
84
85
85 </body>
86 </body>
86
87
87 </html> No newline at end of file
88 </html>
General Comments 0
You need to be logged in to leave comments. Login now