Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for IPython/Jupyter 4.x #151

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
----------------
Expand Down
52 changes: 35 additions & 17 deletions ftplugin/python/vim_ipython.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand All @@ -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())
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's not necessary to call s.lstrip().rstrip() again as It's already been done in line 153

except IOError as e:
echo(":IPython " + s + " failed", "Info")
echo("^-- failed '" + s + "' not found", "Error")
Expand Down Expand Up @@ -232,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]
Expand Down Expand Up @@ -320,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']
Expand Down