代码之家  ›  专栏  ›  技术社区  ›  multigoodverse

突出显示Jupyter单元格中的部分代码

  •  15
  • multigoodverse  · 技术社区  · 7 年前

    有没有办法突出显示朱庇特牢房的某些线条类似于下图的内容(我用照片编辑器创建的):

    enter image description here

    我的意思不是用光标来选择,而是永久的。 例如,当您想突出显示新添加的代码时,这对演示文稿很有用。

    1 回复  |  直到 7 年前
        1
  •  8
  •   Leon    7 年前

    下面提供的Jupyter笔记本扩展允许您突出显示代码单元格中的行范围按如下方式安装并启用:

    $ jupyter nbextension install codehighlighter.js --user
    $ jupyter nbextension enable codehighlighter --user
    

    然后一个按钮 lightbulb icon 将出现在您的Jupyter笔记本工具栏上。按该按钮将突出显示当前代码单元格中的选定行(如果没有选择,则突出显示当前行)。

    高亮显示将与笔记本一起保存(作为单元格元数据),但在笔记本被(重新)打开时不会自动启用要显示保存的高光,必须按 恢复高光 纽扣 bars icon ).


    codehighter.js代码

    define([
        'base/js/namespace'
    ], function(
        Jupyter
    ) {
        function load_ipython_extension() {
    
            var style = document.createElement('style');
            style.type = 'text/css';
            style.innerHTML = '.codehighlighter { background: yellow; }';
            document.getElementsByTagName('head')[0].appendChild(style);
    
            var highlight_code_in_cell = function (cell, from, to) {
                var cm = cell.code_mirror;
                for ( var lineno = from; lineno < to ; ++lineno )
                    cm.addLineClass(lineno, 'background', 'codehighlighter');
            }
    
            var highlight_selected_code = function () {
                var cell = Jupyter.notebook.get_selected_cell();
                var cm = cell.code_mirror;
                var from = cm.getCursor('from');
                var to = cm.getCursor('to');
                var endLine = (to.ch > 0 ? to.line + 1 : to.line);
                highlight_code_in_cell(cell, from.line, endLine);
                if ( ! cell.metadata.codehighlighter )
                    cell.metadata.codehighlighter = [];
                cell.metadata.codehighlighter.push([from.line, endLine]);
            };
    
            var highlight_from_metadata = function() {
                Jupyter.notebook.get_cells().forEach(function(cell) {
                    if (cell.metadata.codehighlighter) {
                        cell.metadata.codehighlighter.forEach(function(range) {
                            highlight_code_in_cell(cell, range[0], range[1]);
                        });
                    }
                });
            }
    
            function registerAction(action_name, action) {
                var prefix = 'codehighlighter';
                return Jupyter.actions.register(action, action_name, prefix);
            }
    
            var hilite_code = registerAction('highlight-code', {
                                             icon: 'fa-lightbulb-o',
                                             help    : 'Highlight selected code',
                                             help_index : 'zz',
                                             handler : highlight_selected_code
            });
            var restore_hilites = registerAction('restore-highlights', {
                                             icon: 'fa-bars',
                                             help    : 'Restore highlights',
                                             help_index : 'zz',
                                             handler : highlight_from_metadata
            });
    
            Jupyter.toolbar.add_buttons_group([hilite_code, restore_hilites]);
        }
    
        return {
            load_ipython_extension: load_ipython_extension
        };
    });