##// END OF EJS Templates
add stdin to notebook...
MinRK -
Show More
@@ -28,6 +28,7 b' var IPython = (function (IPython) {'
28 this.kernel_id = null;
28 this.kernel_id = null;
29 this.shell_channel = null;
29 this.shell_channel = null;
30 this.iopub_channel = null;
30 this.iopub_channel = null;
31 this.stdin_channel = null;
31 this.base_url = base_url;
32 this.base_url = base_url;
32 this.running = false;
33 this.running = false;
33 this.username = "username";
34 this.username = "username";
@@ -127,6 +128,7 b' var IPython = (function (IPython) {'
127 var ws_url = this.ws_url + this.kernel_url;
128 var ws_url = this.ws_url + this.kernel_url;
128 console.log("Starting WebSockets:", ws_url);
129 console.log("Starting WebSockets:", ws_url);
129 this.shell_channel = new this.WebSocket(ws_url + "/shell");
130 this.shell_channel = new this.WebSocket(ws_url + "/shell");
131 this.stdin_channel = new this.WebSocket(ws_url + "/stdin");
130 this.iopub_channel = new this.WebSocket(ws_url + "/iopub");
132 this.iopub_channel = new this.WebSocket(ws_url + "/iopub");
131 send_cookie = function(){
133 send_cookie = function(){
132 this.send(that.session_id + ':' + document.cookie);
134 this.send(that.session_id + ':' + document.cookie);
@@ -150,21 +152,22 b' var IPython = (function (IPython) {'
150 that._websocket_closed(ws_url, false);
152 that._websocket_closed(ws_url, false);
151 }
153 }
152 };
154 };
153 this.shell_channel.onopen = send_cookie;
155 var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel];
154 this.shell_channel.onclose = ws_closed_early;
156 for (var i=0; i < channels.length; i++) {
155 this.iopub_channel.onopen = send_cookie;
157 channels[i].onopen = send_cookie;
156 this.iopub_channel.onclose = ws_closed_early;
158 channels[i].onclose = ws_closed_early;
159 }
157 // switch from early-close to late-close message after 1s
160 // switch from early-close to late-close message after 1s
158 setTimeout(function() {
161 setTimeout(function() {
159 if (that.shell_channel !== null) {
162 for (var i=0; i < channels.length; i++) {
160 that.shell_channel.onclose = ws_closed_late;
163 if (channels[i] !== null) {
161 }
164 channels[i].onclose = ws_closed_late;
162 if (that.iopub_channel !== null) {
165 }
163 that.iopub_channel.onclose = ws_closed_late;
164 }
166 }
165 }, 1000);
167 }, 1000);
166 this.shell_channel.onmessage = $.proxy(this._handle_shell_reply,this);
168 this.shell_channel.onmessage = $.proxy(this._handle_shell_reply, this);
167 this.iopub_channel.onmessage = $.proxy(this._handle_iopub_reply,this);
169 this.iopub_channel.onmessage = $.proxy(this._handle_iopub_reply, this);
170 this.stdin_channel.onmessage = $.proxy(this._handle_input_request, this);
168 };
171 };
169
172
170 /**
173 /**
@@ -172,16 +175,14 b' var IPython = (function (IPython) {'
172 * @method stop_channels
175 * @method stop_channels
173 */
176 */
174 Kernel.prototype.stop_channels = function () {
177 Kernel.prototype.stop_channels = function () {
175 if (this.shell_channel !== null) {
178 var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel];
176 this.shell_channel.onclose = function (evt) {};
179 for (var i=0; i < channels.length; i++) {
177 this.shell_channel.close();
180 if ( channels[i] !== null ) {
178 this.shell_channel = null;
181 channels[i].onclose = function (evt) {};
179 };
182 channels[i].close();
180 if (this.iopub_channel !== null) {
183 }
181 this.iopub_channel.onclose = function (evt) {};
182 this.iopub_channel.close();
183 this.iopub_channel = null;
184 };
184 };
185 this.shell_channel = this.iopub_channel = this.stdin_channel = null;
185 };
186 };
186
187
187 // Main public methods.
188 // Main public methods.
@@ -282,7 +283,7 b' var IPython = (function (IPython) {'
282 silent : true,
283 silent : true,
283 user_variables : [],
284 user_variables : [],
284 user_expressions : {},
285 user_expressions : {},
285 allow_stdin : false
286 allow_stdin : true
286 };
287 };
287 $.extend(true, content, options)
288 $.extend(true, content, options)
288 $([IPython.events]).trigger('execution_request.Kernel', {kernel: this, content:content});
289 $([IPython.events]).trigger('execution_request.Kernel', {kernel: this, content:content});
@@ -343,8 +344,19 b' var IPython = (function (IPython) {'
343 };
344 };
344 };
345 };
345
346
347 Kernel.prototype.send_input_reply = function (input, header) {
346
348
347 // Reply handlers.
349 var content = {
350 value : input,
351 };
352 $([IPython.events]).trigger('input_reply.Kernel', {kernel: this, content:content});
353 var msg = this._get_msg("input_reply", content);
354 this.stdin_channel.send(JSON.stringify(msg));
355 return msg.header.msg_id;
356 };
357
358
359 // Reply handlers
348
360
349 Kernel.prototype.get_callbacks_for_msg = function (msg_id) {
361 Kernel.prototype.get_callbacks_for_msg = function (msg_id) {
350 var callbacks = this._msg_callbacks[msg_id];
362 var callbacks = this._msg_callbacks[msg_id];
@@ -433,6 +445,21 b' var IPython = (function (IPython) {'
433 };
445 };
434
446
435
447
448 Kernel.prototype._handle_input_request = function (e) {
449 var request = $.parseJSON(e.data);
450 console.log("input", request);
451 var header = request.header;
452 var content = request.content;
453 var metadata = request.metadata;
454 var msg_type = header.msg_type;
455 if (msg_type !== 'input_request') {
456 console.log("Invalid input request!", request);
457 return;
458 }
459 $([IPython.events]).trigger('input_request.Kernel', {kernel: this, request:request});
460 };
461
462
436 IPython.Kernel = Kernel;
463 IPython.Kernel = Kernel;
437
464
438 return IPython;
465 return IPython;
@@ -115,6 +115,34 b' var IPython = (function (IPython) {'
115 var index = that.find_cell_index(data.cell);
115 var index = that.find_cell_index(data.cell);
116 that.select(index);
116 that.select(index);
117 });
117 });
118 $([IPython.events]).on('input_request.Kernel', function (event, data) {
119 var dialog = $('<div/>').attr('id','input_form').append(
120 $('<form/>')
121 .attr("action", "javascript:$('#input_form').parent().find('button').click();")
122 .append(
123 $('<input/>')
124 .attr('id', 'input_prompt_dialog')
125 .attr('type', 'text')
126 .attr('name', 'input')
127 ));
128 $(document).append(dialog);
129 dialog.dialog({
130 resizable: false,
131 modal: true,
132 title: data.request.content.prompt,
133 closeText: '',
134 buttons : {
135 "Okay": function () {
136 IPython.notebook.kernel.send_input_reply(
137 $("input#input_prompt_dialog").attr('value'),
138 data.request.header
139 );
140 $(this).dialog('close');
141 dialog.remove();
142 }
143 }
144 });
145 });
118
146
119
147
120 $(document).keydown(function (event) {
148 $(document).keydown(function (event) {
General Comments 0
You need to be logged in to leave comments. Login now