在外部编辑器中编辑IPython单元 [英] Edit IPython cell in an external editor

查看:110
本文介绍了在外部编辑器中编辑IPython单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在IPython笔记本中设置键盘快捷键非常好,它可以允许在外部编辑器(例如gvim)中编辑当前单元格的内容.也许只是将当前单元格的内容复制到一个临时文件中,在其上启动gvim,并在每次保存该文件时更新当前单元格(并在退出gvim时删除该临时文件).另外,如果通过浏览器编辑了单元格,则可能会更新临时文件,以便gvim知道文件已更改.

It would be great to have a keyboard short-cut in IPython notebook, which would allow to edit the content of the current cell in an external editor (e.g. gvim). Maybe just copy the content of the current cell into a temporary file, launch gvim on it, and update the current cell each time the file is saved (and delete the temporary file when exiting gvim). Also, maybe update the temporary file if the cell is edited from the browser, so that gvim knows the file has changed.

我知道像vim-ipython和ipython-vimception这样的项目,但是它们不符合我的需求.我认为浏览器足以应付简单的事情,但是当需要更强大的编辑功能时,就无需重新发明轮子了.

I am aware of projects like vim-ipython and ipython-vimception, but they don't correspond to my needs. I think the browser is enough for simple things, but when more powerful editing is required there is no need to reinvent the wheel.

您知道IPython笔记本中是否已经存在这样的功能?

Do you know if such a feature exists in IPython notebook already?

谢谢.

推荐答案

这是我想到的.我添加了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;
    }}
);

这篇关于在外部编辑器中编辑IPython单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆