##// END OF EJS Templates
Clean up session class
Jessica B. Hamrick -
Show More
@@ -9,129 +9,166 b' define(['
9 ], function(IPython, $, utils, kernel) {
9 ], function(IPython, $, utils, kernel) {
10 "use strict";
10 "use strict";
11
11
12 var Session = function(options){
12 var Session = function (options) {
13 this.kernel = null;
14 this.id = null;
13 this.id = null;
15 this.notebook = options.notebook;
14 this.notebook = {
16 this.events = options.notebook.events;
15 name: options.notebook_name,
17 this.name = options.notebook_name;
16 path: options.notebook_path
18 this.path = options.notebook_path;
17 };
19 this.kernel_name = options.kernel_name;
18 this.kernel = {
19 name: options.kernel_name
20 };
21
20 this.base_url = options.base_url;
22 this.base_url = options.base_url;
21 this.ws_url = options.ws_url;
23 this.ws_url = options.ws_url;
24 this.sessions_url = utils.url_join_encode(this.base_url, 'api/sessions');
25
26 this.notebook_obj = options.notebook;
27 this.events = options.events;
22 };
28 };
23
29
30 /**
31 * GET /api/sessions
32 */
33 Session.prototype.list = function (success, error) {
34 $.ajax(this.sessions_url, {
35 processData: false,
36 cache: false,
37 type: "GET",
38 dataType: "json",
39 success: success,
40 error: this._on_error(error)
41 });
42 };
43
44 /**
45 * POST /api/sessions
46 */
24 Session.prototype.start = function (success, error) {
47 Session.prototype.start = function (success, error) {
25 var that = this;
48 var that = this;
26 var model = {
49 var on_success = function (data, status, xhr) {
27 notebook : {
50 var kernel_service_url = utils.url_path_join(that.base_url, "api/kernels");
28 name : this.name,
51 that.kernel = new kernel.Kernel(kernel_service_url, that.ws_url, that.notebook_obj, that.kernel_name);
29 path : this.path
52 that.kernel._kernel_started(data.kernel);
30 },
53 if (success) {
31 kernel : {
54 success(data, status, xhr);
32 name : this.kernel_name
33 }
55 }
34 };
56 };
35 var settings = {
57 var on_error = function (xhr, status, err) {
36 processData : false,
58 that.events.trigger('status_dead.Kernel');
37 cache : false,
59 if (error) {
38 type : "POST",
60 error(xhr, status, err);
39 data: JSON.stringify(model),
40 dataType : "json",
41 success : function (data, status, xhr) {
42 that._handle_start_success(data);
43 if (success) {
44 success(data, status, xhr);
45 }
46 },
47 error : function (xhr, status, err) {
48 that._handle_start_failure(xhr, status, err);
49 if (error !== undefined) {
50 error(xhr, status, err);
51 }
52 utils.log_ajax_error(xhr, status, err);
53 }
61 }
54 };
62 };
55 var url = utils.url_join_encode(this.base_url, 'api/sessions');
63
56 $.ajax(url, settings);
64 $.ajax(this.sessions_url, {
65 processData: false,
66 cache: false,
67 type: "POST",
68 data: JSON.stringify(this._get_model()),
69 dataType: "json",
70 success: this._on_success(on_success),
71 error: this._on_error(on_error)
72 });
57 };
73 };
58
74
59 Session.prototype.rename_notebook = function (name, path) {
75 /**
60 this.name = name;
76 * GET /api/sessions/[:session_id]
61 this.path = path;
77 */
62 var model = {
78 Session.prototype.get_info = function (success, error) {
63 notebook : {
79 var url = utils.url_join_encode(this.sessions_url, this.id);
64 name : this.name,
80 $.ajax(url, {
65 path : this.path
81 processData: false,
66 }
82 cache: false,
67 };
83 type: "GET",
68 var settings = {
84 dataType: "json",
69 processData : false,
85 success: this._on_success(success),
70 cache : false,
86 error: this._on_error(error)
71 type : "PATCH",
87 });
72 data: JSON.stringify(model),
73 dataType : "json",
74 error : utils.log_ajax_error,
75 };
76 var url = utils.url_join_encode(this.base_url, 'api/sessions', this.id);
77 $.ajax(url, settings);
78 };
88 };
79
89
80 Session.prototype.delete = function (success, error) {
90 /**
81 var settings = {
91 * PATCH /api/sessions/[:session_id]
82 processData : false,
92 */
83 cache : false,
93 Session.prototype.change = function (notebook_name, notebook_path, kernel_name, success, error) {
84 type : "DELETE",
94 this.notebook.name = notebook_name;
95 this.notebook.path = notebook_path;
96 this.kernel.name = kernel_name;
97
98 var url = utils.url_join_encode(this.sessions_url, this.id);
99 $.ajax(url, {
100 processData: false,
101 cache: false,
102 type: "PATCH",
103 data: JSON.stringify(this._get_model()),
85 dataType : "json",
104 dataType : "json",
86 success : success,
105 success: this._on_success(success),
87 error : error || utils.log_ajax_error,
106 error: this._on_error(error)
88 };
107 });
108 };
109
110 Session.prototype.rename_notebook = function (name, path, success, error) {
111 this.change(name, path, this.kernel.name, success, error);
112 };
113
114 /**
115 * DELETE /api/sessions/[:session_id]
116 */
117 Session.prototype.kill = function (success, error) {
89 if (this.kernel) {
118 if (this.kernel) {
90 this.kernel.running = false;
119 this.kernel.running = false;
91 this.kernel.stop_channels();
120 this.kernel.stop_channels();
92 }
121 }
93 var url = utils.url_join_encode(this.base_url, 'api/sessions', this.id);
122
94 $.ajax(url, settings);
123 var url = utils.url_join_encode(this.sessions_url, this.id);
124 $.ajax(url, {
125 processData: false,
126 cache: false,
127 type: "DELETE",
128 dataType: "json",
129 success: this._on_success(success),
130 error: this._on_error(error)
131 });
95 };
132 };
96
133
97 // Kernel related things
134 Session.prototype._get_model = function () {
98 /**
135 return {
99 * Create the Kernel object associated with this Session.
136 notebook: this.notebook,
100 *
137 kernel: this.kernel
101 * @method _handle_start_success
138 };
102 */
103 Session.prototype._handle_start_success = function (data, status, xhr) {
104 this.id = data.id;
105 // If we asked for 'python', the response will have 'python3' or 'python2'.
106 this.kernel_name = data.kernel.name;
107 this.events.trigger('started.Session', this);
108 var kernel_service_url = utils.url_path_join(this.base_url, "api/kernels");
109 this.kernel = new kernel.Kernel(kernel_service_url, this.ws_url, this.notebook, this.kernel_name);
110 this.kernel._kernel_started(data.kernel);
111 };
139 };
112
140
113 Session.prototype._handle_start_failure = function (xhr, status, error) {
141 Session.prototype._update_model = function (data) {
114 this.events.trigger('start_failed.Session', [this, xhr, status, error]);
142 this.id = data.id;
115 };
143 if (data.notebook) {
116
144 this.notebook.name = data.notebook.name;
117 /**
145 this.notebook.path = data.notebook.path;
118 * Prompt the user to restart the IPython kernel.
146 }
119 *
147 if (data.kernel) {
120 * @method restart_kernel
148 this.kernel.name = data.kernel.name;
121 */
149 }
122 Session.prototype.restart_kernel = function () {
123 this.kernel.restart();
124 };
150 };
125
151
126 Session.prototype.interrupt_kernel = function() {
152 Session.prototype._on_success = function (success) {
127 this.kernel.interrupt();
153 var that = this;
154 return function (data, status, xhr) {
155 that._update_model(data);
156 if (success) {
157 success(data, status, xhr);
158 }
159 };
128 };
160 };
129
130
161
131 Session.prototype.kill_kernel = function() {
162 Session.prototype._on_error = function (error) {
132 this.kernel.kill();
163 return function (xhr, status, err) {
164 utils.log_ajax_error(xhr, status, err);
165 if (error) {
166 error(xhr, status, err);
167 }
168 };
133 };
169 };
134
170
171
135 var SessionAlreadyStarting = function (message) {
172 var SessionAlreadyStarting = function (message) {
136 this.name = "SessionAlreadyStarting";
173 this.name = "SessionAlreadyStarting";
137 this.message = (message || "");
174 this.message = (message || "");
@@ -144,6 +181,6 b' define(['
144
181
145 return {
182 return {
146 Session: Session,
183 Session: Session,
147 SessionAlreadyStarting: SessionAlreadyStarting,
184 SessionAlreadyStarting: SessionAlreadyStarting
148 };
185 };
149 });
186 });
General Comments 0
You need to be logged in to leave comments. Login now