-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
add stdin to the notebook #3089
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1b0c2f7
8fae131
24dcb6b
97db8db
8b1382e
ed8a7ce
5631528
5a8093e
2142444
59f59aa
641fc2f
5d6d133
5b087cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ var IPython = (function (IPython) { | |
this.kernel_id = null; | ||
this.shell_channel = null; | ||
this.iopub_channel = null; | ||
this.stdin_channel = null; | ||
this.base_url = base_url; | ||
this.running = false; | ||
this.username = "username"; | ||
|
@@ -127,9 +128,12 @@ var IPython = (function (IPython) { | |
var ws_url = this.ws_url + this.kernel_url; | ||
console.log("Starting WebSockets:", ws_url); | ||
this.shell_channel = new this.WebSocket(ws_url + "/shell"); | ||
this.stdin_channel = new this.WebSocket(ws_url + "/stdin"); | ||
this.iopub_channel = new this.WebSocket(ws_url + "/iopub"); | ||
send_cookie = function(){ | ||
this.send(document.cookie); | ||
// send the session id so the Session object Python-side | ||
// has the same identity | ||
this.send(that.session_id + ':' + document.cookie); | ||
}; | ||
var already_called_onclose = false; // only alert once | ||
var ws_closed_early = function(evt){ | ||
|
@@ -150,38 +154,41 @@ var IPython = (function (IPython) { | |
that._websocket_closed(ws_url, false); | ||
} | ||
}; | ||
this.shell_channel.onopen = send_cookie; | ||
this.shell_channel.onclose = ws_closed_early; | ||
this.iopub_channel.onopen = send_cookie; | ||
this.iopub_channel.onclose = ws_closed_early; | ||
var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel]; | ||
for (var i=0; i < channels.length; i++) { | ||
channels[i].onopen = send_cookie; | ||
channels[i].onclose = ws_closed_early; | ||
} | ||
// switch from early-close to late-close message after 1s | ||
setTimeout(function() { | ||
if (that.shell_channel !== null) { | ||
that.shell_channel.onclose = ws_closed_late; | ||
} | ||
if (that.iopub_channel !== null) { | ||
that.iopub_channel.onclose = ws_closed_late; | ||
for (var i=0; i < channels.length; i++) { | ||
if (channels[i] !== null) { | ||
channels[i].onclose = ws_closed_late; | ||
} | ||
} | ||
}, 1000); | ||
this.shell_channel.onmessage = $.proxy(this._handle_shell_reply,this); | ||
this.iopub_channel.onmessage = $.proxy(this._handle_iopub_reply,this); | ||
this.shell_channel.onmessage = $.proxy(this._handle_shell_reply, this); | ||
this.iopub_channel.onmessage = $.proxy(this._handle_iopub_reply, this); | ||
this.stdin_channel.onmessage = $.proxy(this._handle_input_request, this); | ||
|
||
$([IPython.events]).on('send_input_reply.Kernel', function(evt, data) { | ||
that.send_input_reply(data); | ||
}); | ||
}; | ||
|
||
/** | ||
* Start the `shell`and `iopub` channels. | ||
* @method stop_channels | ||
*/ | ||
Kernel.prototype.stop_channels = function () { | ||
if (this.shell_channel !== null) { | ||
this.shell_channel.onclose = function (evt) {}; | ||
this.shell_channel.close(); | ||
this.shell_channel = null; | ||
}; | ||
if (this.iopub_channel !== null) { | ||
this.iopub_channel.onclose = function (evt) {}; | ||
this.iopub_channel.close(); | ||
this.iopub_channel = null; | ||
var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel]; | ||
for (var i=0; i < channels.length; i++) { | ||
if ( channels[i] !== null ) { | ||
channels[i].onclose = function (evt) {}; | ||
channels[i].close(); | ||
} | ||
}; | ||
this.shell_channel = this.iopub_channel = this.stdin_channel = null; | ||
}; | ||
|
||
// Main public methods. | ||
|
@@ -284,6 +291,9 @@ var IPython = (function (IPython) { | |
user_expressions : {}, | ||
allow_stdin : false | ||
}; | ||
if (callbacks.input_request !== undefined) { | ||
content.allow_stdin = true; | ||
} | ||
$.extend(true, content, options) | ||
$([IPython.events]).trigger('execution_request.Kernel', {kernel: this, content:content}); | ||
var msg = this._get_msg("execute_request", content); | ||
|
@@ -343,8 +353,18 @@ var IPython = (function (IPython) { | |
}; | ||
}; | ||
|
||
Kernel.prototype.send_input_reply = function (input) { | ||
var content = { | ||
value : input, | ||
}; | ||
$([IPython.events]).trigger('input_reply.Kernel', {kernel: this, content:content}); | ||
var msg = this._get_msg("input_reply", content); | ||
this.stdin_channel.send(JSON.stringify(msg)); | ||
return msg.header.msg_id; | ||
}; | ||
|
||
|
||
// Reply handlers. | ||
// Reply handlers | ||
|
||
Kernel.prototype.get_callbacks_for_msg = function (msg_id) { | ||
var callbacks = this._msg_callbacks[msg_id]; | ||
|
@@ -433,6 +453,26 @@ var IPython = (function (IPython) { | |
}; | ||
|
||
|
||
Kernel.prototype._handle_input_request = function (e) { | ||
var request = $.parseJSON(e.data); | ||
var header = request.header; | ||
var content = request.content; | ||
var metadata = request.metadata; | ||
var msg_type = header.msg_type; | ||
if (msg_type !== 'input_request') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then again, maybe "input" is better as it is the name of the message. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is a request, it has a reply, just like execute, etc. |
||
console.log("Invalid input request!", request); | ||
return; | ||
} | ||
var callbacks = this.get_callbacks_for_msg(request.parent_header.msg_id); | ||
if (callbacks !== undefined) { | ||
var cb = callbacks[msg_type]; | ||
if (cb !== undefined) { | ||
cb(content, metadata); | ||
} | ||
}; | ||
}; | ||
|
||
|
||
IPython.Kernel = Kernel; | ||
|
||
return IPython; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe use
stdin
rather thaninput
to be consistent in the naming?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also below...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's called an input_request, that's the message type. See our message spec