当我在Jupyter笔记本中选择
运行全部
,运行以上全部
或运行以下全部
后,如何在Jupyter笔记本中跟随当前正在运行的单元格?也就是说,在整个笔记本的执行过程中,我希望显示给我的单元格是正在运行的单元格。运行全部
,运行以上全部
或运行以下全部
后,如何在Jupyter笔记本中跟随当前正在运行的单元格?也就是说,在整个笔记本的执行过程中,我希望显示给我的单元格是正在运行的单元格。将以下内容添加到~/.jupyter/custom/custom.js
中,并重新加载正在运行的笔记本:
/*
In Command mode Meta-[ toggles Follow Exec Cell mode, Meta-] turns it off.
To adjust the behavior you can adjust the arguments:
* behavior: One of "auto", "instant", or "smooth". Defaults to "auto". Defines the transition animation.
* block: One of "start", "center", "end", or "nearest". Defaults to "center".
* inline: One of "start", "center", "end", or "nearest". Defaults to "nearest".
https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView
*/
function scrollIntoRunningCell(evt, data) {
$('.running')[0].scrollIntoView({behavior: 'smooth', inline: 'center'});
}
Jupyter.keyboard_manager.command_shortcuts.add_shortcut('Meta-[', {
help: 'Follow Executing Cell On',
help_index: 'zz',
handler: function (event) {
Jupyter.notebook.events.on('finished_execute.CodeCell', scrollIntoRunningCell);
//console.log("Follow Executing Cell On")
return false;
}
});
Jupyter.keyboard_manager.command_shortcuts.add_shortcut('Meta-]', {
help: 'Follow Executing Cell Off',
help_index: 'zz',
handler: function (event) {
Jupyter.notebook.events.off('finished_execute.CodeCell', scrollIntoRunningCell);
//console.log("Follow Executing Cell Off")
return false;
}
});
Meta-[
将当前运行的单元格保持在屏幕中央,按下Meta-]
返回正常行为。~/.jupyter/custom/custom.js
并重新加载运行的笔记本后,使用Alt-I
跳转一次:// Alt-I: Go to Running cell shortcut [Command mode]
Jupyter.keyboard_manager.command_shortcuts.add_shortcut('Alt-I', {
help : 'Go to Running cell',
help_index : 'zz',
handler : function (event) {
setTimeout(function() {
// Find running cell and click the first one
if ($('.running').length > 0) {
//alert("found running cell");
$('.running')[0].scrollIntoView();
}}, 250);
return false;
}
});
注意:为了使其正常工作,所有部分都应该未折叠-否则它将不知道进入折叠的部分。
您可以根据自己的喜好调整激活快捷键。
请记住,这3个快捷方式只在命令模式下起作用(有关如何确定,请参见上文)。
已经测试过,在jupyter notebook 5.6.0和python 3.6.6中可以使用。