Show More
@@ -1,186 +1,195 | |||
|
1 | 1 | // Copyright (c) IPython Development Team. |
|
2 | 2 | // Distributed under the terms of the Modified BSD License. |
|
3 | 3 | |
|
4 | 4 | define([ |
|
5 | 5 | 'base/js/namespace', |
|
6 | 6 | 'jquery', |
|
7 | 7 | 'base/js/utils', |
|
8 | 8 | 'services/kernels/js/kernel', |
|
9 | 9 | ], function(IPython, $, utils, kernel) { |
|
10 | 10 | "use strict"; |
|
11 | 11 | |
|
12 | 12 | var Session = function (options) { |
|
13 | 13 | this.id = null; |
|
14 | this.notebook = { | |
|
14 | this.notebook_model = { | |
|
15 | 15 | name: options.notebook_name, |
|
16 | 16 | path: options.notebook_path |
|
17 | 17 | }; |
|
18 | this.kernel = { | |
|
18 | this.kernel_model = { | |
|
19 | id: null, | |
|
19 | 20 | name: options.kernel_name |
|
20 | 21 | }; |
|
21 | 22 | |
|
22 | 23 | this.base_url = options.base_url; |
|
23 | 24 | this.ws_url = options.ws_url; |
|
24 | 25 | this.sessions_url = utils.url_join_encode(this.base_url, 'api/sessions'); |
|
25 | 26 | |
|
26 |
this.notebook |
|
|
27 | this.notebook = options.notebook; | |
|
28 | this.kernel = null; | |
|
27 | 29 | this.events = options.events; |
|
28 | 30 | }; |
|
29 | 31 | |
|
30 | 32 | /** |
|
31 | 33 | * GET /api/sessions |
|
32 | 34 | */ |
|
33 | 35 | Session.prototype.list = function (success, error) { |
|
34 | 36 | $.ajax(this.sessions_url, { |
|
35 | 37 | processData: false, |
|
36 | 38 | cache: false, |
|
37 | 39 | type: "GET", |
|
38 | 40 | dataType: "json", |
|
39 | 41 | success: success, |
|
40 | 42 | error: this._on_error(error) |
|
41 | 43 | }); |
|
42 | 44 | }; |
|
43 | 45 | |
|
44 | 46 | /** |
|
45 | 47 | * POST /api/sessions |
|
46 | 48 | */ |
|
47 | 49 | Session.prototype.start = function (success, error) { |
|
48 | 50 | var that = this; |
|
49 | 51 | var on_success = function (data, status, xhr) { |
|
50 | 52 | var kernel_service_url = utils.url_path_join(that.base_url, "api/kernels"); |
|
51 | that.kernel = new kernel.Kernel(kernel_service_url, that.ws_url, that.notebook_obj, that.kernel_name); | |
|
53 | that.kernel = new kernel.Kernel( | |
|
54 | kernel_service_url, that.ws_url, that.notebook, | |
|
55 | that.kernel_model.id, that.kernel_model.name); | |
|
52 | 56 | that.kernel._kernel_started(data.kernel); |
|
53 | 57 | if (success) { |
|
54 | 58 | success(data, status, xhr); |
|
55 | 59 | } |
|
56 | 60 | }; |
|
57 | 61 | var on_error = function (xhr, status, err) { |
|
58 | 62 | that.events.trigger('status_dead.Kernel'); |
|
59 | 63 | if (error) { |
|
60 | 64 | error(xhr, status, err); |
|
61 | 65 | } |
|
62 | 66 | }; |
|
63 | 67 | |
|
64 | 68 | $.ajax(this.sessions_url, { |
|
65 | 69 | processData: false, |
|
66 | 70 | cache: false, |
|
67 | 71 | type: "POST", |
|
68 | 72 | data: JSON.stringify(this._get_model()), |
|
69 | 73 | dataType: "json", |
|
70 | 74 | success: this._on_success(on_success), |
|
71 | 75 | error: this._on_error(on_error) |
|
72 | 76 | }); |
|
73 | 77 | }; |
|
74 | 78 | |
|
75 | 79 | /** |
|
76 | 80 | * GET /api/sessions/[:session_id] |
|
77 | 81 | */ |
|
78 | 82 | Session.prototype.get_info = function (success, error) { |
|
79 | 83 | var url = utils.url_join_encode(this.sessions_url, this.id); |
|
80 | 84 | $.ajax(url, { |
|
81 | 85 | processData: false, |
|
82 | 86 | cache: false, |
|
83 | 87 | type: "GET", |
|
84 | 88 | dataType: "json", |
|
85 | 89 | success: this._on_success(success), |
|
86 | 90 | error: this._on_error(error) |
|
87 | 91 | }); |
|
88 | 92 | }; |
|
89 | 93 | |
|
90 | 94 | /** |
|
91 | 95 | * PATCH /api/sessions/[:session_id] |
|
92 | 96 | */ |
|
93 | 97 | Session.prototype.change = function (notebook_name, notebook_path, kernel_name, success, error) { |
|
94 | this.notebook.name = notebook_name; | |
|
95 | this.notebook.path = notebook_path; | |
|
96 | this.kernel.name = kernel_name; | |
|
98 | this.notebook_model.name = notebook_name; | |
|
99 | this.notebook_model.path = notebook_path; | |
|
100 | this.kernel_model.name = kernel_name; | |
|
97 | 101 | |
|
98 | 102 | var url = utils.url_join_encode(this.sessions_url, this.id); |
|
99 | 103 | $.ajax(url, { |
|
100 | 104 | processData: false, |
|
101 | 105 | cache: false, |
|
102 | 106 | type: "PATCH", |
|
103 | 107 | data: JSON.stringify(this._get_model()), |
|
104 | 108 | dataType : "json", |
|
105 | 109 | success: this._on_success(success), |
|
106 | 110 | error: this._on_error(error) |
|
107 | 111 | }); |
|
108 | 112 | }; |
|
109 | 113 | |
|
110 | 114 | Session.prototype.rename_notebook = function (name, path, success, error) { |
|
111 | this.change(name, path, this.kernel.name, success, error); | |
|
115 | this.change(name, path, this.kernel_model.name, success, error); | |
|
112 | 116 | }; |
|
113 | 117 | |
|
114 | 118 | /** |
|
115 | 119 | * DELETE /api/sessions/[:session_id] |
|
116 | 120 | */ |
|
117 |
Session.prototype. |
|
|
118 | if (this.kernel) { | |
|
119 | this.kernel.running = false; | |
|
120 |
|
|
|
121 | Session.prototype.delete = function (success, error) { | |
|
122 | var that = this; | |
|
123 | var on_success = function (data, status, xhr) { | |
|
124 | if (that.kernel) { | |
|
125 | that.kernel._kernel_dead(); | |
|
121 | 126 | } |
|
127 | }; | |
|
122 | 128 | |
|
123 | 129 | var url = utils.url_join_encode(this.sessions_url, this.id); |
|
124 | 130 | $.ajax(url, { |
|
125 | 131 | processData: false, |
|
126 | 132 | cache: false, |
|
127 | 133 | type: "DELETE", |
|
128 | 134 | dataType: "json", |
|
129 | success: this._on_success(success), | |
|
135 | success: this._on_success(on_success), | |
|
130 | 136 | error: this._on_error(error) |
|
131 | 137 | }); |
|
132 | 138 | }; |
|
133 | 139 | |
|
134 | 140 | Session.prototype._get_model = function () { |
|
135 | 141 | return { |
|
136 | notebook: this.notebook, | |
|
137 | kernel: this.kernel | |
|
142 | notebook: this.notebook_model, | |
|
143 | kernel: this.kernel_model | |
|
138 | 144 | }; |
|
139 | 145 | }; |
|
140 | 146 | |
|
141 | 147 | Session.prototype._update_model = function (data) { |
|
148 | if (data && data.id) { | |
|
142 | 149 | this.id = data.id; |
|
143 | if (data.notebook) { | |
|
144 | this.notebook.name = data.notebook.name; | |
|
145 | this.notebook.path = data.notebook.path; | |
|
146 | 150 | } |
|
147 |
if (data. |
|
|
148 |
this. |
|
|
151 | if (data && data.notebook) { | |
|
152 | this.notebook_model.name = data.notebook.name; | |
|
153 | this.notebook_model.path = data.notebook.path; | |
|
154 | } | |
|
155 | if (data && data.kernel) { | |
|
156 | this.kernel_model.name = data.kernel.name; | |
|
157 | this.kernel_model.id = data.kernel.id; | |
|
149 | 158 | } |
|
150 | 159 | }; |
|
151 | 160 | |
|
152 | 161 | Session.prototype._on_success = function (success) { |
|
153 | 162 | var that = this; |
|
154 | 163 | return function (data, status, xhr) { |
|
155 | 164 | that._update_model(data); |
|
156 | 165 | if (success) { |
|
157 | 166 | success(data, status, xhr); |
|
158 | 167 | } |
|
159 | 168 | }; |
|
160 | 169 | }; |
|
161 | 170 | |
|
162 | 171 | Session.prototype._on_error = function (error) { |
|
163 | 172 | return function (xhr, status, err) { |
|
164 | 173 | utils.log_ajax_error(xhr, status, err); |
|
165 | 174 | if (error) { |
|
166 | 175 | error(xhr, status, err); |
|
167 | 176 | } |
|
168 | 177 | }; |
|
169 | 178 | }; |
|
170 | 179 | |
|
171 | 180 | |
|
172 | 181 | var SessionAlreadyStarting = function (message) { |
|
173 | 182 | this.name = "SessionAlreadyStarting"; |
|
174 | 183 | this.message = (message || ""); |
|
175 | 184 | }; |
|
176 | 185 | |
|
177 | 186 | SessionAlreadyStarting.prototype = Error.prototype; |
|
178 | 187 | |
|
179 | 188 | // For backwards compatability. |
|
180 | 189 | IPython.Session = Session; |
|
181 | 190 | |
|
182 | 191 | return { |
|
183 | 192 | Session: Session, |
|
184 | 193 | SessionAlreadyStarting: SessionAlreadyStarting |
|
185 | 194 | }; |
|
186 | 195 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now