且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在外部编辑器中编辑IPython单元

更新时间:2023-08-28 23:38:34

这是我想到的.我添加了2个快捷方式:

This is what I came up with. I added 2 shortcuts:

  • 'g'以使用当前单元格的内容启动gvim(您可以使用任何喜欢的文本编辑器替换gvim).
  • 'u'用gvim保存的内容更新当前单元格的内容.

因此,当您要使用首选的编辑器编辑单元格时,请按"g",对单元格进行所需的更改,将文件保存在编辑器中(然后退出),然后按"u".

So, when you want to edit the cell with your preferred editor, hit 'g', make the changes you want to the cell, save the file in your editor (and quit), then hit 'u'.

只需执行此单元格即可启用以下功能:

Just execute this cell to enable these features:

%%javascript

IPython.keyboard_manager.command_shortcuts.add_shortcut('g', {
    handler : function (event) {
        var input = IPython.notebook.get_selected_cell().get_text();
        var cmd = "f = open('.toto.py', 'w');f.close()";
        if (input != "") {
            cmd = '%%writefile .toto.py\n' + input;
        }
        IPython.notebook.kernel.execute(cmd);
        cmd = "import os;os.system('gvim .toto.py')";
        IPython.notebook.kernel.execute(cmd);
        return false;
    }}
);

IPython.keyboard_manager.command_shortcuts.add_shortcut('u', {
    handler : function (event) {
        function handle_output(msg) {
            var ret = msg.content.text;
            IPython.notebook.get_selected_cell().set_text(ret);
        }
        var callback = {'output': handle_output};
        var cmd = "f = open('.toto.py', 'r');print(f.read())";
        IPython.notebook.kernel.execute(cmd, {iopub: callback}, {silent: false});
        return false;
    }}
);