Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50659 views
1
//----------------------------------------------------------------------------
2
// Copyright (C) 2011 The IPython Development Team
3
//
4
// Distributed under the terms of the BSD License. The full license is in
5
// the file COPYING, distributed as part of this software.
6
//----------------------------------------------------------------------------
7
8
//============================================================================
9
// NotebookList
10
//============================================================================
11
12
var IPython = (function (IPython) {
13
"use strict";
14
15
var utils = IPython.utils;
16
17
var NotebookList = function (selector, options, element_name) {
18
var that = this
19
// allow code re-use by just changing element_name in kernellist.js
20
this.element_name = element_name || 'notebook';
21
this.selector = selector;
22
if (this.selector !== undefined) {
23
this.element = $(selector);
24
this.style();
25
this.bind_events();
26
}
27
this.notebooks_list = [];
28
this.sessions = {};
29
this.base_url = options.base_url || utils.get_body_data("baseUrl");
30
this.notebook_path = options.notebook_path || utils.get_body_data("notebookPath");
31
$([IPython.events]).on('sessions_loaded.Dashboard',
32
function(e, d) { that.sessions_loaded(d); });
33
};
34
35
NotebookList.prototype.style = function () {
36
var prefix = '#' + this.element_name
37
$(prefix + '_toolbar').addClass('list_toolbar');
38
$(prefix + '_list_info').addClass('toolbar_info');
39
$(prefix + '_buttons').addClass('toolbar_buttons');
40
$(prefix + '_list_header').addClass('list_header');
41
this.element.addClass("list_container");
42
};
43
44
45
NotebookList.prototype.bind_events = function () {
46
var that = this;
47
$('#refresh_' + this.element_name + '_list').click(function () {
48
that.load_sessions();
49
});
50
this.element.bind('dragover', function () {
51
return false;
52
});
53
this.element.bind('drop', function(event){
54
that.handleFilesUpload(event,'drop');
55
return false;
56
});
57
};
58
59
NotebookList.prototype.handleFilesUpload = function(event, dropOrForm) {
60
var that = this;
61
var files;
62
if(dropOrForm =='drop'){
63
files = event.originalEvent.dataTransfer.files;
64
} else
65
{
66
files = event.originalEvent.target.files;
67
}
68
for (var i = 0; i < files.length; i++) {
69
var f = files[i];
70
var reader = new FileReader();
71
reader.readAsText(f);
72
var name_and_ext = utils.splitext(f.name);
73
var file_ext = name_and_ext[1];
74
if (file_ext === '.ipynb') {
75
var item = that.new_notebook_item(0);
76
item.addClass('new-file');
77
that.add_name_input(f.name, item);
78
// Store the notebook item in the reader so we can use it later
79
// to know which item it belongs to.
80
$(reader).data('item', item);
81
reader.onload = function (event) {
82
var nbitem = $(event.target).data('item');
83
that.add_notebook_data(event.target.result, nbitem);
84
that.add_upload_button(nbitem);
85
};
86
} else {
87
var dialog = 'Uploaded notebooks must be .ipynb files';
88
IPython.dialog.modal({
89
title : 'Invalid file type',
90
body : dialog,
91
buttons : {'OK' : {'class' : 'btn-primary'}}
92
});
93
}
94
}
95
// Replace the file input form wth a clone of itself. This is required to
96
// reset the form. Otherwise, if you upload a file, delete it and try to
97
// upload it again, the changed event won't fire.
98
var form = $('input.fileinput');
99
form.replaceWith(form.clone(true));
100
return false;
101
};
102
103
NotebookList.prototype.clear_list = function (remove_uploads) {
104
// Clears the navigation tree.
105
//
106
// Parameters
107
// remove_uploads: bool=False
108
// Should upload prompts also be removed from the tree.
109
if (remove_uploads) {
110
this.element.children('.list_item').remove();
111
} else {
112
this.element.children('.list_item:not(.new-file)').remove();
113
}
114
};
115
116
NotebookList.prototype.load_sessions = function(){
117
IPython.session_list.load_sessions();
118
};
119
120
121
NotebookList.prototype.sessions_loaded = function(data){
122
this.sessions = data;
123
this.load_list();
124
};
125
126
NotebookList.prototype.load_list = function () {
127
var that = this;
128
var settings = {
129
processData : false,
130
cache : false,
131
type : "GET",
132
dataType : "json",
133
success : $.proxy(this.list_loaded, this),
134
error : $.proxy( function(xhr, status, error){
135
utils.log_ajax_error(xhr, status, error);
136
that.list_loaded([], null, null, {msg:"Error connecting to server."});
137
},this)
138
};
139
140
var url = utils.url_join_encode(
141
this.base_url,
142
'api',
143
'notebooks',
144
this.notebook_path
145
);
146
$.ajax(url, settings);
147
};
148
149
150
NotebookList.prototype.list_loaded = function (data, status, xhr, param) {
151
var message = 'Notebook list empty.';
152
if (param !== undefined && param.msg) {
153
message = param.msg;
154
}
155
var item = null;
156
var len = data.length;
157
this.clear_list();
158
if (len === 0) {
159
item = this.new_notebook_item(0);
160
var span12 = item.children().first();
161
span12.empty();
162
span12.append($('<div style="margin:auto;text-align:center;color:grey"/>').text(message));
163
}
164
var path = this.notebook_path;
165
var offset = 0;
166
if (path !== '') {
167
item = this.new_notebook_item(0);
168
this.add_dir(path, '..', item);
169
offset = 1;
170
}
171
for (var i=0; i<len; i++) {
172
if (data[i].type === 'directory') {
173
var name = data[i].name;
174
item = this.new_notebook_item(i+offset);
175
this.add_dir(path, name, item);
176
} else {
177
var name = data[i].name;
178
item = this.new_notebook_item(i+offset);
179
this.add_link(path, name, item);
180
name = utils.url_path_join(path, name);
181
if(this.sessions[name] === undefined){
182
this.add_delete_button(item);
183
} else {
184
this.add_shutdown_button(item,this.sessions[name]);
185
}
186
}
187
}
188
};
189
190
191
NotebookList.prototype.new_notebook_item = function (index) {
192
var item = $('<div/>').addClass("list_item").addClass("row-fluid");
193
// item.addClass('list_item ui-widget ui-widget-content ui-helper-clearfix');
194
// item.css('border-top-style','none');
195
item.append($("<div/>").addClass("span12").append(
196
$('<i/>').addClass('item_icon')
197
).append(
198
$("<a/>").addClass("item_link").append(
199
$("<span/>").addClass("item_name")
200
)
201
).append(
202
$('<div/>').addClass("item_buttons btn-group pull-right")
203
));
204
205
if (index === -1) {
206
this.element.append(item);
207
} else {
208
this.element.children().eq(index).after(item);
209
}
210
return item;
211
};
212
213
214
NotebookList.prototype.add_dir = function (path, name, item) {
215
item.data('name', name);
216
item.data('path', path);
217
item.find(".item_name").text(name);
218
item.find(".item_icon").addClass('folder_icon').addClass('icon-fixed-width');
219
item.find("a.item_link")
220
.attr('href',
221
utils.url_join_encode(
222
this.base_url,
223
"tree",
224
path,
225
name
226
)
227
);
228
};
229
230
231
NotebookList.prototype.add_link = function (path, nbname, item) {
232
item.data('nbname', nbname);
233
item.data('path', path);
234
item.find(".item_name").text(nbname);
235
item.find(".item_icon").addClass('notebook_icon').addClass('icon-fixed-width');
236
item.find("a.item_link")
237
.attr('href',
238
utils.url_join_encode(
239
this.base_url,
240
"notebooks",
241
path,
242
nbname
243
)
244
).attr('target','_blank');
245
};
246
247
248
NotebookList.prototype.add_name_input = function (nbname, item) {
249
item.data('nbname', nbname);
250
item.find(".item_icon").addClass('notebook_icon').addClass('icon-fixed-width');
251
item.find(".item_name").empty().append(
252
$('<input/>')
253
.addClass("nbname_input")
254
.attr('value', utils.splitext(nbname)[0])
255
.attr('size', '30')
256
.attr('type', 'text')
257
);
258
};
259
260
261
NotebookList.prototype.add_notebook_data = function (data, item) {
262
item.data('nbdata', data);
263
};
264
265
266
NotebookList.prototype.add_shutdown_button = function (item, session) {
267
var that = this;
268
var shutdown_button = $("<button/>").text("Shutdown").addClass("btn btn-mini btn-danger").
269
click(function (e) {
270
var settings = {
271
processData : false,
272
cache : false,
273
type : "DELETE",
274
dataType : "json",
275
success : function () {
276
that.load_sessions();
277
},
278
error : utils.log_ajax_error,
279
};
280
var url = utils.url_join_encode(
281
that.base_url,
282
'api/sessions',
283
session
284
);
285
$.ajax(url, settings);
286
return false;
287
});
288
// var new_buttons = item.find('a'); // shutdown_button;
289
item.find(".item_buttons").text("").append(shutdown_button);
290
};
291
292
NotebookList.prototype.add_delete_button = function (item) {
293
var new_buttons = $('<span/>').addClass("btn-group pull-right");
294
var notebooklist = this;
295
var delete_button = $("<button/>").text("Delete").addClass("btn btn-mini").
296
click(function (e) {
297
// $(this) is the button that was clicked.
298
var that = $(this);
299
// We use the nbname and notebook_id from the parent notebook_item element's
300
// data because the outer scopes values change as we iterate through the loop.
301
var parent_item = that.parents('div.list_item');
302
var nbname = parent_item.data('nbname');
303
var message = 'Are you sure you want to permanently delete the notebook: ' + nbname + '?';
304
IPython.dialog.modal({
305
title : "Delete notebook",
306
body : message,
307
buttons : {
308
Delete : {
309
class: "btn-danger",
310
click: function() {
311
var settings = {
312
processData : false,
313
cache : false,
314
type : "DELETE",
315
dataType : "json",
316
success : function (data, status, xhr) {
317
parent_item.remove();
318
},
319
error : utils.log_ajax_error,
320
};
321
var url = utils.url_join_encode(
322
notebooklist.base_url,
323
'api/notebooks',
324
notebooklist.notebook_path,
325
nbname
326
);
327
$.ajax(url, settings);
328
}
329
},
330
Cancel : {}
331
}
332
});
333
return false;
334
});
335
item.find(".item_buttons").text("").append(delete_button);
336
};
337
338
339
NotebookList.prototype.add_upload_button = function (item) {
340
var that = this;
341
var upload_button = $('<button/>').text("Upload")
342
.addClass('btn btn-primary btn-mini upload_button')
343
.click(function (e) {
344
var nbname = item.find('.item_name > input').val();
345
if (nbname.slice(nbname.length-6, nbname.length) != ".ipynb") {
346
nbname = nbname + ".ipynb";
347
}
348
var path = that.notebook_path;
349
var nbdata = item.data('nbdata');
350
var content_type = 'application/json';
351
var model = {
352
content : JSON.parse(nbdata),
353
};
354
var settings = {
355
processData : false,
356
cache : false,
357
type : 'PUT',
358
dataType : 'json',
359
data : JSON.stringify(model),
360
headers : {'Content-Type': content_type},
361
success : function (data, status, xhr) {
362
that.add_link(path, nbname, item);
363
that.add_delete_button(item);
364
},
365
error : utils.log_ajax_error,
366
};
367
368
var url = utils.url_join_encode(
369
that.base_url,
370
'api/notebooks',
371
that.notebook_path,
372
nbname
373
);
374
$.ajax(url, settings);
375
return false;
376
});
377
var cancel_button = $('<button/>').text("Cancel")
378
.addClass("btn btn-mini")
379
.click(function (e) {
380
console.log('cancel click');
381
item.remove();
382
return false;
383
});
384
item.find(".item_buttons").empty()
385
.append(upload_button)
386
.append(cancel_button);
387
};
388
389
390
NotebookList.prototype.new_notebook = function(){
391
var path = this.notebook_path;
392
var base_url = this.base_url;
393
var settings = {
394
processData : false,
395
cache : false,
396
type : "POST",
397
dataType : "json",
398
async : false,
399
success : function (data, status, xhr) {
400
var notebook_name = data.name;
401
window.open(
402
utils.url_join_encode(
403
base_url,
404
'notebooks',
405
path,
406
notebook_name),
407
'_blank'
408
);
409
},
410
error : $.proxy(this.new_notebook_failed, this),
411
};
412
var url = utils.url_join_encode(
413
base_url,
414
'api/notebooks',
415
path
416
);
417
$.ajax(url, settings);
418
};
419
420
421
NotebookList.prototype.new_notebook_failed = function (xhr, status, error) {
422
utils.log_ajax_error(xhr, status, error);
423
var msg;
424
if (xhr.responseJSON && xhr.responseJSON.message) {
425
msg = xhr.responseJSON.message;
426
} else {
427
msg = xhr.statusText;
428
}
429
IPython.dialog.modal({
430
title : 'Creating Notebook Failed',
431
body : "The error was: " + msg,
432
buttons : {'OK' : {'class' : 'btn-primary'}}
433
});
434
}
435
436
437
IPython.NotebookList = NotebookList;
438
439
return IPython;
440
441
}(IPython));
442
443