I'm inserting <iframe ...>
objects that reference remote website and give me an interactive visualization in notebook in Chrome (from Simple way to visualize a TensorFlow graph in Jupyter?) . The downside of this is that when I close the notebook, and then open it again, all the visualizations are replaced with empty whitespace.
I suspect showing it requires re-executing javascript code in the iframe
.
Another downside is that when hosting this notebook (ie, github), all such cells are empty, so a static image approach is more portable than re-executing javascript.
Can someone see a way to save screenshots of these cells and embedding them into notebook? Maybe something like camera_ready(14)
which will embed static screenshot of visualization from output cell 14.
The easiest way to take a screenshot on Windows 10 or Windows 11 is with the Print Screen (PrtScn) key. To capture your entire screen, simply press PrtScn on the upper-right side of your keyboard. In Windows 10, the screenshot will be copied to your clipboard.
Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable.
It should run on Jupyter notebooks 4.0 and greater. Open jupyter-shot. ipynb and run the cells. You should then be able to select any cell and press r to see a screenshot of the cell appear in a new window with an Imgur url.
PhantomJS can help you with screen capture. While your notebook is running, PhantomJS browser will open it and make a screenshot of a specified output cell:
JS file for PhantomJS:
// address of the notebook
var address = "http://localhost:8888/notebooks/Untitled.ipynb";
// auth token from Jupyter console
var authToken = "af6bc1d90688bb6c26aeb206b8690e4855d27ef8d265b1bc";
// cell number with a widget output
var cellNumber = 10;
// this function is used to verify that a page is fully loaded
// source: https://github.com/ariya/phantomjs/blob/master/examples/waitfor.js
function waitFor(testFx, onReady, timeOutMillis) {
var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3000,
start = new Date().getTime(),
condition = false,
interval = setInterval(function() {
if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
// If not time-out yet and condition not yet fulfilled
condition = (typeof(testFx) === "string" ? eval(testFx) : testFx());
} else {
if(!condition) {
// If condition still not fulfilled (timeout but condition is 'false')
console.log("'waitFor()' timeout");
phantom.exit(1);
} else {
// Condition fulfilled (timeout and/or condition is 'true')
console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
typeof(onReady) === "string" ? eval(onReady) : onReady();
clearInterval(interval); //< Stop this interval
}
}
}, 250); //< repeat check every 250ms
};
// log in to a notebook using a token
function logIn() {
console.log("Logging in");
page.evaluate(function(token) {
document.forms[0].password.value = token;
document.forms[0].submit();
}, authToken);
}
// wait for a notebook to fully load, find the
// needed output cell and save it as a PNG file
function saveAsPNG() {
console.log("Saving PNG")
// Wait for 'notebook-container' to be visible
waitFor(function() {
// Check in the page if a specific element is now visible
return page.evaluate(function() {
return $("#notebook-container").is(":visible");
});
}, function() {
console.log("The notebook-container element should be visible now.");
var clipRect = page.evaluate(function(cell){
// we are selecting only the output cell
var searchStr = 'div.input_prompt:contains("[' + cell + ']:")';
var parentCell = $(searchStr).parents('div.cell')[0];
// get only the data div
var outputSubarea = $(parentCell).find('div.output_subarea')[0];
// get the coordinates of the data div
return outputSubarea.getBoundingClientRect()
}, cellNumber);
page.clipRect = {
top: clipRect.top,
left: clipRect.left,
width: clipRect.width,
height: clipRect.height
};
page.render('example.png');
phantom.exit();
});
}
var page = require('webpage').create();
// it seems, viewportSize should fully cover the
// the rendered div position, or nothing will be saved.
page.viewportSize = { width: 5000, height: 5000 };
page.open(address, function (status) {
// Check for page load success
if (status !== "success") {
console.log("Unable to open a page");
} else {
// Wait for 'password_input' to be visible
waitFor(function() {
// Check in the page if a specific element is now visible
return page.evaluate(function() {
return $("#password_input").is(":visible");
});
}, function() {
console.log("The password_input element should be visible now.");
logIn();
saveAsPNG();
});
}
});
Specify the address of your notebook, auth token and cell number in the script and run it with phantomjs:
phantomjs script.js
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With