Skip to content

Instantly share code, notes, and snippets.

@ruslangrimov
Last active February 18, 2022 14:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruslangrimov/1c0af96a6648526bf34442a109a32c5a to your computer and use it in GitHub Desktop.
Save ruslangrimov/1c0af96a6648526bf34442a109a32c5a to your computer and use it in GitHub Desktop.
Script to resume receiving cells output after a notebook reloaded or even opened in other browser
// This script makes it possible to resume cells output receiving after a notebook reloads.
// Works even when the notebook is open in other browser (output will be shown in all opened windows).
// Works with keras and tqdm (not with tqdm_notebook).
//
// Add this code into your ~/.jupyter/custom/custom.js
define([
'base/js/namespace',
'base/js/promises'
], function(Jupyter, promises) {
promises.app_initialized.then(function(appname) {
if (appname === 'NotebookApp') {
console.log("Adding hack to resume receiving output after notebook reloads");
IPython.notebook.events.on('execute.CodeCell', function(evt, data) {
// Put current message id into cell's metadata
data.cell._metadata['_last_msg_id'] = data.cell.last_msg_id;
// Force to save the notebook after a cell executed.
// However, it might be not desirable. Just comment line below
IPython.notebook.save_notebook();
});
IPython.notebook.events.on('received_unsolicited_message.Kernel', function(evt, data) {
// Get supposed receiver's message id
var p_msg_id = data.parent_header.msg_id;
var cells = IPython.notebook.get_cells();
// Try to find proper receiver
for (var i = 0; i < cells.length; i ++) {
if (cells[i]._metadata['_last_msg_id'] == p_msg_id) {
cell = cells[i];
console.log("Send unsolicited message to cell " + i);
// We use _* instead of * as a mark of being resumed by our hack
cell.set_input_prompt('_*');
cell.element.addClass("running");
cell.output_area.handle_output(data);
break;
}
}
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment