gpt4 book ai didi

google-chrome - 在笔记本中保存 <iframe> 对象的屏幕截图?

转载 作者:行者123 更新时间:2023-12-04 07:10:51 27 4
gpt4 key购买 nike

我正在插入 <iframe ...>引用远程网站并在 Chrome 中的笔记本中为我提供交互式可视化的对象(来自 Simple way to visualize a TensorFlow graph in Jupyter?)。这样做的缺点是,当我关闭笔记本,然后再次打开它时,所有的可视化都被空白替换。

我怀疑显示它需要在 iframe 中重新执行 javascript 代码.

另一个缺点是,当托管此笔记本(即 github)时,所有此类单元格都是空的,因此静态图像方法比重新执行 javascript 更具可移植性。

有人能看到一种方法来保存这些单元格的屏幕截图并将它们嵌入到笔记本中吗?也许像 camera_ready(14)它将嵌入来自输出单元格 14 的可视化静态屏幕截图。

最佳答案

PhantomJS可以帮助您进行屏幕截图。当你的 notebook 正在运行时,PhantomJS 浏览器会打开它并对指定的输出单元格进行截图:

PhantomJS 的 JS 文件:

// 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();
});
}
});

在脚本中指定笔记本地址、身份验证 token 和单元格编号,然后使用 phantomjs 运行它:
phantomjs script.js

关于google-chrome - 在笔记本中保存 &lt;iframe&gt; 对象的屏幕截图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42286090/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com