##// END OF EJS Templates
Better alert message if no WebSockets are detected.
Brian E. Granger -
Show More
@@ -1,156 +1,156
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2008-2011 The IPython Development Team
2 // Copyright (C) 2008-2011 The IPython Development Team
3 //
3 //
4 // Distributed under the terms of the BSD License. The full license is in
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
6 //----------------------------------------------------------------------------
7
7
8 //============================================================================
8 //============================================================================
9 // Kernel
9 // Kernel
10 //============================================================================
10 //============================================================================
11
11
12 var IPython = (function (IPython) {
12 var IPython = (function (IPython) {
13
13
14 var utils = IPython.utils;
14 var utils = IPython.utils;
15
15
16 var Kernel = function () {
16 var Kernel = function () {
17 this.kernel_id = null;
17 this.kernel_id = null;
18 this.base_url = "/kernels";
18 this.base_url = "/kernels";
19 this.kernel_url = null;
19 this.kernel_url = null;
20 this.shell_channel = null;
20 this.shell_channel = null;
21 this.iopub_channel = null;
21 this.iopub_channel = null;
22 this.running = false;
22 this.running = false;
23 if (typeof(WebSocket) !== 'undefined') {
23 if (typeof(WebSocket) !== 'undefined') {
24 this.WebSocket = WebSocket
24 this.WebSocket = WebSocket
25 } else if (typeof(MozWebSocket) !== 'undefined') {
25 } else if (typeof(MozWebSocket) !== 'undefined') {
26 this.WebSocket = MozWebSocket
26 this.WebSocket = MozWebSocket
27 } else {
27 } else {
28 alert('Your browser does not have WebSocket support, please try Chrome, Safari or Firefox 6');
28 alert('Your browser does not have WebSocket support, please try Chrome, Safari or Firefox 6. Firefox 4 and 5 are also supported by you have to enable WebSockets in about:config.');
29 };
29 };
30 };
30 };
31
31
32
32
33 Kernel.prototype.get_msg = function (msg_type, content) {
33 Kernel.prototype.get_msg = function (msg_type, content) {
34 var msg = {
34 var msg = {
35 header : {
35 header : {
36 msg_id : utils.uuid(),
36 msg_id : utils.uuid(),
37 username : "username",
37 username : "username",
38 session: this.session_id,
38 session: this.session_id,
39 msg_type : msg_type
39 msg_type : msg_type
40 },
40 },
41 content : content,
41 content : content,
42 parent_header : {}
42 parent_header : {}
43 };
43 };
44 return msg;
44 return msg;
45 }
45 }
46
46
47 Kernel.prototype.start = function (notebook_id, callback) {
47 Kernel.prototype.start = function (notebook_id, callback) {
48 var that = this;
48 var that = this;
49 if (!this.running) {
49 if (!this.running) {
50 var qs = $.param({notebook:notebook_id});
50 var qs = $.param({notebook:notebook_id});
51 $.post(this.base_url + '?' + qs,
51 $.post(this.base_url + '?' + qs,
52 function (kernel_id) {
52 function (kernel_id) {
53 that._handle_start_kernel(kernel_id, callback);
53 that._handle_start_kernel(kernel_id, callback);
54 },
54 },
55 'json'
55 'json'
56 );
56 );
57 };
57 };
58 };
58 };
59
59
60
60
61 Kernel.prototype.restart = function (callback) {
61 Kernel.prototype.restart = function (callback) {
62 IPython.kernel_status_widget.status_restarting();
62 IPython.kernel_status_widget.status_restarting();
63 var url = this.kernel_url + "/restart";
63 var url = this.kernel_url + "/restart";
64 var that = this;
64 var that = this;
65 if (this.running) {
65 if (this.running) {
66 this.stop_channels();
66 this.stop_channels();
67 $.post(url,
67 $.post(url,
68 function (kernel_id) {
68 function (kernel_id) {
69 that._handle_start_kernel(kernel_id, callback);
69 that._handle_start_kernel(kernel_id, callback);
70 },
70 },
71 'json'
71 'json'
72 );
72 );
73 };
73 };
74 };
74 };
75
75
76
76
77 Kernel.prototype._handle_start_kernel = function (json, callback) {
77 Kernel.prototype._handle_start_kernel = function (json, callback) {
78 this.running = true;
78 this.running = true;
79 this.kernel_id = json.kernel_id;
79 this.kernel_id = json.kernel_id;
80 this.ws_url = json.ws_url;
80 this.ws_url = json.ws_url;
81 this.kernel_url = this.base_url + "/" + this.kernel_id;
81 this.kernel_url = this.base_url + "/" + this.kernel_id;
82 this.start_channels();
82 this.start_channels();
83 callback();
83 callback();
84 IPython.kernel_status_widget.status_idle();
84 IPython.kernel_status_widget.status_idle();
85 };
85 };
86
86
87
87
88 Kernel.prototype.start_channels = function () {
88 Kernel.prototype.start_channels = function () {
89 this.stop_channels();
89 this.stop_channels();
90 var ws_url = this.ws_url + this.kernel_url;
90 var ws_url = this.ws_url + this.kernel_url;
91 console.log("Starting WS:", ws_url);
91 console.log("Starting WS:", ws_url);
92 this.shell_channel = new this.WebSocket(ws_url + "/shell");
92 this.shell_channel = new this.WebSocket(ws_url + "/shell");
93 this.iopub_channel = new this.WebSocket(ws_url + "/iopub");
93 this.iopub_channel = new this.WebSocket(ws_url + "/iopub");
94 };
94 };
95
95
96
96
97 Kernel.prototype.stop_channels = function () {
97 Kernel.prototype.stop_channels = function () {
98 if (this.shell_channel !== null) {
98 if (this.shell_channel !== null) {
99 this.shell_channel.close();
99 this.shell_channel.close();
100 this.shell_channel = null;
100 this.shell_channel = null;
101 };
101 };
102 if (this.iopub_channel !== null) {
102 if (this.iopub_channel !== null) {
103 this.iopub_channel.close();
103 this.iopub_channel.close();
104 this.iopub_channel = null;
104 this.iopub_channel = null;
105 };
105 };
106 };
106 };
107
107
108 Kernel.prototype.execute = function (code) {
108 Kernel.prototype.execute = function (code) {
109 var content = {
109 var content = {
110 code : code,
110 code : code,
111 silent : false,
111 silent : false,
112 user_variables : [],
112 user_variables : [],
113 user_expressions : {}
113 user_expressions : {}
114 };
114 };
115 var msg = this.get_msg("execute_request", content);
115 var msg = this.get_msg("execute_request", content);
116 this.shell_channel.send(JSON.stringify(msg));
116 this.shell_channel.send(JSON.stringify(msg));
117 return msg.header.msg_id;
117 return msg.header.msg_id;
118 }
118 }
119
119
120
120
121 Kernel.prototype.complete = function (line, cursor_pos) {
121 Kernel.prototype.complete = function (line, cursor_pos) {
122 var content = {
122 var content = {
123 text : '',
123 text : '',
124 line : line,
124 line : line,
125 cursor_pos : cursor_pos
125 cursor_pos : cursor_pos
126 };
126 };
127 var msg = this.get_msg("complete_request", content);
127 var msg = this.get_msg("complete_request", content);
128 this.shell_channel.send(JSON.stringify(msg));
128 this.shell_channel.send(JSON.stringify(msg));
129 return msg.header.msg_id;
129 return msg.header.msg_id;
130 }
130 }
131
131
132
132
133 Kernel.prototype.interrupt = function () {
133 Kernel.prototype.interrupt = function () {
134 if (this.running) {
134 if (this.running) {
135 $.post(this.kernel_url + "/interrupt");
135 $.post(this.kernel_url + "/interrupt");
136 };
136 };
137 };
137 };
138
138
139
139
140 Kernel.prototype.kill = function () {
140 Kernel.prototype.kill = function () {
141 if (this.running) {
141 if (this.running) {
142 this.running = false;
142 this.running = false;
143 var settings = {
143 var settings = {
144 cache : false,
144 cache : false,
145 type : "DELETE",
145 type : "DELETE",
146 };
146 };
147 $.ajax(this.kernel_url, settings);
147 $.ajax(this.kernel_url, settings);
148 };
148 };
149 };
149 };
150
150
151 IPython.Kernel = Kernel;
151 IPython.Kernel = Kernel;
152
152
153 return IPython;
153 return IPython;
154
154
155 }(IPython));
155 }(IPython));
156
156
General Comments 0
You need to be logged in to leave comments. Login now