| 1 |
// Copyright (c) Jupyter Development Team. |
| 2 |
// Distributed under the terms of the Modified BSD License. |
| 3 |
|
| 4 |
import { |
| 5 |
JupyterFrontEnd, |
| 6 |
JupyterFrontEndPlugin, |
| 7 |
} from '@jupyterlab/application'; |
| 8 |
|
| 9 |
import { |
| 10 |
ISessionContext, |
| 11 |
DOMUtils, |
| 12 |
IToolbarWidgetRegistry, |
| 13 |
} from '@jupyterlab/apputils'; |
| 14 |
|
| 15 |
import { Cell, CodeCell } from '@jupyterlab/cells'; |
| 16 |
|
| 17 |
import { Text, Time } from '@jupyterlab/coreutils'; |
| 18 |
|
| 19 |
import { IDocumentManager } from '@jupyterlab/docmanager'; |
| 20 |
|
| 21 |
import { IMainMenu } from '@jupyterlab/mainmenu'; |
| 22 |
|
| 23 |
import { |
| 24 |
NotebookPanel, |
| 25 |
INotebookTracker, |
| 26 |
INotebookTools, |
| 27 |
} from '@jupyterlab/notebook'; |
| 28 |
|
| 29 |
import { ISettingRegistry } from '@jupyterlab/settingregistry'; |
| 30 |
|
| 31 |
import { ITranslator, nullTranslator } from '@jupyterlab/translation'; |
| 32 |
|
| 33 |
import { INotebookShell } from '@jupyter-notebook/application'; |
| 34 |
|
| 35 |
import { Poll } from '@lumino/polling'; |
| 36 |
|
| 37 |
import { Widget } from '@lumino/widgets'; |
| 38 |
|
| 39 |
import { TrustedComponent } from './trusted'; |
| 40 |
|
| 41 |
/** |
| 42 |
* The class for kernel status errors. |
| 43 |
*/ |
| 44 |
const KERNEL_STATUS_ERROR_CLASS = 'jp-NotebookKernelStatus-error'; |
| 45 |
|
| 46 |
/** |
| 47 |
* The class for kernel status warnings. |
| 48 |
*/ |
| 49 |
const KERNEL_STATUS_WARN_CLASS = 'jp-NotebookKernelStatus-warn'; |
| 50 |
|
| 51 |
/** |
| 52 |
* The class for kernel status infos. |
| 53 |
*/ |
| 54 |
const KERNEL_STATUS_INFO_CLASS = 'jp-NotebookKernelStatus-info'; |
| 55 |
|
| 56 |
/** |
| 57 |
* The class to fade out the kernel status. |
| 58 |
*/ |
| 59 |
const KERNEL_STATUS_FADE_OUT_CLASS = 'jp-NotebookKernelStatus-fade'; |
| 60 |
|
| 61 |
/** |
| 62 |
* The class for scrolled outputs |
| 63 |
*/ |
| 64 |
const SCROLLED_OUTPUTS_CLASS = 'jp-mod-outputsScrolled'; |
| 65 |
|
| 66 |
/** |
| 67 |
* A plugin for the checkpoint indicator |
| 68 |
*/ |
| 69 |
const checkpoints: JupyterFrontEndPlugin<void> = { |
| 70 |
id: '@jupyter-notebook/notebook-extension:checkpoints', |
| 71 |
autoStart: true, |
| 72 |
requires: [IDocumentManager, ITranslator], |
| 73 |
optional: [INotebookShell, IToolbarWidgetRegistry], |
| 74 |
activate: ( |
| 75 |
app: JupyterFrontEnd, |
| 76 |
docManager: IDocumentManager, |
| 77 |
translator: ITranslator, |
| 78 |
notebookShell: INotebookShell | null, |
| 79 |
toolbarRegistry: IToolbarWidgetRegistry | null |
| 80 |
) => { |
| 81 |
const { shell } = app; |
| 82 |
const trans = translator.load('notebook'); |
| 83 |
const node = document.createElement('div'); |
| 84 |
|
| 85 |
if (toolbarRegistry) { |
| 86 |
toolbarRegistry.addFactory('TopBar', 'checkpoint', (toolbar) => { |
| 87 |
const widget = new Widget({ node }); |
| 88 |
widget.id = DOMUtils.createDomID(); |
| 89 |
widget.addClass('jp-NotebookCheckpoint'); |
| 90 |
return widget; |
| 91 |
}); |
| 92 |
} |
| 93 |
|
| 94 |
const onChange = async () => { |
| 95 |
const current = shell.currentWidget; |
| 96 |
if (!current) { |
| 97 |
return; |
| 98 |
} |
| 99 |
const context = docManager.contextForWidget(current); |
| 100 |
|
| 101 |
context?.fileChanged.disconnect(onChange); |
| 102 |
context?.fileChanged.connect(onChange); |
| 103 |
|
| 104 |
const checkpoints = await context?.listCheckpoints(); |
| 105 |
if (!checkpoints) { |
| 106 |
return; |
| 107 |
} |
| 108 |
const checkpoint = checkpoints[checkpoints.length - 1]; |
| 109 |
node.textContent = trans.__( |
| 110 |
'Last Checkpoint: %1', |
| 111 |
Time.formatHuman(new Date(checkpoint.last_modified)) |
| 112 |
); |
| 113 |
}; |
| 114 |
|
| 115 |
if (notebookShell) { |
| 116 |
notebookShell.currentChanged.connect(onChange); |
| 117 |
} |
| 118 |
|
| 119 |
new Poll({ |
| 120 |
auto: true, |
| 121 |
factory: () => onChange(), |
| 122 |
frequency: { |
| 123 |
interval: 2000, |
| 124 |
backoff: false, |
| 125 |
}, |
| 126 |
standby: 'when-hidden', |
| 127 |
}); |
| 128 |
}, |
| 129 |
}; |
| 130 |
|
| 131 |
/** |
| 132 |
* Add a command to close the browser tab when clicking on "Close and Shut Down" |
| 133 |
*/ |
| 134 |
const closeTab: JupyterFrontEndPlugin<void> = { |
| 135 |
id: '@jupyter-notebook/notebook-extension:close-tab', |
| 136 |
autoStart: true, |
| 137 |
requires: [IMainMenu], |
| 138 |
optional: [ITranslator], |
| 139 |
activate: ( |
| 140 |
app: JupyterFrontEnd, |
| 141 |
menu: IMainMenu, |
| 142 |
translator: ITranslator | null |
| 143 |
) => { |
| 144 |
const { commands } = app; |
| 145 |
translator = translator ?? nullTranslator; |
| 146 |
const trans = translator.load('notebook'); |
| 147 |
|
| 148 |
const id = 'notebook:close-and-halt'; |
| 149 |
commands.addCommand(id, { |
| 150 |
label: trans.__('Close and Shut Down Notebook'), |
| 151 |
execute: async () => { |
| 152 |
await commands.execute('notebook:close-and-shutdown'); |
| 153 |
window.close(); |
| 154 |
}, |
| 155 |
}); |
| 156 |
menu.fileMenu.closeAndCleaners.add({ |
| 157 |
id, |
| 158 |
// use a small rank to it takes precedence over the default |
| 159 |
// shut down action for the notebook |
| 160 |
rank: 0, |
| 161 |
}); |
| 162 |
}, |
| 163 |
}; |
| 164 |
|
| 165 |
/** |
| 166 |
* The kernel logo plugin. |
| 167 |
*/ |
| 168 |
const kernelLogo: JupyterFrontEndPlugin<void> = { |
| 169 |
id: '@jupyter-notebook/notebook-extension:kernel-logo', |
| 170 |
autoStart: true, |
| 171 |
requires: [INotebookShell], |
| 172 |
optional: [IToolbarWidgetRegistry], |
| 173 |
activate: ( |
| 174 |
app: JupyterFrontEnd, |
| 175 |
shell: INotebookShell, |
| 176 |
toolbarRegistry: IToolbarWidgetRegistry | null |
| 177 |
) => { |
| 178 |
const { serviceManager } = app; |
| 179 |
|
| 180 |
const node = document.createElement('div'); |
| 181 |
const img = document.createElement('img'); |
| 182 |
|
| 183 |
const onChange = async () => { |
| 184 |
const current = shell.currentWidget; |
| 185 |
if (!(current instanceof NotebookPanel)) { |
| 186 |
return; |
| 187 |
} |
| 188 |
|
| 189 |
if (!node.hasChildNodes()) { |
| 190 |
node.appendChild(img); |
| 191 |
} |
| 192 |
|
| 193 |
await current.sessionContext.ready; |
| 194 |
current.sessionContext.kernelChanged.disconnect(onChange); |
| 195 |
current.sessionContext.kernelChanged.connect(onChange); |
| 196 |
|
| 197 |
const name = current.sessionContext.session?.kernel?.name ?? ''; |
| 198 |
const spec = serviceManager.kernelspecs?.specs?.kernelspecs[name]; |
| 199 |
if (!spec) { |
| 200 |
node.childNodes[0].remove(); |
| 201 |
return; |
| 202 |
} |
| 203 |
|
| 204 |
const kernelIconUrl = spec.resources['logo-64x64']; |
| 205 |
if (!kernelIconUrl) { |
| 206 |
node.childNodes[0].remove(); |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
img.src = kernelIconUrl; |
| 211 |
img.title = spec.display_name; |
| 212 |
}; |
| 213 |
|
| 214 |
if (toolbarRegistry) { |
| 215 |
toolbarRegistry.addFactory('TopBar', 'kernelLogo', (toolbar) => { |
| 216 |
const widget = new Widget({ node }); |
| 217 |
widget.addClass('jp-NotebookKernelLogo'); |
| 218 |
return widget; |
| 219 |
}); |
| 220 |
} |
| 221 |
|
| 222 |
app.started.then(() => { |
| 223 |
shell.currentChanged.connect(onChange); |
| 224 |
}); |
| 225 |
}, |
| 226 |
}; |
| 227 |
|
| 228 |
/** |
| 229 |
* A plugin to display the kernel status; |
| 230 |
*/ |
| 231 |
const kernelStatus: JupyterFrontEndPlugin<void> = { |
| 232 |
id: '@jupyter-notebook/notebook-extension:kernel-status', |
| 233 |
autoStart: true, |
| 234 |
requires: [INotebookShell, ITranslator], |
| 235 |
activate: ( |
| 236 |
app: JupyterFrontEnd, |
| 237 |
shell: INotebookShell, |
| 238 |
translator: ITranslator |
| 239 |
) => { |
| 240 |
const trans = translator.load('notebook'); |
| 241 |
const widget = new Widget(); |
| 242 |
widget.addClass('jp-NotebookKernelStatus'); |
| 243 |
app.shell.add(widget, 'menu', { rank: 10_010 }); |
| 244 |
|
| 245 |
const removeClasses = () => { |
| 246 |
widget.removeClass(KERNEL_STATUS_ERROR_CLASS); |
| 247 |
widget.removeClass(KERNEL_STATUS_WARN_CLASS); |
| 248 |
widget.removeClass(KERNEL_STATUS_INFO_CLASS); |
| 249 |
widget.removeClass(KERNEL_STATUS_FADE_OUT_CLASS); |
| 250 |
}; |
| 251 |
|
| 252 |
const onStatusChanged = (sessionContext: ISessionContext) => { |
| 253 |
const status = sessionContext.kernelDisplayStatus; |
| 254 |
let text = `Kernel ${Text.titleCase(status)}`; |
| 255 |
removeClasses(); |
| 256 |
switch (status) { |
| 257 |
case 'busy': |
| 258 |
case 'idle': |
| 259 |
text = ''; |
| 260 |
widget.addClass(KERNEL_STATUS_FADE_OUT_CLASS); |
| 261 |
break; |
| 262 |
case 'dead': |
| 263 |
case 'terminating': |
| 264 |
widget.addClass(KERNEL_STATUS_ERROR_CLASS); |
| 265 |
break; |
| 266 |
case 'unknown': |
| 267 |
widget.addClass(KERNEL_STATUS_WARN_CLASS); |
| 268 |
break; |
| 269 |
default: |
| 270 |
widget.addClass(KERNEL_STATUS_INFO_CLASS); |
| 271 |
widget.addClass(KERNEL_STATUS_FADE_OUT_CLASS); |
| 272 |
break; |
| 273 |
} |
| 274 |
widget.node.textContent = trans.__(text); |
| 275 |
}; |
| 276 |
|
| 277 |
const onChange = async () => { |
| 278 |
const current = shell.currentWidget; |
| 279 |
if (!(current instanceof NotebookPanel)) { |
| 280 |
return; |
| 281 |
} |
| 282 |
const sessionContext = current.sessionContext; |
| 283 |
sessionContext.statusChanged.connect(onStatusChanged); |
| 284 |
}; |
| 285 |
|
| 286 |
shell.currentChanged.connect(onChange); |
| 287 |
}, |
| 288 |
}; |
| 289 |
|
| 290 |
/** |
| 291 |
* A plugin to enable scrolling for outputs by default. |
| 292 |
* Mimic the logic from the classic notebook, as found here: |
| 293 |
* https://github.com/jupyter/notebook/blob/a9a31c096eeffe1bff4e9164c6a0442e0e13cdb3/notebook/static/notebook/js/outputarea.js#L96-L120 |
| 294 |
*/ |
| 295 |
const scrollOutput: JupyterFrontEndPlugin<void> = { |
| 296 |
id: '@jupyter-notebook/notebook-extension:scroll-output', |
| 297 |
autoStart: true, |
| 298 |
requires: [INotebookTracker], |
| 299 |
optional: [ISettingRegistry], |
| 300 |
activate: async ( |
| 301 |
app: JupyterFrontEnd, |
| 302 |
tracker: INotebookTracker, |
| 303 |
settingRegistry: ISettingRegistry | null |
| 304 |
) => { |
| 305 |
const autoScrollThreshold = 100; |
| 306 |
let autoScrollOutputs = true; |
| 307 |
|
| 308 |
// decide whether to scroll the output of the cell based on some heuristics |
| 309 |
const autoScroll = (cell: CodeCell) => { |
| 310 |
if (!autoScrollOutputs) { |
| 311 |
// bail if disabled via the settings |
| 312 |
return; |
| 313 |
} |
| 314 |
const { outputArea } = cell; |
| 315 |
// respect cells with an explicit scrolled state |
| 316 |
const scrolled = cell.model.getMetadata('scrolled'); |
| 317 |
if (scrolled !== undefined) { |
| 318 |
return; |
| 319 |
} |
| 320 |
const { node } = outputArea; |
| 321 |
const height = node.scrollHeight; |
| 322 |
const fontSize = parseFloat(node.style.fontSize.replace('px', '')); |
| 323 |
const lineHeight = (fontSize || 14) * 1.3; |
| 324 |
// do not set via cell.outputScrolled = true, as this would |
| 325 |
// otherwise synchronize the scrolled state to the notebook metadata |
| 326 |
const scroll = height > lineHeight * autoScrollThreshold; |
| 327 |
cell.toggleClass(SCROLLED_OUTPUTS_CLASS, scroll); |
| 328 |
}; |
| 329 |
|
| 330 |
const handlers: { [id: string]: () => void } = {}; |
| 331 |
|
| 332 |
const setAutoScroll = (cell: Cell) => { |
| 333 |
if (cell.model.type === 'code') { |
| 334 |
const codeCell = cell as CodeCell; |
| 335 |
const id = codeCell.model.id; |
| 336 |
autoScroll(codeCell); |
| 337 |
if (handlers[id]) { |
| 338 |
codeCell.outputArea.model.changed.disconnect(handlers[id]); |
| 339 |
} |
| 340 |
handlers[id] = () => autoScroll(codeCell); |
| 341 |
codeCell.outputArea.model.changed.connect(handlers[id]); |
| 342 |
} |
| 343 |
}; |
| 344 |
|
| 345 |
tracker.widgetAdded.connect((sender, notebook) => { |
| 346 |
// when the notebook widget is created, process all the cells |
| 347 |
notebook.sessionContext.ready.then(() => { |
| 348 |
notebook.content.widgets.forEach(setAutoScroll); |
| 349 |
}); |
| 350 |
|
| 351 |
notebook.model?.cells.changed.connect((sender, args) => { |
| 352 |
notebook.content.widgets.forEach(setAutoScroll); |
| 353 |
}); |
| 354 |
}); |
| 355 |
|
| 356 |
if (settingRegistry) { |
| 357 |
const loadSettings = settingRegistry.load(scrollOutput.id); |
| 358 |
const updateSettings = (settings: ISettingRegistry.ISettings): void => { |
| 359 |
autoScrollOutputs = settings.get('autoScrollOutputs') |
| 360 |
.composite as boolean; |
| 361 |
}; |
| 362 |
|
| 363 |
Promise.all([loadSettings, app.restored]) |
| 364 |
.then(([settings]) => { |
| 365 |
updateSettings(settings); |
| 366 |
settings.changed.connect((settings) => { |
| 367 |
updateSettings(settings); |
| 368 |
}); |
| 369 |
}) |
| 370 |
.catch((reason: Error) => { |
| 371 |
console.error(reason.message); |
| 372 |
}); |
| 373 |
} |
| 374 |
}, |
| 375 |
}; |
| 376 |
|
| 377 |
/** |
| 378 |
* A plugin to add the NotebookTools to the side panel; |
| 379 |
*/ |
| 380 |
const notebookToolsWidget: JupyterFrontEndPlugin<void> = { |
| 381 |
id: '@jupyter-notebook/notebook-extension:notebook-tools', |
| 382 |
autoStart: true, |
| 383 |
requires: [INotebookShell], |
| 384 |
optional: [INotebookTools], |
| 385 |
activate: ( |
| 386 |
app: JupyterFrontEnd, |
| 387 |
shell: INotebookShell, |
| 388 |
notebookTools: INotebookTools | null |
| 389 |
) => { |
| 390 |
const onChange = async () => { |
| 391 |
const current = shell.currentWidget; |
| 392 |
if (!(current instanceof NotebookPanel)) { |
| 393 |
return; |
| 394 |
} |
| 395 |
|
| 396 |
// Add the notebook tools in right area. |
| 397 |
if (notebookTools) { |
| 398 |
shell.add(notebookTools, 'right', { type: 'Property Inspector' }); |
| 399 |
} |
| 400 |
}; |
| 401 |
shell.currentChanged.connect(onChange); |
| 402 |
}, |
| 403 |
}; |
| 404 |
|
| 405 |
/** |
| 406 |
* A plugin that adds a Trusted indicator to the menu area |
| 407 |
*/ |
| 408 |
const trusted: JupyterFrontEndPlugin<void> = { |
| 409 |
id: '@jupyter-notebook/notebook-extension:trusted', |
| 410 |
autoStart: true, |
| 411 |
requires: [INotebookShell, ITranslator], |
| 412 |
activate: ( |
| 413 |
app: JupyterFrontEnd, |
| 414 |
notebookShell: INotebookShell, |
| 415 |
translator: ITranslator |
| 416 |
): void => { |
| 417 |
const onChange = async () => { |
| 418 |
const current = notebookShell.currentWidget; |
| 419 |
if (!(current instanceof NotebookPanel)) { |
| 420 |
return; |
| 421 |
} |
| 422 |
|
| 423 |
const notebook = current.content; |
| 424 |
await current.context.ready; |
| 425 |
|
| 426 |
const widget = TrustedComponent.create({ notebook, translator }); |
| 427 |
notebookShell.add(widget, 'menu', { |
| 428 |
rank: 11_000, |
| 429 |
}); |
| 430 |
}; |
| 431 |
|
| 432 |
notebookShell.currentChanged.connect(onChange); |
| 433 |
}, |
| 434 |
}; |
| 435 |
|
| 436 |
/** |
| 437 |
* Export the plugins as default. |
| 438 |
*/ |
| 439 |
const plugins: JupyterFrontEndPlugin<any>[] = [ |
| 440 |
checkpoints, |
| 441 |
closeTab, |
| 442 |
kernelLogo, |
| 443 |
kernelStatus, |
| 444 |
scrollOutput, |
| 445 |
notebookToolsWidget, |
| 446 |
trusted, |
| 447 |
]; |
| 448 |
|
| 449 |
export default plugins; |