Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50654 views
1
define([
2
'base/js/namespace',
3
'jquery',
4
'base/js/utils',
5
'base/js/dialog',
6
'base/js/notificationarea',
7
'moment'
8
], function(IPython, $, utils, dialog, notificationarea, moment) {
9
"use strict";
10
var NotificationArea = notificationarea.NotificationArea;
11
12
var NotebookNotificationArea = function(selector, options) {
13
NotificationArea.apply(this, [selector, options]);
14
this.save_widget = options.save_widget;
15
this.notebook = options.notebook;
16
this.keyboard_manager = options.keyboard_manager;
17
};
18
19
NotebookNotificationArea.prototype = Object.create(NotificationArea.prototype);
20
21
/**
22
* Initialize the default set of notification widgets.
23
*
24
* @method init_notification_widgets
25
*/
26
NotebookNotificationArea.prototype.init_notification_widgets = function () {
27
this.init_kernel_notification_widget();
28
this.init_notebook_notification_widget();
29
};
30
31
/**
32
* Initialize the notification widget for kernel status messages.
33
*
34
* @method init_kernel_notification_widget
35
*/
36
NotebookNotificationArea.prototype.init_kernel_notification_widget = function () {
37
var that = this;
38
var knw = this.new_notification_widget('kernel');
39
var $kernel_ind_icon = $("#kernel_indicator_icon");
40
var $modal_ind_icon = $("#modal_indicator");
41
var $readonly_ind_icon = $('#readonly-indicator');
42
var $body = $('body');
43
44
// Listen for the notebook loaded event. Set readonly indicator.
45
this.events.on('notebook_loaded.Notebook', function() {
46
if (that.notebook.writable) {
47
$readonly_ind_icon.hide();
48
} else {
49
$readonly_ind_icon.show();
50
}
51
});
52
53
// Command/Edit mode
54
this.events.on('edit_mode.Notebook', function () {
55
that.save_widget.update_document_title();
56
$body.addClass('edit_mode');
57
$body.removeClass('command_mode');
58
$modal_ind_icon.attr('title','Edit Mode');
59
});
60
61
this.events.on('command_mode.Notebook', function () {
62
that.save_widget.update_document_title();
63
$body.removeClass('edit_mode');
64
$body.addClass('command_mode');
65
$modal_ind_icon.attr('title','Command Mode');
66
});
67
68
// Implicitly start off in Command mode, switching to Edit mode will trigger event
69
$modal_ind_icon.addClass('modal_indicator').attr('title','Command Mode');
70
$body.addClass('command_mode');
71
72
// Kernel events
73
74
// this can be either kernel_created.Kernel or kernel_created.Session
75
this.events.on('kernel_created.Kernel kernel_created.Session', function () {
76
knw.info("Kernel Created", 500);
77
});
78
79
this.events.on('kernel_reconnecting.Kernel', function () {
80
knw.warning("Connecting to kernel");
81
});
82
83
this.events.on('kernel_connection_dead.Kernel', function (evt, info) {
84
knw.danger("Not Connected", undefined, function () {
85
// schedule reconnect a short time in the future, don't reconnect immediately
86
setTimeout($.proxy(info.kernel.reconnect, info.kernel), 500);
87
}, {title: 'click to reconnect'});
88
});
89
90
this.events.on('kernel_connected.Kernel', function () {
91
knw.info("Connected", 500);
92
});
93
94
this.events.on('kernel_restarting.Kernel', function () {
95
that.save_widget.update_document_title();
96
knw.set_message("Restarting kernel", 2000);
97
});
98
99
this.events.on('kernel_autorestarting.Kernel', function (evt, info) {
100
// Only show the dialog on the first restart attempt. This
101
// number gets tracked by the `Kernel` object and passed
102
// along here, because we don't want to show the user 5
103
// dialogs saying the same thing (which is the number of
104
// times it tries restarting).
105
if (info.attempt === 1) {
106
107
dialog.kernel_modal({
108
notebook: that.notebook,
109
keyboard_manager: that.keyboard_manager,
110
title: "Kernel Restarting",
111
body: "The kernel appears to have died. It will restart automatically.",
112
buttons: {
113
OK : {
114
class : "btn-primary"
115
}
116
}
117
});
118
}
119
120
that.save_widget.update_document_title();
121
knw.danger("Dead kernel");
122
$kernel_ind_icon.attr('class','kernel_dead_icon').attr('title','Kernel Dead');
123
});
124
125
this.events.on('kernel_interrupting.Kernel', function () {
126
knw.set_message("Interrupting kernel", 2000);
127
});
128
129
this.events.on('kernel_disconnected.Kernel', function () {
130
$kernel_ind_icon
131
.attr('class', 'kernel_disconnected_icon')
132
.attr('title', 'No Connection to Kernel');
133
});
134
135
this.events.on('kernel_connection_failed.Kernel', function (evt, info) {
136
// only show the dialog if this is the first failed
137
// connect attempt, because the kernel will continue
138
// trying to reconnect and we don't want to spam the user
139
// with messages
140
if (info.attempt === 1) {
141
142
var msg = "A connection to the notebook server could not be established." +
143
" The notebook will continue trying to reconnect, but" +
144
" until it does, you will NOT be able to run code. Check your" +
145
" network connection or notebook server configuration.";
146
147
dialog.kernel_modal({
148
title: "Connection failed",
149
body: msg,
150
keyboard_manager: that.keyboard_manager,
151
notebook: that.notebook,
152
buttons : {
153
"OK": {}
154
}
155
});
156
}
157
});
158
159
this.events.on('kernel_killed.Kernel kernel_killed.Session', function () {
160
that.save_widget.update_document_title();
161
knw.warning("No kernel");
162
$kernel_ind_icon.attr('class','kernel_busy_icon').attr('title','Kernel is not running');
163
});
164
165
this.events.on('kernel_dead.Kernel', function () {
166
167
var showMsg = function () {
168
169
var msg = 'The kernel has died, and the automatic restart has failed.' +
170
' It is possible the kernel cannot be restarted.' +
171
' If you are not able to restart the kernel, you will still be able to save' +
172
' the notebook, but running code will no longer work until the notebook' +
173
' is reopened.';
174
175
dialog.kernel_modal({
176
title: "Dead kernel",
177
body : msg,
178
keyboard_manager: that.keyboard_manager,
179
notebook: that.notebook,
180
buttons : {
181
"Manual Restart": {
182
class: "btn-danger",
183
click: function () {
184
that.notebook.start_session();
185
}
186
},
187
"Don't restart": {}
188
}
189
});
190
191
return false;
192
};
193
194
that.save_widget.update_document_title();
195
knw.danger("Dead kernel", undefined, showMsg);
196
$kernel_ind_icon.attr('class','kernel_dead_icon').attr('title','Kernel Dead');
197
198
showMsg();
199
});
200
201
this.events.on("no_kernel.Kernel", function (evt, data) {
202
$("#kernel_indicator").find('.kernel_indicator_name').text("No Kernel");
203
});
204
205
this.events.on('kernel_dead.Session', function (evt, info) {
206
var full = info.xhr.responseJSON.message;
207
var short = info.xhr.responseJSON.short_message || 'Kernel error';
208
var traceback = info.xhr.responseJSON.traceback;
209
210
var showMsg = function () {
211
var msg = $('<div/>').append($('<p/>').text(full));
212
var cm, cm_elem, cm_open;
213
214
if (traceback) {
215
cm_elem = $('<div/>')
216
.css('margin-top', '1em')
217
.css('padding', '1em')
218
.addClass('output_scroll');
219
msg.append(cm_elem);
220
cm = CodeMirror(cm_elem.get(0), {
221
mode: "python",
222
readOnly : true
223
});
224
cm.setValue(traceback);
225
cm_open = $.proxy(cm.refresh, cm);
226
}
227
228
dialog.kernel_modal({
229
title: "Failed to start the kernel",
230
body : msg,
231
keyboard_manager: that.keyboard_manager,
232
notebook: that.notebook,
233
open: cm_open,
234
buttons : {
235
"Ok": { class: 'btn-primary' }
236
}
237
});
238
239
return false;
240
};
241
242
that.save_widget.update_document_title();
243
$kernel_ind_icon.attr('class','kernel_dead_icon').attr('title','Kernel Dead');
244
knw.danger(short, undefined, showMsg);
245
});
246
247
this.events.on('kernel_starting.Kernel kernel_created.Session', function () {
248
window.document.title='(Starting) '+window.document.title;
249
$kernel_ind_icon.attr('class','kernel_busy_icon').attr('title','Kernel Busy');
250
knw.set_message("Kernel starting, please wait...");
251
});
252
253
this.events.on('kernel_ready.Kernel', function () {
254
that.save_widget.update_document_title();
255
$kernel_ind_icon.attr('class','kernel_idle_icon').attr('title','Kernel Idle');
256
knw.info("Kernel ready", 500);
257
});
258
259
this.events.on('kernel_idle.Kernel', function () {
260
that.save_widget.update_document_title();
261
$kernel_ind_icon.attr('class','kernel_idle_icon').attr('title','Kernel Idle');
262
});
263
264
this.events.on('kernel_busy.Kernel', function () {
265
window.document.title='(Busy) '+window.document.title;
266
$kernel_ind_icon.attr('class','kernel_busy_icon').attr('title','Kernel Busy');
267
});
268
269
this.events.on('spec_match_found.Kernel', function (evt, data) {
270
that.widget('kernelspec').info("Using kernel: " + data.found.spec.display_name, 3000, undefined, {
271
title: "Only candidate for language: " + data.selected.language + " was " + data.found.spec.display_name
272
});
273
});
274
275
276
// Start the kernel indicator in the busy state, and send a kernel_info request.
277
// When the kernel_info reply arrives, the kernel is idle.
278
$kernel_ind_icon.attr('class','kernel_busy_icon').attr('title','Kernel Busy');
279
};
280
281
/**
282
* Initialize the notification widget for notebook status messages.
283
*
284
* @method init_notebook_notification_widget
285
*/
286
NotebookNotificationArea.prototype.init_notebook_notification_widget = function () {
287
var nnw = this.new_notification_widget('notebook');
288
289
// Notebook events
290
this.events.on('notebook_loading.Notebook', function () {
291
nnw.set_message("Loading notebook",500);
292
});
293
this.events.on('notebook_loaded.Notebook', function () {
294
nnw.set_message("Notebook loaded",500);
295
});
296
this.events.on('notebook_saving.Notebook', function () {
297
nnw.set_message("Saving notebook",500);
298
});
299
this.events.on('notebook_saved.Notebook', function () {
300
nnw.set_message("Notebook saved",2000);
301
});
302
this.events.on('notebook_save_failed.Notebook', function (evt, error) {
303
nnw.warning(error.message || "Notebook save failed");
304
});
305
this.events.on('notebook_copy_failed.Notebook', function (evt, error) {
306
nnw.warning(error.message || "Notebook copy failed");
307
});
308
309
// Checkpoint events
310
this.events.on('checkpoint_created.Notebook', function (evt, data) {
311
var msg = "Checkpoint created";
312
if (data.last_modified) {
313
var d = new Date(data.last_modified);
314
msg = msg + ": " + moment(d).format("HH:mm:ss");
315
}
316
nnw.set_message(msg, 2000);
317
});
318
this.events.on('checkpoint_failed.Notebook', function () {
319
nnw.warning("Checkpoint failed");
320
});
321
this.events.on('checkpoint_deleted.Notebook', function () {
322
nnw.set_message("Checkpoint deleted", 500);
323
});
324
this.events.on('checkpoint_delete_failed.Notebook', function () {
325
nnw.warning("Checkpoint delete failed");
326
});
327
this.events.on('checkpoint_restoring.Notebook', function () {
328
nnw.set_message("Restoring to checkpoint...", 500);
329
});
330
this.events.on('checkpoint_restore_failed.Notebook', function () {
331
nnw.warning("Checkpoint restore failed");
332
});
333
334
// Autosave events
335
this.events.on('autosave_disabled.Notebook', function () {
336
nnw.set_message("Autosave disabled", 2000);
337
});
338
this.events.on('autosave_enabled.Notebook', function (evt, interval) {
339
nnw.set_message("Saving every " + interval / 1000 + "s", 1000);
340
});
341
};
342
343
// Backwards compatibility.
344
IPython.NotificationArea = NotebookNotificationArea;
345
346
return {'NotebookNotificationArea': NotebookNotificationArea};
347
});
348
349