Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edit IPython cell in an external editor

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.

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.

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

Thanks.

like image 822
David Brochart Avatar asked Feb 03 '15 21:02

David Brochart


3 Answers

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

  • 'g' to launch gvim with the content of the current cell (you can replace gvim with whatever text editor you like).
  • 'u' to update the content of the current cell with what was saved by gvim.

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;
    }}
);
like image 153
David Brochart Avatar answered Oct 08 '22 09:10

David Brochart


For people who find this question who are using the IPython terminal application, there is a keyboard shortcut built in which launches $EDITOR with the contents of the current cell. Saving and exiting the editor replaces (but does not yet execute) the contents of the cell with that of the saved file.

The default keyboard shortcut is the F2 key. This corresponds to the IPython setting IPython.terminal.shortcuts.open_input_in_editor.

like image 45
Jake Lishman Avatar answered Oct 08 '22 11:10

Jake Lishman


Building off of the accepted answer by @david-brochart, I've taken his code and wrapped it up into a magic function so now I only need to run the line magic%gvim in a notebook in order to enable editing any cell's contents via Gvim for the entire notebook (and I can reuse the same line magic in any other notebook running on my system).

If you'd like to do something similar, just create a file named something like my_magic_functions.py inside your ipython startup folder (your ipython startup path is likely similar to ~/.ipython/profile_default/startup) and then put the following code inside that file (and save it):

import IPython.core.magic as ipym
from IPython import get_ipython

@ipym.magics_class
class MareBearMagics(ipym.Magics):
    @ipym.line_magic
    def gvim(self, line):
        cell_text = """
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 = get_ipython()
        ipython.run_cell_magic(
            magic_name='javascript', line=None, cell=cell_text)
        print("Cell contents can now be edited via Gvim. From command mode "
              "use 'g' to open current cell contents in Gvim. After ':wq' "
              "from Gvim, use 'u' in command mode to update cell contents.")


if __name__ == '__main__':
    get_ipython().register_magics(MareBearMagics)

Now start up your Jupyter notebook kernel, and you should be able to type %gvim% into a cell (and you can use auto-completion to find the new magic command) and then run the cell to enable editing the notebook's cell contents in Gvim. You'll receive an output message that lets you know the magic command took effect:

Cell contents can now be edited via Gvim. From command mode use 'g' to open current cell contents in Gvim. After ':wq' from Gvim, use 'u' in command mode to update cell contents.

Thanks to the folks in this stackoverflow question and these as well for giving me the ingredients to pull this together. :-)

  • How to run an IPython magic from a script (or timing a Python script)
  • How do I define custom magics in jupyter?
like image 28
MarenstainBear Avatar answered Oct 08 '22 11:10

MarenstainBear