Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SublimeREPL/config/Python/ipy_repl.py file for integrating Jupyter & IPython 6 in SublimeREPL console
import os
import json
import socket
import threading
activate_this = os.environ.get("SUBLIMEREPL_ACTIVATE_THIS", None)
# turn off pager
os.environ['TERM'] = 'emacs'
if activate_this:
with open(activate_this, "r") as f:
exec(f.read(), {"__file__": activate_this})
try:
import jupyter_console.app
JUPYTER = True
except ImportError:
JUPYTER = False
if not JUPYTER:
try:
import IPython
IPYTHON = True
version = IPython.version_info[0]
except ImportError:
# for virtualenvs w/o IPython
import code
code.InteractiveConsole().interact()
# Jupyter and IPython 4+
if JUPYTER or (IPYTHON and version > 3):
from traitlets.config.loader import Config
# all other versions
else:
from IPython.config.loader import Config
editor = "subl -w"
cfg = Config()
cfg.ZMQTerminalInteractiveShell.simple_prompt = True
cfg.InteractiveShell.readline_use = False
cfg.InteractiveShell.autoindent = True
cfg.InteractiveShell.colors = "NoColor"
cfg.InteractiveShell.editor = os.environ.get("SUBLIMEREPL_EDITOR", editor)
if JUPYTER:
app = jupyter_console.app.ZMQTerminalIPythonApp(config=cfg, user_ns={})
elif IPYTHON:
app = IPython.terminal.ipapp.TerminalIPythonApp(config=cfg, user_ns={})
app.initialize()
ac_port = int(os.environ.get("SUBLIMEREPL_AC_PORT", "0"))
ac_ip = os.environ.get("SUBLIMEREPL_AC_IP", "127.0.0.1")
if ac_port:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ac_ip, ac_port))
def read_netstring(s):
size = 0
while True:
ch = s.recv(1)
if ch == b':':
break
size = size * 10 + int(ch)
msg = b""
while size != 0:
msg += s.recv(size)
size -= len(msg)
ch = s.recv(1)
assert ch == b','
return msg
def send_netstring(sock, msg):
payload = b"".join([str(len(msg)).encode("ascii"), b':', msg.encode("utf-8"), b','])
sock.sendall(payload)
def complete(shell, req):
# Jupyter
if JUPYTER:
result = shell.Completer.complete_request(req['line'], req['cursor_pos'])
return (req['line'], result['matches'])
# Ipython 4
elif IPYTHON and version > 3:
return shell.complete(**req)
# Ipython 1-3
else:
return []
def handle():
while True:
msg = read_netstring(s).decode("utf-8")
try:
req = json.loads(msg)
result = complete(app.shell, req)
res = json.dumps(result)
send_netstring(s, res)
except Exception:
send_netstring(s, b"[]")
if ac_port:
t = threading.Thread(target=handle)
t.start()
app.start()
if ac_port:
s.close()
@dkapitan

This comment has been minimized.

Copy link

@dkapitan dkapitan commented Apr 5, 2018

@pe224:
thank you!

@cgjosephlee

This comment has been minimized.

Copy link

@cgjosephlee cgjosephlee commented Aug 27, 2018

Works great!

@MattDMo

This comment has been minimized.

Copy link

@MattDMo MattDMo commented Mar 16, 2020

This is awesome! I've incorporated your changes back into my gist, and I thank you profusely for your work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment