##// END OF EJS Templates
fixed shutdown button refresh on dashboard
Zachary Sailer -
Show More
@@ -1,105 +1,107 b''
1 """Tornado handlers for the notebooks web service.
1 """Tornado handlers for the notebooks web service.
2
2
3 Authors:
3 Authors:
4
4
5 * Zach Sailer
5 * Zach Sailer
6 """
6 """
7
7
8 #-----------------------------------------------------------------------------
8 #-----------------------------------------------------------------------------
9 # Copyright (C) 2008-2011 The IPython Development Team
9 # Copyright (C) 2008-2011 The IPython Development Team
10 #
10 #
11 # Distributed under the terms of the BSD License. The full license is in
11 # Distributed under the terms of the BSD License. The full license is in
12 # the file COPYING, distributed as part of this software.
12 # the file COPYING, distributed as part of this software.
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 # Imports
16 # Imports
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18
18
19 from tornado import web
19 from tornado import web
20
20
21 from zmq.utils import jsonapi
21 from zmq.utils import jsonapi
22
22
23 from IPython.utils.jsonutil import date_default
23 from IPython.utils.jsonutil import date_default
24
24
25 from ...base.handlers import IPythonHandler, authenticate_unless_readonly
25 from ...base.handlers import IPythonHandler, authenticate_unless_readonly
26
26
27 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
28 # Session web service handlers
28 # Session web service handlers
29 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30
30
31
31
32
32
33 class SessionRootHandler(IPythonHandler):
33 class SessionRootHandler(IPythonHandler):
34
34
35
35
36 @authenticate_unless_readonly
36 @authenticate_unless_readonly
37 def get(self):
37 def get(self):
38 sm = self.session_manager
38 sm = self.session_manager
39 nbm = self.notebook_manager
39 nbm = self.notebook_manager
40 km = self.kernel_manager
40 km = self.kernel_manager
41 sessions = sm.list_sessions()
41 sessions = sm.list_sessions()
42 self.finish(jsonapi.dumps(sessions))
42 self.finish(jsonapi.dumps(sessions))
43
43
44
44
45 @web.authenticated
45 @web.authenticated
46 def post(self):
46 def post(self):
47 sm = self.session_manager
47 sm = self.session_manager
48 nbm = self.notebook_manager
48 nbm = self.notebook_manager
49 km = self.kernel_manager
49 km = self.kernel_manager
50 notebook_path = self.get_argument('notebook_path', default=None)
50 notebook_path = self.get_argument('notebook_path', default=None)
51 notebook_name, path = nbm.named_notebook_path(notebook_path)
51 notebook_name, path = nbm.named_notebook_path(notebook_path)
52 session_id, model = sm.get_session(notebook_name, path)
52 session_id, model = sm.get_session(notebook_name, path)
53 if model == None:
53 if model == None:
54 kernel_id = km.start_kernel()
54 kernel_id = km.start_kernel()
55 kernel = km.kernel_model(kernel_id, self.ws_url)
55 kernel = km.kernel_model(kernel_id, self.ws_url)
56 model = sm.session_model(session_id, notebook_name, path, kernel)
56 model = sm.session_model(session_id, notebook_name, path, kernel)
57 self.finish(jsonapi.dumps(model))
57 self.finish(jsonapi.dumps(model))
58
58
59
59
60 class SessionHandler(IPythonHandler):
60 class SessionHandler(IPythonHandler):
61
61
62 @web.authenticated
62 @web.authenticated
63 def get(self, session_id):
63 def get(self, session_id):
64 sm = self.session_manager
64 sm = self.session_manager
65 model = sm.get_session_from_id(session_id)
65 model = sm.get_session_from_id(session_id)
66 self.finish(jsonapi.dumps(model))
66 self.finish(jsonapi.dumps(model))
67
67
68
68
69 @authenticate_unless_readonly
69 @authenticate_unless_readonly
70 def put(self, session_id):
70 def put(self, session_id):
71 sm = self.session_manager
71 sm = self.session_manager
72 nbm = self.notebook_manager
72 nbm = self.notebook_manager
73 km = self.kernel_manager
73 km = self.kernel_manager
74 notebook_path = self.get_argument('notebook_path', default=None)
74 notebook_path = self.get_argument('notebook_path', default=None)
75 notebook_name, path = nbm.named_notebook_path(notebook_path)
75 notebook_name, path = nbm.named_notebook_path(notebook_path)
76 kernel_id = sm.get_kernel_from_session(session_id)
76 kernel_id = sm.get_kernel_from_session(session_id)
77 kernel = km.kernel_model(kernel_id, self.ws_url)
77 kernel = km.kernel_model(kernel_id, self.ws_url)
78 sm.delete_mapping_for_session(session_id)
78 sm.delete_mapping_for_session(session_id)
79 model = sm.session_model(session_id, notebook_name, path, kernel)
79 model = sm.session_model(session_id, notebook_name, path, kernel)
80 return model
80 self.finish(jsonapi.dumps(model))
81
81
82 @web.authenticated
82 @web.authenticated
83 def delete(self, session_id):
83 def delete(self, session_id):
84 sm = self.session_manager
84 sm = self.session_manager
85 nbm = self.notebook_manager
85 nbm = self.notebook_manager
86 km = self.kernel_manager
86 km = self.kernel_manager
87 kernel_id = sm.get_kernel_from_session(session_id)
87 kernel_id = sm.get_kernel_from_session(session_id)
88 km.shutdown_kernel(kernel_id)
88 km.shutdown_kernel(kernel_id)
89 sm.delete_mapping_for_session(session_id)
89 sm.delete_mapping_for_session(session_id)
90 self.set_status(204)
91 self.finish()
90
92
91
93
92 #-----------------------------------------------------------------------------
94 #-----------------------------------------------------------------------------
93 # URL to handler mappings
95 # URL to handler mappings
94 #-----------------------------------------------------------------------------
96 #-----------------------------------------------------------------------------
95
97
96 _session_id_regex = r"(?P<session_id>\w+-\w+-\w+-\w+-\w+)"
98 _session_id_regex = r"(?P<session_id>\w+-\w+-\w+-\w+-\w+)"
97
99
98 default_handlers = [
100 default_handlers = [
99 (r"api/sessions/%s" % _session_id_regex, SessionHandler),
101 (r"api/sessions/%s" % _session_id_regex, SessionHandler),
100 (r"api/sessions", SessionRootHandler)
102 (r"api/sessions", SessionRootHandler)
101 ]
103 ]
102
104
103
105
104
106
105
107
@@ -1,349 +1,346 b''
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 // NotebookList
9 // NotebookList
10 //============================================================================
10 //============================================================================
11
11
12 var IPython = (function (IPython) {
12 var IPython = (function (IPython) {
13
13
14 var NotebookList = function (selector) {
14 var NotebookList = function (selector) {
15 this.selector = selector;
15 this.selector = selector;
16 if (this.selector !== undefined) {
16 if (this.selector !== undefined) {
17 this.element = $(selector);
17 this.element = $(selector);
18 this.style();
18 this.style();
19 this.bind_events();
19 this.bind_events();
20 }
20 }
21 this.notebooks_list = new Array();
21 this.notebooks_list = new Array();
22 this.sessions = new Object();
22 this.sessions = new Object();
23 };
23 };
24
24
25 NotebookList.prototype.baseProjectUrl = function () {
25 NotebookList.prototype.baseProjectUrl = function () {
26 return $('body').data('baseProjectUrl');
26 return $('body').data('baseProjectUrl');
27 };
27 };
28
28
29 NotebookList.prototype.notebookPath = function() {
29 NotebookList.prototype.notebookPath = function() {
30 var path = $('body').data('notebookPath');
30 var path = $('body').data('notebookPath');
31 if (path != "") {
31 if (path != "") {
32 if (path[path.length-1] != '/') {
32 if (path[path.length-1] != '/') {
33 path = path.substring(0,path.length);
33 path = path.substring(0,path.length);
34 };
34 };
35 return path;
35 return path;
36 } else {
36 } else {
37 return path;
37 return path;
38 };
38 };
39 };
39 };
40
40
41 NotebookList.prototype.url_name = function(name){
41 NotebookList.prototype.url_name = function(name){
42 return encodeURIComponent(name);
42 return encodeURIComponent(name);
43 };
43 };
44
44
45 NotebookList.prototype.style = function () {
45 NotebookList.prototype.style = function () {
46 $('#notebook_toolbar').addClass('list_toolbar');
46 $('#notebook_toolbar').addClass('list_toolbar');
47 $('#drag_info').addClass('toolbar_info');
47 $('#drag_info').addClass('toolbar_info');
48 $('#notebook_buttons').addClass('toolbar_buttons');
48 $('#notebook_buttons').addClass('toolbar_buttons');
49 $('#notebook_list_header').addClass('list_header');
49 $('#notebook_list_header').addClass('list_header');
50 this.element.addClass("list_container");
50 this.element.addClass("list_container");
51 };
51 };
52
52
53
53
54 NotebookList.prototype.bind_events = function () {
54 NotebookList.prototype.bind_events = function () {
55 var that = this;
55 var that = this;
56 $('#refresh_notebook_list').click(function () {
56 $('#refresh_notebook_list').click(function () {
57 that.load_list();
57 that.load_list();
58 });
58 });
59 this.element.bind('dragover', function () {
59 this.element.bind('dragover', function () {
60 return false;
60 return false;
61 });
61 });
62 this.element.bind('drop', function(event){
62 this.element.bind('drop', function(event){
63 that.handelFilesUpload(event,'drop');
63 that.handelFilesUpload(event,'drop');
64 return false;
64 return false;
65 });
65 });
66 };
66 };
67
67
68 NotebookList.prototype.handelFilesUpload = function(event, dropOrForm) {
68 NotebookList.prototype.handelFilesUpload = function(event, dropOrForm) {
69 var that = this;
69 var that = this;
70 var files;
70 var files;
71 if(dropOrForm =='drop'){
71 if(dropOrForm =='drop'){
72 files = event.originalEvent.dataTransfer.files;
72 files = event.originalEvent.dataTransfer.files;
73 } else
73 } else
74 {
74 {
75 files = event.originalEvent.target.files
75 files = event.originalEvent.target.files
76 }
76 }
77 for (var i = 0, f; f = files[i]; i++) {
77 for (var i = 0, f; f = files[i]; i++) {
78 var reader = new FileReader();
78 var reader = new FileReader();
79 reader.readAsText(f);
79 reader.readAsText(f);
80 var fname = f.name.split('.');
80 var fname = f.name.split('.');
81 var nbname = fname.slice(0,-1).join('.');
81 var nbname = fname.slice(0,-1).join('.');
82 var nbformat = fname.slice(-1)[0];
82 var nbformat = fname.slice(-1)[0];
83 if (nbformat === 'ipynb') {nbformat = 'json';};
83 if (nbformat === 'ipynb') {nbformat = 'json';};
84 if (nbformat === 'py' || nbformat === 'json') {
84 if (nbformat === 'py' || nbformat === 'json') {
85 var item = that.new_notebook_item(0);
85 var item = that.new_notebook_item(0);
86 that.add_name_input(nbname, item);
86 that.add_name_input(nbname, item);
87 item.data('nbformat', nbformat);
87 item.data('nbformat', nbformat);
88 // Store the notebook item in the reader so we can use it later
88 // Store the notebook item in the reader so we can use it later
89 // to know which item it belongs to.
89 // to know which item it belongs to.
90 $(reader).data('item', item);
90 $(reader).data('item', item);
91 reader.onload = function (event) {
91 reader.onload = function (event) {
92 var nbitem = $(event.target).data('item');
92 var nbitem = $(event.target).data('item');
93 that.add_notebook_data(event.target.result, nbitem);
93 that.add_notebook_data(event.target.result, nbitem);
94 that.add_upload_button(nbitem);
94 that.add_upload_button(nbitem);
95 };
95 };
96 };
96 };
97 }
97 }
98 return false;
98 return false;
99 };
99 };
100
100
101 NotebookList.prototype.clear_list = function () {
101 NotebookList.prototype.clear_list = function () {
102 this.element.children('.list_item').remove();
102 this.element.children('.list_item').remove();
103 };
103 };
104
104
105 NotebookList.prototype.load_sessions = function(){
105 NotebookList.prototype.load_sessions = function(){
106 console.log("DID IT MAKE IT?");
107 var that = this;
106 var settings = {
108 var settings = {
107 processData : false,
109 processData : false,
108 cache : false,
110 cache : false,
109 type : "GET",
111 type : "GET",
110 dataType : "json",
112 dataType : "json",
111 success : $.proxy(this.sessions_loaded, this)
113 success : $.proxy(that.sessions_loaded, this)
112 };
114 };
113 var url = this.baseProjectUrl() + 'api/sessions';
115 var url = this.baseProjectUrl() + 'api/sessions';
114 $.ajax(url,settings);
116 $.ajax(url,settings);
115 };
117 };
116
118
117
119
118 NotebookList.prototype.sessions_loaded = function(data){
120 NotebookList.prototype.sessions_loaded = function(data){
119 this.sessions=new Object();
121 this.sessions = new Object();
120 var len = data.length;
122 var len = data.length;
121 if (len != 0) {
123 if (len != 0) {
122 for (var i=0; i<len; i++) {
124 for (var i=0; i<len; i++) {
123 if (data[i]['notebook_path']==null) {
125 if (data[i]['notebook_path']==null) {
124 nb_path = data[i]['notebook_name'];
126 nb_path = data[i]['notebook_name'];
125 }
127 }
126 else {
128 else {
127 nb_path = data[i]['notebook_path'] + data[i]['notebook_name'];
129 nb_path = data[i]['notebook_path'] + data[i]['notebook_name'];
128 }
130 }
129 this.sessions[nb_path]= data[i]['session_id'];
131 this.sessions[nb_path]= data[i]['session_id'];
130 }
132 }
131 };
133 };
132 this.load_list();
134 this.load_list();
133 };
135 };
134
136
135 NotebookList.prototype.load_list = function () {
137 NotebookList.prototype.load_list = function () {
136 var that = this;
138 var that = this;
137 var settings = {
139 var settings = {
138 processData : false,
140 processData : false,
139 cache : false,
141 cache : false,
140 type : "GET",
142 type : "GET",
141 dataType : "json",
143 dataType : "json",
142 success : $.proxy(this.list_loaded, this),
144 success : $.proxy(this.list_loaded, this),
143 error : $.proxy( function(){
145 error : $.proxy( function(){
144 that.list_loaded([], null, null, {msg:"Error connecting to server."});
146 that.list_loaded([], null, null, {msg:"Error connecting to server."});
145 },this)
147 },this)
146 };
148 };
147
149
148 var url = this.baseProjectUrl() + 'api/notebooks/' + this.notebookPath();
150 var url = this.baseProjectUrl() + 'api/notebooks/' + this.notebookPath();
149 $.ajax(url, settings);
151 $.ajax(url, settings);
150 };
152 };
151
153
152
154
153 NotebookList.prototype.list_loaded = function (data, status, xhr, param) {
155 NotebookList.prototype.list_loaded = function (data, status, xhr, param) {
154 var message = 'Notebook list empty.';
156 var message = 'Notebook list empty.';
155 if (param !== undefined && param.msg) {
157 if (param !== undefined && param.msg) {
156 var message = param.msg;
158 var message = param.msg;
157 }
159 }
158 var len = data.length;
160 var len = data.length;
159 this.clear_list();
161 this.clear_list();
160 if(len == 0)
162 if(len == 0)
161 {
163 {
162 $(this.new_notebook_item(0))
164 $(this.new_notebook_item(0))
163 .append(
165 .append(
164 $('<div style="margin:auto;text-align:center;color:grey"/>')
166 $('<div style="margin:auto;text-align:center;color:grey"/>')
165 .text(message)
167 .text(message)
166 )
168 )
167 }
169 }
168 for (var i=0; i<len; i++) {
170 for (var i=0; i<len; i++) {
169 var name = data[i].notebook_name;
171 var name = data[i].notebook_name;
170 var path = this.notebookPath();
172 var path = this.notebookPath();
171 var nbname = name.split(".")[0];
173 var nbname = name.split(".")[0];
172 var item = this.new_notebook_item(i);
174 var item = this.new_notebook_item(i);
173 this.add_link(path, nbname, item);
175 this.add_link(path, nbname, item);
174 name = this.notebookPath() + name;
176 name = this.notebookPath() + name;
175 if(this.sessions[name] == undefined){
177 if(this.sessions[name] == undefined){
176 this.add_delete_button(item);
178 this.add_delete_button(item);
177 } else {
179 } else {
178 this.add_shutdown_button(item,this.sessions[name]);
180 this.add_shutdown_button(item,this.sessions[name]);
179 }
181 }
180 };
182 };
181 };
183 };
182
184
183
185
184 NotebookList.prototype.new_notebook_item = function (index) {
186 NotebookList.prototype.new_notebook_item = function (index) {
185 var item = $('<div/>').addClass("list_item").addClass("row-fluid");
187 var item = $('<div/>').addClass("list_item").addClass("row-fluid");
186 // item.addClass('list_item ui-widget ui-widget-content ui-helper-clearfix');
188 // item.addClass('list_item ui-widget ui-widget-content ui-helper-clearfix');
187 // item.css('border-top-style','none');
189 // item.css('border-top-style','none');
188 item.append($("<div/>").addClass("span12").append(
190 item.append($("<div/>").addClass("span12").append(
189 $("<a/>").addClass("item_link").append(
191 $("<a/>").addClass("item_link").append(
190 $("<span/>").addClass("item_name")
192 $("<span/>").addClass("item_name")
191 )
193 )
192 ).append(
194 ).append(
193 $('<div/>').addClass("item_buttons btn-group pull-right")
195 $('<div/>').addClass("item_buttons btn-group pull-right")
194 ));
196 ));
195
197
196 if (index === -1) {
198 if (index === -1) {
197 this.element.append(item);
199 this.element.append(item);
198 } else {
200 } else {
199 this.element.children().eq(index).after(item);
201 this.element.children().eq(index).after(item);
200 }
202 }
201 return item;
203 return item;
202 };
204 };
203
205
204
206
205 NotebookList.prototype.add_link = function (path, nbname, item) {
207 NotebookList.prototype.add_link = function (path, nbname, item) {
206 item.data('nbname', nbname);
208 item.data('nbname', nbname);
207 item.data('path', path);
209 item.data('path', path);
208 item.find(".item_name").text(nbname);
210 item.find(".item_name").text(nbname);
209 item.find("a.item_link")
211 item.find("a.item_link")
210 .attr('href', this.baseProjectUrl() + "notebooks/" + this.notebookPath() + nbname + ".ipynb")
212 .attr('href', this.baseProjectUrl() + "notebooks/" + this.notebookPath() + nbname + ".ipynb")
211 .attr('target','_blank');
213 .attr('target','_blank');
212 };
214 };
213
215
214
216
215 NotebookList.prototype.add_name_input = function (nbname, item) {
217 NotebookList.prototype.add_name_input = function (nbname, item) {
216 item.data('nbname', nbname);
218 item.data('nbname', nbname);
217 item.find(".item_name").empty().append(
219 item.find(".item_name").empty().append(
218 $('<input/>')
220 $('<input/>')
219 .addClass("nbname_input")
221 .addClass("nbname_input")
220 .attr('value', nbname)
222 .attr('value', nbname)
221 .attr('size', '30')
223 .attr('size', '30')
222 .attr('type', 'text')
224 .attr('type', 'text')
223 );
225 );
224 };
226 };
225
227
226
228
227 NotebookList.prototype.add_notebook_data = function (data, item) {
229 NotebookList.prototype.add_notebook_data = function (data, item) {
228 item.data('nbdata',data);
230 item.data('nbdata',data);
229 };
231 };
230
232
231
233
232 NotebookList.prototype.add_shutdown_button = function (item, session) {
234 NotebookList.prototype.add_shutdown_button = function (item, session) {
233 var that = this;
235 var that = this;
234 var shutdown_button = $("<button/>").text("Shutdown").addClass("btn btn-mini").
236 var shutdown_button = $("<button/>").text("Shutdown").addClass("btn btn-mini").
235 click(function (e) {
237 click(function (e) {
236 var settings = {
238 var settings = {
237 processData : false,
239 processData : false,
238 cache : false,
240 cache : false,
239 type : "DELETE",
241 type : "DELETE",
240 dataType : "json",
242 dataType : "json",
241 success : function (data, status, xhr) {
243 success : function () {
242 that.load_sessions();
244 that.load_sessions();
243 }
245 }
244 };
246 };
245 var url = that.baseProjectUrl() + 'api/sessions/' + session;
247 var url = that.baseProjectUrl() + 'api/sessions/' + session;
246 $.ajax(url, settings);
248 $.ajax(url, settings);
247 return false;
249 return false;
248 });
250 });
249 // var new_buttons = item.find('a'); // shutdown_button;
251 // var new_buttons = item.find('a'); // shutdown_button;
250 item.find(".item_buttons").html("").append(shutdown_button);
252 item.find(".item_buttons").html("").append(shutdown_button);
251 };
253 };
252
254
253 NotebookList.prototype.add_delete_button = function (item) {
255 NotebookList.prototype.add_delete_button = function (item) {
254 var new_buttons = $('<span/>').addClass("btn-group pull-right");
256 var new_buttons = $('<span/>').addClass("btn-group pull-right");
255 var notebooklist = this;
257 var notebooklist = this;
256 var delete_button = $("<button/>").text("Delete").addClass("btn btn-mini").
258 var delete_button = $("<button/>").text("Delete").addClass("btn btn-mini").
257 click(function (e) {
259 click(function (e) {
258 // $(this) is the button that was clicked.
260 // $(this) is the button that was clicked.
259 var that = $(this);
261 var that = $(this);
260 // We use the nbname and notebook_id from the parent notebook_item element's
262 // We use the nbname and notebook_id from the parent notebook_item element's
261 // data because the outer scopes values change as we iterate through the loop.
263 // data because the outer scopes values change as we iterate through the loop.
262 var parent_item = that.parents('div.list_item');
264 var parent_item = that.parents('div.list_item');
263 var nbname = parent_item.data('nbname');
265 var nbname = parent_item.data('nbname');
264 var message = 'Are you sure you want to permanently delete the notebook: ' + nbname + '?';
266 var message = 'Are you sure you want to permanently delete the notebook: ' + nbname + '?';
265 IPython.dialog.modal({
267 IPython.dialog.modal({
266 title : "Delete notebook",
268 title : "Delete notebook",
267 body : message,
269 body : message,
268 buttons : {
270 buttons : {
269 Delete : {
271 Delete : {
270 class: "btn-danger",
272 class: "btn-danger",
271 click: function() {
273 click: function() {
272 var settings = {
274 var settings = {
273 processData : false,
275 processData : false,
274 cache : false,
276 cache : false,
275 type : "DELETE",
277 type : "DELETE",
276 dataType : "json",
278 dataType : "json",
277 success : function (data, status, xhr) {
279 success : function (data, status, xhr) {
278 parent_item.remove();
280 parent_item.remove();
279 }
281 }
280 };
282 };
281 if (notebooklist.notebookPath() == "") {
283 var url = notebooklist.baseProjectUrl() + 'api/notebooks/' + notebooklist.notebookPath() + nbname + '.ipynb';
282 var url = notebooklist.baseProjectUrl() + 'api/notebooks/' + nbname +'.ipynb';
283 }
284 else {
285 var url = notebooklist.baseProjectUrl() + 'api/notebooks/' + notebooklist.notebookPath() + nbname + '.ipynb';
286 }
287 $.ajax(url, settings);
284 $.ajax(url, settings);
288 }
285 }
289 },
286 },
290 Cancel : {}
287 Cancel : {}
291 }
288 }
292 });
289 });
293 return false;
290 return false;
294 });
291 });
295 item.find(".item_buttons").html("").append(delete_button);
292 item.find(".item_buttons").html("").append(delete_button);
296 };
293 };
297
294
298
295
299 NotebookList.prototype.add_upload_button = function (item) {
296 NotebookList.prototype.add_upload_button = function (item) {
300 var that = this;
297 var that = this;
301 var upload_button = $('<button/>').text("Upload")
298 var upload_button = $('<button/>').text("Upload")
302 .addClass('btn btn-primary btn-mini upload_button')
299 .addClass('btn btn-primary btn-mini upload_button')
303 .click(function (e) {
300 .click(function (e) {
304 var nbname = item.find('.item_name > input').attr('value');
301 var nbname = item.find('.item_name > input').attr('value');
305 var nbformat = item.data('nbformat');
302 var nbformat = item.data('nbformat');
306 var nbdata = item.data('nbdata');
303 var nbdata = item.data('nbdata');
307 var content_type = 'text/plain';
304 var content_type = 'text/plain';
308 if (nbformat === 'json') {
305 if (nbformat === 'json') {
309 content_type = 'application/json';
306 content_type = 'application/json';
310 } else if (nbformat === 'py') {
307 } else if (nbformat === 'py') {
311 content_type = 'application/x-python';
308 content_type = 'application/x-python';
312 };
309 };
313 var settings = {
310 var settings = {
314 processData : false,
311 processData : false,
315 cache : false,
312 cache : false,
316 type : 'POST',
313 type : 'POST',
317 dataType : 'json',
314 dataType : 'json',
318 data : nbdata,
315 data : nbdata,
319 headers : {'Content-Type': content_type},
316 headers : {'Content-Type': content_type},
320 success : function (data, status, xhr) {
317 success : function (data, status, xhr) {
321 that.add_link(data, nbname, item);
318 that.add_link(data, nbname, item);
322 that.add_delete_button(item);
319 that.add_delete_button(item);
323 }
320 }
324 };
321 };
325
322
326 var qs = $.param({name:nbname, format:nbformat});
323 var qs = $.param({name:nbname, format:nbformat});
327 var url = that.baseProjectUrl() + 'notebooks?' + qs;
324 var url = that.baseProjectUrl() + 'notebooks?' + qs;
328 $.ajax(url, settings);
325 $.ajax(url, settings);
329 return false;
326 return false;
330 });
327 });
331 var cancel_button = $('<button/>').text("Cancel")
328 var cancel_button = $('<button/>').text("Cancel")
332 .addClass("btn btn-mini")
329 .addClass("btn btn-mini")
333 .click(function (e) {
330 .click(function (e) {
334 console.log('cancel click');
331 console.log('cancel click');
335 item.remove();
332 item.remove();
336 return false;
333 return false;
337 });
334 });
338 item.find(".item_buttons").empty()
335 item.find(".item_buttons").empty()
339 .append(upload_button)
336 .append(upload_button)
340 .append(cancel_button);
337 .append(cancel_button);
341 };
338 };
342
339
343
340
344 IPython.NotebookList = NotebookList;
341 IPython.NotebookList = NotebookList;
345
342
346 return IPython;
343 return IPython;
347
344
348 }(IPython));
345 }(IPython));
349
346
General Comments 0
You need to be logged in to leave comments. Login now