Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50654 views
1
//----------------------------------------------------------------------------
2
// Copyright (C) 2012 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
// Notification widget
10
//============================================================================
11
12
var IPython = (function (IPython) {
13
"use strict";
14
var utils = IPython.utils;
15
16
17
var NotificationArea = function (selector) {
18
this.selector = selector;
19
if (this.selector !== undefined) {
20
this.element = $(selector);
21
}
22
this.widget_dict = {};
23
};
24
25
NotificationArea.prototype.temp_message = function (msg, timeout, css_class) {
26
var uuid = utils.uuid();
27
if( css_class == 'danger') {css_class = 'ui-state-error';}
28
if( css_class == 'warning') {css_class = 'ui-state-highlight';}
29
var tdiv = $('<div>')
30
.attr('id',uuid)
31
.addClass('notification_widget ui-widget ui-widget-content ui-corner-all')
32
.addClass('border-box-sizing')
33
.addClass(css_class)
34
.hide()
35
.text(msg);
36
37
$(this.selector).append(tdiv);
38
var tmout = Math.max(1500,(timeout||1500));
39
tdiv.fadeIn(100);
40
41
setTimeout(function () {
42
tdiv.fadeOut(100, function () {tdiv.remove();});
43
}, tmout);
44
};
45
46
NotificationArea.prototype.widget = function(name) {
47
if(this.widget_dict[name] === undefined) {
48
return this.new_notification_widget(name);
49
}
50
return this.get_widget(name);
51
};
52
53
NotificationArea.prototype.get_widget = function(name) {
54
if(this.widget_dict[name] === undefined) {
55
throw('no widgets with this name');
56
}
57
return this.widget_dict[name];
58
};
59
60
NotificationArea.prototype.new_notification_widget = function(name) {
61
if(this.widget_dict[name] !== undefined) {
62
throw('widget with that name already exists ! ');
63
}
64
var div = $('<div/>').attr('id','notification_'+name);
65
$(this.selector).append(div);
66
this.widget_dict[name] = new IPython.NotificationWidget('#notification_'+name);
67
return this.widget_dict[name];
68
};
69
70
NotificationArea.prototype.init_notification_widgets = function() {
71
var knw = this.new_notification_widget('kernel');
72
var $kernel_ind_icon = $("#kernel_indicator_icon");
73
var $modal_ind_icon = $("#modal_indicator_icon");
74
75
// Command/Edit mode
76
$([IPython.events]).on('edit_mode.Notebook',function () {
77
IPython.save_widget.update_document_title();
78
$modal_ind_icon.attr('class','edit_mode_icon').attr('title','Edit Mode');
79
});
80
81
$([IPython.events]).on('command_mode.Notebook',function () {
82
IPython.save_widget.update_document_title();
83
$modal_ind_icon.attr('class','command_mode_icon').attr('title','Command Mode');
84
});
85
86
// Implicitly start off in Command mode, switching to Edit mode will trigger event
87
$modal_ind_icon.attr('class','command-mode_icon').attr('title','Command Mode');
88
89
// Kernel events
90
$([IPython.events]).on('status_idle.Kernel',function () {
91
IPython.save_widget.update_document_title();
92
$kernel_ind_icon.attr('class','kernel_idle_icon').attr('title','Kernel Idle');
93
});
94
95
$([IPython.events]).on('status_busy.Kernel',function () {
96
window.document.title='(Busy) '+window.document.title;
97
$kernel_ind_icon.attr('class','kernel_busy_icon').attr('title','Kernel Busy');
98
});
99
100
$([IPython.events]).on('status_restarting.Kernel',function () {
101
IPython.save_widget.update_document_title();
102
knw.set_message("Restarting kernel", 2000);
103
});
104
105
$([IPython.events]).on('status_interrupting.Kernel',function () {
106
knw.set_message("Interrupting kernel", 2000);
107
});
108
109
// Start the kernel indicator in the busy state, and send a kernel_info request.
110
// When the kernel_info reply arrives, the kernel is idle.
111
$kernel_ind_icon.attr('class','kernel_busy_icon').attr('title','Kernel Busy');
112
113
$([IPython.events]).on('status_started.Kernel', function (evt, data) {
114
data.kernel.kernel_info(function () {
115
$([IPython.events]).trigger('status_idle.Kernel');
116
});
117
});
118
119
$([IPython.events]).on('status_dead.Kernel',function () {
120
var msg = 'The kernel has died, and the automatic restart has failed.' +
121
' It is possible the kernel cannot be restarted.' +
122
' If you are not able to restart the kernel, you will still be able to save' +
123
' the notebook, but running code will no longer work until the notebook' +
124
' is reopened.';
125
126
IPython.dialog.modal({
127
title: "Dead kernel",
128
body : msg,
129
buttons : {
130
"Manual Restart": {
131
class: "btn-danger",
132
click: function () {
133
$([IPython.events]).trigger('status_restarting.Kernel');
134
IPython.notebook.start_kernel();
135
}
136
},
137
"Don't restart": {}
138
}
139
});
140
});
141
142
$([IPython.events]).on('websocket_closed.Kernel', function (event, data) {
143
var kernel = data.kernel;
144
var ws_url = data.ws_url;
145
var early = data.early;
146
var msg;
147
if (!early) {
148
knw.set_message('Reconnecting WebSockets', 1000);
149
setTimeout(function () {
150
kernel.start_channels();
151
}, 5000);
152
return;
153
}
154
console.log('WebSocket connection failed: ', ws_url);
155
msg = "A WebSocket connection could not be established." +
156
" You will NOT be able to run code. Check your" +
157
" network connection or notebook server configuration.";
158
IPython.dialog.modal({
159
title: "WebSocket connection failed",
160
body: msg,
161
buttons : {
162
"OK": {},
163
"Reconnect": {
164
click: function () {
165
knw.set_message('Reconnecting WebSockets', 1000);
166
setTimeout(function () {
167
kernel.start_channels();
168
}, 5000);
169
}
170
}
171
}
172
});
173
});
174
175
176
var nnw = this.new_notification_widget('notebook');
177
178
// Notebook events
179
$([IPython.events]).on('notebook_loading.Notebook', function () {
180
nnw.set_message("Loading notebook",500);
181
});
182
$([IPython.events]).on('notebook_loaded.Notebook', function () {
183
nnw.set_message("Notebook loaded",500);
184
});
185
$([IPython.events]).on('notebook_saving.Notebook', function () {
186
nnw.set_message("Saving notebook",500);
187
});
188
$([IPython.events]).on('notebook_saved.Notebook', function () {
189
nnw.set_message("Notebook saved",2000);
190
});
191
$([IPython.events]).on('notebook_save_failed.Notebook', function (evt, xhr, status, data) {
192
nnw.set_message(data || "Notebook save failed");
193
});
194
195
// Checkpoint events
196
$([IPython.events]).on('checkpoint_created.Notebook', function (evt, data) {
197
var msg = "Checkpoint created";
198
if (data.last_modified) {
199
var d = new Date(data.last_modified);
200
msg = msg + ": " + d.format("HH:MM:ss");
201
}
202
nnw.set_message(msg, 2000);
203
});
204
$([IPython.events]).on('checkpoint_failed.Notebook', function () {
205
nnw.set_message("Checkpoint failed");
206
});
207
$([IPython.events]).on('checkpoint_deleted.Notebook', function () {
208
nnw.set_message("Checkpoint deleted", 500);
209
});
210
$([IPython.events]).on('checkpoint_delete_failed.Notebook', function () {
211
nnw.set_message("Checkpoint delete failed");
212
});
213
$([IPython.events]).on('checkpoint_restoring.Notebook', function () {
214
nnw.set_message("Restoring to checkpoint...", 500);
215
});
216
$([IPython.events]).on('checkpoint_restore_failed.Notebook', function () {
217
nnw.set_message("Checkpoint restore failed");
218
});
219
220
// Autosave events
221
$([IPython.events]).on('autosave_disabled.Notebook', function () {
222
nnw.set_message("Autosave disabled", 2000);
223
});
224
$([IPython.events]).on('autosave_enabled.Notebook', function (evt, interval) {
225
nnw.set_message("Saving every " + interval / 1000 + "s", 1000);
226
});
227
228
};
229
230
IPython.NotificationArea = NotificationArea;
231
232
return IPython;
233
234
}(IPython));
235
236
237