我已经能够通过在笔记本中注入javascript来实现这两个功能。下面是代码片段。在from IPython.display import HTML, Javascript, display
def initialize():
display(HTML(
'''
code_show = false;
function restart_run_all(){
IPython.notebook.kernel.restart();
setTimeout(function(){
IPython.notebook.execute_all_cells();
}, 10000)
}
function code_toggle() {
if (code_show) {
$('div.input').hide(200);
} else {
$('div.input').show(200);
}
code_show = !code_show
}
Click to toggle
Click to Restart and Run all Cells
'''
))
initialize()
restart_run_all()函数重新启动笔记本内核,然后在10秒后执行所有单元。可以根据需要调整超时函数的参数。在
code_toggle()函数用于切换笔记本中单元格的输入区域。它还提供了一个很好的动画,同时切换代码单元。在