From 7b936b542ddc4faef484ac539d331149c613add5 Mon Sep 17 00:00:00 2001 From: Jonathan Helmus Date: Sat, 20 Feb 2016 13:25:24 -0600 Subject: [PATCH 1/4] Connection with IPython/Jupyter 4.x Changes based on work by GitHub users deto in PR#144 --- ftplugin/python/vim_ipython.py | 40 ++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/ftplugin/python/vim_ipython.py b/ftplugin/python/vim_ipython.py index 0a4ed08..2969c1d 100644 --- a/ftplugin/python/vim_ipython.py +++ b/ftplugin/python/vim_ipython.py @@ -93,7 +93,10 @@ def new_ipy(s=''): new_ipy() """ - from IPython.kernel import KernelManager + try: + from jupyter_client import KernelManager + except ImportError: # for compatability with IPython 3 or lower + from IPython.kernel import KernelManager km = KernelManager() km.start_kernel() return km_from_string(km.connection_file) @@ -107,21 +110,26 @@ def km_from_string(s=''): import IPython except ImportError: raise ImportError("Could not find IPython. " + _install_instructions) - from IPython.config.loader import KeyValueConfigLoader + try: - from IPython.kernel import ( - KernelManager, - find_connection_file, - ) - except ImportError: - # IPython < 1.0 - from IPython.zmq.blockingkernelmanager import BlockingKernelManager as KernelManager - from IPython.zmq.kernelapp import kernel_aliases + from traitlets.config.loader import KeyValueConfigLoader + except ImportError: # IPython <= 3.0 + from IPython.config.loader import KeyValueConfigLoader + + try: + from jupyter_client import KernelManager, find_connection_file + except ImportError: # IPython <= 3.0 try: - from IPython.lib.kernel import find_connection_file + from IPython.kernel import KernelManager, find_connection_file except ImportError: - # < 0.12, no find_connection_file - pass + # IPython < 1.0 + from IPython.zmq.blockingkernelmanager import BlockingKernelManager as KernelManager + from IPython.zmq.kernelapp import kernel_aliases + try: + from IPython.lib.kernel import find_connection_file + except ImportError: + # < 0.12, no find_connection_file + pass global km, kc, send @@ -142,7 +150,11 @@ def km_from_string(s=''): p = p.lstrip().rstrip() # profile part of the string fullpath = find_connection_file(k,p) else: - fullpath = find_connection_file(s.lstrip().rstrip()) + s = s.lstrip().rstrip() + if len(s) == 0: + fullpath = find_connection_file() + else: + fullpath = find_connection_file(s.lstrip().rstrip()) except IOError as e: echo(":IPython " + s + " failed", "Info") echo("^-- failed '" + s + "' not found", "Error") From 69fbe89ffdc2f9999ae6266c2659f497918d7f6c Mon Sep 17 00:00:00 2001 From: Jonathan Helmus Date: Sat, 20 Feb 2016 13:29:03 -0600 Subject: [PATCH 2/4] Doc inspection for IPython/Jupyter 4.x --- ftplugin/python/vim_ipython.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ftplugin/python/vim_ipython.py b/ftplugin/python/vim_ipython.py index 2969c1d..deee873 100644 --- a/ftplugin/python/vim_ipython.py +++ b/ftplugin/python/vim_ipython.py @@ -244,7 +244,10 @@ def disconnect(): def get_doc(word, level=0): if kc is None: return ["Not connected to IPython, cannot query: %s" % word] - msg_id = kc.shell_channel.object_info(word, level) + if hasattr(kc, 'inspect'): # IPython/Jupyter 4.x + msg_id = kc.inspect(word, None, level) + else: # IPython <= 3 + msg_id = kc.shell_channel.object_info(word, level) doc = get_doc_msg(msg_id) # get around unicode problems when interfacing with vim return [d.encode(vim_encoding) for d in doc] From faeec9e5993ff1462b1ffd10f5d15a90c92291de Mon Sep 17 00:00:00 2001 From: Jonathan Helmus Date: Sat, 20 Feb 2016 13:36:04 -0600 Subject: [PATCH 3/4] Completions for IPython/Jupyter 4.x --- ftplugin/python/vim_ipython.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ftplugin/python/vim_ipython.py b/ftplugin/python/vim_ipython.py index deee873..f7b5a53 100644 --- a/ftplugin/python/vim_ipython.py +++ b/ftplugin/python/vim_ipython.py @@ -335,8 +335,11 @@ def get_doc_buffer(level=0): def ipy_complete(base, current_line, pos): # pos is the location of the start of base, add the length # to get the completion position - msg_id = kc.shell_channel.complete(base, current_line, - int(pos) + len(base) - 1) + if hasattr(kc, 'complete'): + msg_id = kc.complete(current_line, int(pos) + len(base) - 1) + else: + msg_id = kc.shell_channel.complete(base, current_line, + int(pos) + len(base) - 1) try: m = get_child_msg(msg_id) matches = m['content']['matches'] From f6aac07810a7135721e1e77dcbc304e81249e0bd Mon Sep 17 00:00:00 2001 From: Jonathan Helmus Date: Sat, 20 Feb 2016 13:38:17 -0600 Subject: [PATCH 4/4] Update README.rst to note support for IPython 4.x --- README.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index 26e556a..1b4bab8 100644 --- a/README.rst +++ b/README.rst @@ -2,9 +2,9 @@ vim-ipython ########### -A two-way integration between Vim and IPython. +A two-way integration between Vim and IPython/Jupyter. -IPython versions 0.11.x, 0.12.x, 0.13.x, 1.x, 2.x and 3.x +IPython/Jupyter versions 0.11.x, 0.12.x, 0.13.x, 1.x, 2.x, 3.x, 4.x * author: Paul Ivanov (http://pirsquared.org) * github: http://github.com/ivanov/vim-ipython @@ -294,6 +294,7 @@ pull request with your attribution. * @pydave for IPythonTerminate (sending SIGTERM using our hack) * @luispedro for IPythonNew * @jjhelmus and @wmvanvliet for IPython 3.x support. +* @jjhelmus and @deto for IPython/Jupyter 4.x support. Similar Projects ----------------