如何在IPython笔记本中阻止注释代码?

Wal*_*mly 32 ipython ipython-notebook

我已经在IPython笔记本中定义了一个函数,并希望能够阻止对它的一部分进行注释.直观地说,我希望能够突出显示一段代码,右键单击并选择注释掉选择,但这尚未实现.

有没有办法做到这一点?

Jak*_*kob 51

默认解决方案

在IPython 2.x和3.x (cmd|ctrl)-/工作但需要英语(美国)键盘布局,请参阅https://github.com/ipython/ipython/pull/3673.

其他键盘布局

如果您使用非英语键盘布局,则可以通过custom.js为codemirror编辑器定义自定义键绑定.为此,添加例如以下行

define([
    'base/js/namespace',
    'base/js/events'
    ],
    function(IPython, events) {
        events.on("app_initialized.NotebookApp",
            function () {
                IPython.Cell.options_default.cm_config.extraKeys = {"Ctrl-," : "toggleComment"};
            }
        );
    }
);
Run Code Online (Sandbox Code Playgroud)

使用Ctrl+ ,来切换(阻止)评论.我使用德语键盘布局和IPython 3.0.之前的解决方案(参见编辑)与chrome配合得很好,但不适用于firefox.

旧解决方案(IPython 1.x)

如果您使用IPython的1.x中,你可以尝试comment-uncomment.jshttps://github.com/ipython-contrib/IPython-notebook-extensions -我还没有尝试过这个,但我想它的一个良好的开端.

  • `找到.-name"custom.js"给了我多个结果,所以为了获得我的custom.js的路径,在笔记本中我做了`import notebook`然后`notebook .__ path__`,并添加到那个路径static/custom/custom. JS. (2认同)