Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50654 views
1
// Copyright (c) IPython Development Team.
2
// Distributed under the terms of the Modified BSD License.
3
4
define([
5
'jquery',
6
'base/js/namespace',
7
'base/js/utils',
8
'base/js/dialog',
9
], function ($, IPython, utils, dialog) {
10
"use strict";
11
12
var NewNotebookWidget = function (selector, options) {
13
this.selector = selector;
14
this.base_url = options.base_url;
15
this.notebook_path = options.notebook_path;
16
this.contents = options.contents;
17
this.default_kernel = null;
18
this.kernelspecs = {};
19
if (this.selector !== undefined) {
20
this.element = $(selector);
21
this.request_kernelspecs();
22
}
23
this.bind_events();
24
};
25
26
NewNotebookWidget.prototype.bind_events = function () {
27
var that = this;
28
this.element.find('#new_notebook').click(function () {
29
that.new_notebook();
30
});
31
};
32
33
NewNotebookWidget.prototype.request_kernelspecs = function () {
34
/** request and then load kernel specs */
35
var url = utils.url_join_encode(this.base_url, 'api/kernelspecs');
36
utils.promising_ajax(url).then($.proxy(this._load_kernelspecs, this));
37
};
38
39
NewNotebookWidget.prototype._load_kernelspecs = function (data) {
40
/** load kernelspec list */
41
var that = this;
42
this.kernelspecs = data.kernelspecs;
43
var menu = this.element.find("#notebook-kernels");
44
var keys = Object.keys(data.kernelspecs).sort(function (a, b) {
45
var da = data.kernelspecs[a].spec.display_name;
46
var db = data.kernelspecs[b].spec.display_name;
47
if (da === db) {
48
return 0;
49
} else if (da > db) {
50
return 1;
51
} else {
52
return -1;
53
}
54
});
55
56
// Create the kernel list in reverse order because
57
// the .after insertion causes each item to be added
58
// to the top of the list.
59
for (var i = keys.length - 1; i >= 0; i--) {
60
var ks = this.kernelspecs[keys[i]];
61
var li = $("<li>")
62
.attr("id", "kernel-" +ks.name)
63
.data('kernelspec', ks).append(
64
$('<a>')
65
.attr('href', '#')
66
.click($.proxy(this.new_notebook, this, ks.name))
67
.text(ks.spec.display_name)
68
.attr('title', 'Create a new notebook with ' + ks.spec.display_name)
69
);
70
menu.after(li);
71
}
72
};
73
74
NewNotebookWidget.prototype.new_notebook = function (kernel_name) {
75
/** create and open a new notebook */
76
var that = this;
77
kernel_name = kernel_name || this.default_kernel;
78
var w = window.open(undefined, IPython._target);
79
this.contents.new_untitled(that.notebook_path, {type: "notebook"}).then(
80
function (data) {
81
var url = utils.url_join_encode(
82
that.base_url, 'notebooks', data.path
83
);
84
if (kernel_name) {
85
url += "?kernel_name=" + kernel_name;
86
}
87
w.location = url;
88
}).catch(function (e) {
89
w.close();
90
dialog.modal({
91
title : 'Creating Notebook Failed',
92
body : $('<div/>')
93
.text("An error occurred while creating a new notebook.")
94
.append($('<div/>')
95
.addClass('alert alert-danger')
96
.text(e.message || e)),
97
buttons: {
98
OK: {'class' : 'btn-primary'}
99
}
100
});
101
});
102
};
103
104
return {'NewNotebookWidget': NewNotebookWidget};
105
});
106
107