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

标签 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

最佳答案

它有部分记录;但是,您必须了解一点 JavaScript 才能创建键盘快捷键。
如您所知,编辑 custom.js 绑定(bind)键盘快捷键,如mentioned in the documentation (如果 Action 不是内置的,则必须使用第二种方法)
对于文档,read the source code (在线或 site-packages/notebook/static/notebook/js/*.js 如果您在本地安装了 jupyter)。特别是,actions.js包含示例绑定(bind)和 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);
    }
});

关于javascript - Jupyter:将单元格移动到笔记本顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53070389/

相关文章:

javascript - 如果单击单选按钮,如何使不可见的文本框可见?

python - Pandas:在列值中查找空格+不常用字符的最快方法是什么?

python - 如何从 Jupyter notebook 获取操作系统命令的输出?

javascript - Jupyter Notebook但适用于HTML/JS/CSS?

python - 在numba中编译abs()比普通的python函数慢

python - Jupyter 笔记本 : connection to kernel restarts infinitely

python - jupyter lab 404错误如何解决

javascript - jQuery 包装全部 : preserving hierarchy of DOM elements

javascript - 替代 Javascript 上的延迟函数

javascript - 读取选择框的值