有没有一种方法可以为jupyter笔记本中的新单元格设置默认标签?

时间:2019-05-30 15:10:17

标签: python jupyter-notebook

我需要在大多数单元格中添加标签“ nocell”,这有点烦人。有没有一种方法可以设置笔记本,以便在创建新单元时默认添加标签? 谢谢!

1 个答案:

答案 0 :(得分:0)

据我所知,没有内置的方法可以做到这一点。但是,您可以考虑创建一个jupyter extension来完成所需的工作。使用此功能,您可以构建一个扩展来标记带有标签的任何新添加的单元格,也可以创建一个新的按钮栏操作,当单击该按钮时,将插入带有标签的单元格。

更新

有关如何使用自定义Jupyter扩展程序添加的按钮准确执行此操作的更多详细信息。此tutorial给出了添加自定义扩展名的说明。您将需要稍微调整示例以使其能够执行所需的操作。具体来说,您应该调整main.js文件。必须遵循以下原则:

define([
    'base/js/namespace',
    'base/js/events'
], function (Jupyter, events) {

    var add_cell_tagged_nocell = function () {
        Jupyter.notebook.insert_cell_below();
        Jupyter.notebook.select_next();
        var cell = Jupyter.notebook.get_selected_cell();
        cell.metadata.tags = ["nocell"];
    };

    function load_ipython_extension() {
        // Button to add tagged cell
        Jupyter.toolbar.add_buttons_group([
            Jupyter.keyboard_manager.actions.register({
                'help': 'Add cell tagged nocell',
                'icon': 'fa-play-circle',
                'handler': add_cell_tagged_nocell
            }, 'add-tagged-cell', 'Tagged cell')
        ])
    }
    return {
        load_ipython_extension: load_ipython_extension
    };
});

然后,在加载扩展时,您将在按钮栏中获得一个附加按钮,以供单击。单击后,将在当前单元格下方插入一个新单元格,并带有标签“ nocell”。

enter image description here

enter image description here

存在一个已知的问题,如果显示标签,则单元格工具栏不会刷新,但是元数据在那里,如果再次隐藏并显示,标签工具栏将正确。

干杯!

相关问题