From 7d350bbc46f99c18331bd79ac96a1019f874abb5 2011-10-13 04:28:05 From: MinRK Date: 2011-10-13 04:28:05 Subject: [PATCH] add %connect_info magic for help connecting secondary clients --- diff --git a/IPython/zmq/zmqshell.py b/IPython/zmq/zmqshell.py index 8b287ca..75f7808 100644 --- a/IPython/zmq/zmqshell.py +++ b/IPython/zmq/zmqshell.py @@ -33,7 +33,7 @@ from IPython.utils import io from IPython.utils.jsonutil import json_clean from IPython.utils.path import get_py_filename from IPython.utils.traitlets import Instance, Type, Dict, CBool -from IPython.utils.warn import warn +from IPython.utils.warn import warn, error from IPython.zmq.displayhook import ZMQShellDisplayHook, _encode_binary from IPython.zmq.session import extract_header from session import Session @@ -427,6 +427,40 @@ class ZMQInteractiveShell(InteractiveShell): """Show a basic reference about the GUI console.""" from IPython.core.usage import gui_reference page.page(gui_reference, auto_html=True) + + def magic_connect_info(self, arg_s): + """Print information for connecting other clients to this kernel + + It will print the contents of this session's connection file, as well as + shortcuts for local clients. + + In the simplest case, when called from the most recently launched kernel, + secondary clients can be connected, simply with: + + $> ipython --existing + + """ + from IPython.zmq.kernelapp import KernelApp + if not KernelApp.initialized(): + error("Not KernelApp is not initialized. I cannot find the connection info") + return + app = KernelApp.instance() + try: + with open(app.connection_file) as f: + s = f.read() + except Exception as e: + error("Could not read connection file: %s" % e) + return + print (s + '\n') + print ("Paste the above JSON into a file, and connect with:\n" + " $> ipython --existing \n" + "or, if you are local, you can connect with just:\n" + " $> ipython --existing %s\n" + "or even just:\n" + " $> ipython --existing\n" + "if this is the most recent IPython session you have started." + % os.path.basename((app.connection_file)) + ) def set_next_input(self, text): """Send the specified text to the frontend to be presented at the next