Created
March 2, 2015 01:33
-
-
Save johnpauljanecek/bcb6a4fd32f1ea11f813 to your computer and use it in GitHub Desktop.
ipython extension used with epc-python.el
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
epc_server.py | |
""" | |
from epc.server import EPCServer | |
from epc.server import ThreadingEPCServer | |
import threading | |
__version__ = "0.1" | |
def load_ipython_extension(ipython): | |
# The `ipython` argument is the currently active `InteractiveShell` | |
# instance, which can be used in any way. This allows you to register | |
# new magics or aliases, for example. | |
print "loaded %s" % (__version__,) | |
server = ThreadingEPCServer(('localhost',0)) | |
print "server port" | |
server.print_port() | |
print ipython.get_ipython() | |
server_thread = threading.Thread(target=server.serve_forever) | |
server_thread.allow_reuse_address = True | |
def ev(exp): | |
ipython.ex(exp) | |
return True | |
server.register_function(ev) | |
def ex(exp): | |
ipython.ex(exp) | |
return True | |
server.register_function(ex) | |
def run_cell(raw_cell,strip_leading = True): | |
if strip_leading : | |
lines = raw_cell.splitlines() | |
leading = len(lines[0]) - len(lines[0].lstrip()) | |
raw_cell = "\n".join(map(lambda l : l[leading:],lines)) | |
ipython.run_cell(raw_cell,store_history=True) | |
return True | |
server.register_function(run_cell) | |
# set_next_input(s) | |
def set_next_input(s): | |
ipython.set_next_input(s) | |
return True | |
server.register_function(set_next_input) | |
def write(s): | |
ipython.write(s) | |
return True | |
server.register_function(write) | |
server.register_function(ipython.write_err,"write_err") | |
ipython.__epc_server = server | |
ipython.__epc_thread = server_thread | |
ipython.__epc_thread.start() | |
ipython.push({"epc_server" : server, | |
"epc_thread" : server_thread, | |
"ipython" : ipython | |
}) | |
def shutdown_hook(self): | |
print "shutdown_hook %s" % (ipython) | |
ipython.__epc_server.shutdown() | |
#import atexit | |
#atexit.register(shutdown_hook) | |
ipython.set_hook('shutdown_hook',shutdown_hook) | |
def unload_ipython_extension(ipython): | |
# If you want your extension to be unloadable, put that logic here. | |
print "unloaded %s" % (ipython.__epc_server,) | |
ipython.__epc_server.shutdown() | |
""" | |
used to load extension | |
%load_ext epc_server | |
%reload_ext epc_server | |
%unload_ext epc_server | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment