Jupyter:将单元格移到笔记本顶部

时间:2018-10-30 18:08:29

标签: javascript jupyter-notebook jupyter

通常,我在笔记本中途发现自己忘记了导入,而是想将其移动到笔记本的顶部(我试图保留大部分导入)。是否可以向~/.jupyter/custom/custom.js添加键盘快捷键,以将单元格移动到笔记本顶部?

目前,我是通过剪切单元格,滚动到笔记本顶部,粘贴并向下滚动到我原来的位置(通常在返回的途中失去位置)来完成此操作的。

下面是fastai forums中的一些代码,可以完成不同的任务:转到运行中的单元格:

Jupyter.keyboard_manager.command_shortcuts.add_shortcut('CMD-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;
    }
});

screenshot of problem

1 个答案:

答案 0 :(得分:2)

部分记录;但是,您必须了解一点 JavaScript 才能创建键盘快捷键。

如您所见,编辑 custom.js 以绑定键盘快捷键,如 mentioned in the documentation(如果操作不是内置的,则必须使用第二种方法)

对于文档,read the source code(在线或在 site-packages/notebook/static/notebook/js/*.js 中,如果您在本地安装了 jupyter)。特别是,actions.js 包含示例绑定,notebook.js 包含您可以在笔记本上调用的函数。

这是一个例子。它有修改 jupyter 剪贴板的缺点。为避免这种情况,您可以查看 notebook.move_cell_down 函数的源代码以了解其实现方式,并进行相应修改。

Jupyter.keyboard_manager.command_shortcuts.add_shortcut('cmdtrl-I', {
    help : 'Move cell to first in notebook',
    handler : function (env) {
        var index = env.notebook.get_selected_index();
        env.notebook.cut_cell();
        env.notebook.select(0);
        env.notebook.paste_cell_above();
        env.notebook.select(index);
    }
});
相关问题