def test_find_connection_file_abspath(): with TemporaryDirectory() as d: cf = 'absolute.json' abs_cf = os.path.abspath(cf) with open(cf, 'w') as f: f.write('{}') assert connect.find_connection_file(abs_cf, path=jupyter_runtime_dir()) == abs_cf
def test_find_connection_file(): cfg = Config() with TemporaryDirectory() as d: cfg.ProfileDir.location = d cf = 'kernel.json' app = DummyConsoleApp(config=cfg, connection_file=cf) app.initialize() security_dir = app.runtime_dir profile_cf = os.path.join(security_dir, cf) with open(profile_cf, 'w') as f: f.write("{}") for query in ( 'kernel.json', 'kern*', '*ernel*', 'k*', ): nt.assert_equal( connect.find_connection_file(query, path=security_dir), profile_cf) JupyterApp._instance = None
def test_find_connection_file_abspath(): with TemporaryDirectory() as d: cf = 'absolute.json' abs_cf = os.path.abspath(cf) with open(cf, 'w') as f: f.write('{}') assert connect.find_connection_file( abs_cf, path=jupyter_runtime_dir()) == abs_cf
def test_find_connection_file_abspath(): with TemporaryDirectory(): cf = "absolute.json" abs_cf = os.path.abspath(cf) with open(cf, "w") as f: f.write("{}") assert connect.find_connection_file( abs_cf, path=jupyter_runtime_dir()) == abs_cf os.remove(abs_cf)
def run(self): cf = find_connection_file() client = BlockingKernelClient(connection_file=cf) client.load_connection_file() client.start_channels(shell=False, iopub=True, stdin=False, control=False, hb=False) while True: msg = client.get_iopub_msg() self.received.emit(msg)
def test_find_connection_file_local(): with TemporaryWorkingDirectory() as d: cf = 'test.json' abs_cf = os.path.abspath(cf) with open(cf, 'w') as f: f.write('{}') for query in ( 'test.json', 'test', abs_cf, os.path.join('.', 'test.json'), ): assert connect.find_connection_file(query, path=['.', jupyter_runtime_dir()]) == abs_cf
def test_find_connection_file_relative(): with TemporaryWorkingDirectory() as d: cf = 'test.json' os.mkdir('subdir') cf = os.path.join('subdir', 'test.json') abs_cf = os.path.abspath(cf) with open(cf, 'w') as f: f.write('{}') for query in ( os.path.join('.', 'subdir', 'test.json'), os.path.join('subdir', 'test.json'), abs_cf, ): assert connect.find_connection_file(query, path=['.', jupyter_runtime_dir()]) == abs_cf
def test_find_connection_file_local(): with TemporaryWorkingDirectory(): cf = "test.json" abs_cf = os.path.abspath(cf) with open(cf, "w") as f: f.write("{}") for query in ( "test.json", "test", abs_cf, os.path.join(".", "test.json"), ): assert connect.find_connection_file( query, path=[".", jupyter_runtime_dir()]) == abs_cf
def test_find_connection_file_relative(): with TemporaryWorkingDirectory(): cf = "test.json" os.mkdir("subdir") cf = os.path.join("subdir", "test.json") abs_cf = os.path.abspath(cf) with open(cf, "w") as f: f.write("{}") for query in ( os.path.join(".", "subdir", "test.json"), os.path.join("subdir", "test.json"), abs_cf, ): assert connect.find_connection_file( query, path=[".", jupyter_runtime_dir()]) == abs_cf
def test_find_connection_file(): with TemporaryDirectory() as d: cf = 'kernel.json' app = DummyConsoleApp(runtime_dir=d, connection_file=cf) app.initialize() security_dir = app.runtime_dir profile_cf = os.path.join(security_dir, cf) with open(profile_cf, 'w') as f: f.write("{}") for query in ( 'kernel.json', 'kern*', '*ernel*', 'k*', ): assert connect.find_connection_file(query, path=security_dir) == profile_cf
def test_find_connection_file(): cfg = Config() with TemporaryDirectory() as d: cfg.ProfileDir.location = d cf = 'kernel.json' app = DummyConsoleApp(config=cfg, connection_file=cf) app.initialize(argv=[]) BaseIPythonApplication._instance = app profile_cf = os.path.join(app.profile_dir.location, 'security', cf) with open(profile_cf, 'w') as f: f.write("{}") for query in ( 'kernel.json', 'kern*', '*ernel*', 'k*', ): nt.assert_equal(connect.find_connection_file(query), profile_cf) BaseIPythonApplication._instance = None
def run(self): cf = find_connection_file() client = BlockingKernelClient(connection_file=cf) client.load_connection_file() client.start_channels(shell=False, iopub=True, stdin=False, control=True, hb=False) while True: try: msg = client.get_iopub_msg(TIMEOUT) self.pub_q.put(msg) except Empty: pass if self.cmd_q.qsize(): cmd = self.cmd_q.get() if cmd is None: print('Client thread closing') break client.execute(cmd) self.ctrl_q.put(client.get_shell_msg())
def test_find_connection_file(): cfg = Config() with TemporaryDirectory() as d: cfg.ProfileDir.location = d cf = 'kernel.json' app = DummyConsoleApp(config=cfg, connection_file=cf) app.initialize() security_dir = app.runtime_dir profile_cf = os.path.join(security_dir, cf) with open(profile_cf, 'w') as f: f.write("{}") for query in ( 'kernel.json', 'kern*', '*ernel*', 'k*', ): nt.assert_equal(connect.find_connection_file(query, path=security_dir), profile_cf) JupyterApp._instance = None
def _create_client_for_kernel(self, connection_file, hostname, sshkey, password): ipycon = self.ipyconsole # Verifying if the connection file exists try: cf_path = osp.dirname(connection_file) cf_filename = osp.basename(connection_file) # To change a possible empty string to None cf_path = cf_path if cf_path else None connection_file = find_connection_file(filename=cf_filename, path=cf_path) except (IOError, UnboundLocalError): QMessageBox.critical( self, _('IPython'), _("Unable to connect to " "<b>%s</b>") % connection_file) return # Getting the master id that corresponds to the client # (i.e. the i in i/A) master_id = None given_name = None external_kernel = False slave_ord = ord('A') - 1 kernel_manager = None for cl in ipycon.get_clients(): if connection_file in cl.connection_file: if cl.get_kernel() is not None: kernel_manager = cl.get_kernel() connection_file = cl.connection_file if master_id is None: master_id = cl.id_['int_id'] given_name = cl.given_name new_slave_ord = ord(cl.id_['str_id']) if new_slave_ord > slave_ord: slave_ord = new_slave_ord # If we couldn't find a client with the same connection file, # it means this is a new master client if master_id is None: ipycon.master_clients += 1 master_id = to_text_string(ipycon.master_clients) external_kernel = True # Set full client name client_id = dict(int_id=master_id, str_id=chr(slave_ord + 1)) # Creating the client show_elapsed_time = ipycon.get_option('show_elapsed_time') reset_warning = ipycon.get_option('show_reset_namespace_warning') ask_before_restart = ipycon.get_option('ask_before_restart') client = MxClientWidget( ipycon, id_=client_id, given_name=given_name, history_filename=get_conf_path('history.py'), config_options=ipycon.config_options(), additional_options=ipycon.additional_options(), interpreter_versions=ipycon.interpreter_versions(), connection_file=connection_file, menu_actions=self.menu_actions, hostname=hostname, external_kernel=external_kernel, slave=True, show_elapsed_time=show_elapsed_time, reset_warning=reset_warning, ask_before_restart=ask_before_restart, css_path=ipycon.css_path) # Change stderr_dir if requested if ipycon.test_dir is not None: client.stderr_dir = ipycon.test_dir # Create kernel client kernel_client = QtKernelClient(connection_file=connection_file) # This is needed for issue spyder-ide/spyder#9304. try: kernel_client.load_connection_file() except Exception as e: QMessageBox.critical( self, _('Connection error'), _("An error occurred while trying to load " "the kernel connection file. The error " "was:\n\n") + to_text_string(e)) return if hostname is not None: try: connection_info = dict(ip=kernel_client.ip, shell_port=kernel_client.shell_port, iopub_port=kernel_client.iopub_port, stdin_port=kernel_client.stdin_port, hb_port=kernel_client.hb_port) newports = ipycon.tunnel_to_kernel(connection_info, hostname, sshkey, password) (kernel_client.shell_port, kernel_client.iopub_port, kernel_client.stdin_port, kernel_client.hb_port) = newports # Save parameters to connect comm later kernel_client.ssh_parameters = (hostname, sshkey, password) except Exception as e: QMessageBox.critical( self, _('Connection error'), _("Could not open ssh tunnel. The " "error was:\n\n") + to_text_string(e)) return # Assign kernel manager and client to shellwidget kernel_client.start_channels() shellwidget = client.shellwidget shellwidget.set_kernel_client_and_manager(kernel_client, kernel_manager) shellwidget.sig_exception_occurred.connect( self.main.console.sig_exception_occurred) if external_kernel: shellwidget.sig_is_spykernel.connect(self.connect_external_kernel) shellwidget.check_spyder_kernel() # Set elapsed time, if possible if not external_kernel: ipycon.set_elapsed_time(client) # Adding a new tab for the client ipycon.add_tab(client, name=client.get_name()) # Register client ipycon.register_client(client)
import pathlib from jupyter_client.manager import KernelManager from jupyter_client.connect import find_connection_file # forkする? m = KernelManager() m.connection_file = "kernel-me.json" m.write_connection_file() info = m.get_connection_info() print(info) print(m.connection_file) print(find_connection_file()) print(find_connection_file(pathlib.Path(m.connection_file).name)) # m.load_connection_file() m.blocking_client()
def _create_client_for_kernel(self, connection_file, hostname, sshkey, password): ipycon = self.ipyconsole # Verifying if the connection file exists try: cf_path = osp.dirname(connection_file) cf_filename = osp.basename(connection_file) # To change a possible empty string to None cf_path = cf_path if cf_path else None connection_file = find_connection_file(filename=cf_filename, path=cf_path) if osp.splitext(connection_file)[1] != ".json": # There might be a file with the same id in the path. connection_file = find_connection_file( filename=cf_filename + ".json", path=cf_path) except (IOError, UnboundLocalError): QMessageBox.critical(self, _('IPython'), _("Unable to connect to " "<b>%s</b>") % connection_file) return # Getting the master id that corresponds to the client # (i.e. the i in i/A) master_id = None given_name = None is_external_kernel = True known_spyder_kernel = False slave_ord = ord('A') - 1 kernel_manager = None for cl in ipycon.clients: if connection_file in cl.connection_file: if cl.get_kernel() is not None: kernel_manager = cl.get_kernel() connection_file = cl.connection_file if master_id is None: master_id = cl.id_['int_id'] is_external_kernel = cl.shellwidget.is_external_kernel known_spyder_kernel = cl.shellwidget.is_spyder_kernel given_name = cl.given_name new_slave_ord = ord(cl.id_['str_id']) if new_slave_ord > slave_ord: slave_ord = new_slave_ord # If we couldn't find a client with the same connection file, # it means this is a new master client if master_id is None: ipycon.master_clients += 1 master_id = str(ipycon.master_clients) # Set full client name client_id = dict(int_id=master_id, str_id=chr(slave_ord + 1)) # Creating the client show_elapsed_time = ipycon.get_conf('show_elapsed_time') reset_warning = ipycon.get_conf('show_reset_namespace_warning') ask_before_restart = ipycon.get_conf('ask_before_restart') client_args = { 'ask_before_closing': ipycon.get_conf('ask_before_closing'), 'std_dir': ipycon._test_dir if ipycon._test_dir else None, 'is_external_kernel': is_external_kernel, 'is_spyder_kernel': known_spyder_kernel, 'handlers': ipycon.registered_spyder_kernel_handlers, 'configuration': ipycon.CONFIGURATION } client = MxClientWidget(ipycon, id_=client_id, given_name=given_name, history_filename=get_conf_path('history.py'), config_options=ipycon.config_options(), additional_options=ipycon.additional_options(), interpreter_versions=ipycon.interpreter_versions(), connection_file=connection_file, # menu_actions=menu_actions, hostname=hostname, # slave=True, show_elapsed_time=show_elapsed_time, reset_warning=reset_warning, ask_before_restart=ask_before_restart, css_path=ipycon.css_path, **client_args) # Create kernel client kernel_client = QtKernelClient(connection_file=connection_file) # This is needed for issue spyder-ide/spyder#9304. try: kernel_client.load_connection_file() except Exception as e: QMessageBox.critical(self, _('Connection error'), _("An error occurred while trying to load " "the kernel connection file. The error " "was:\n\n") + str(e)) return if hostname is not None: try: connection_info = dict(ip = kernel_client.ip, shell_port = kernel_client.shell_port, iopub_port = kernel_client.iopub_port, stdin_port = kernel_client.stdin_port, hb_port = kernel_client.hb_port) newports = ipycon.tunnel_to_kernel(connection_info, hostname, sshkey, password) (kernel_client.shell_port, kernel_client.iopub_port, kernel_client.stdin_port, kernel_client.hb_port) = newports # Save parameters to connect comm later kernel_client.ssh_parameters = (hostname, sshkey, password) except Exception as e: QMessageBox.critical(self, _('Connection error'), _("Could not open ssh tunnel. The " "error was:\n\n") + str(e)) return # Assign kernel manager and client to shellwidget kernel_client.start_channels() shellwidget = client.shellwidget shellwidget.set_kernel_client_and_manager( kernel_client, kernel_manager) shellwidget.sig_exception_occurred.connect( ipycon.sig_exception_occurred) if not known_spyder_kernel: shellwidget.sig_is_spykernel.connect( self.connect_external_spyder_kernel) shellwidget.check_spyder_kernel() ipycon.sig_shellwidget_created.emit(shellwidget) # Modified from IPython code to remove modelx widgets # kernel_client.stopped_channels.connect( # lambda: ipycon.sig_shellwidget_deleted.emit(shellwidget)) kernel_client.stopped_channels.connect( lambda c=client: self.process_finished(c) ) # Set elapsed time, if possible if not is_external_kernel: ipycon.set_client_elapsed_time(client) # Adding a new tab for the client ipycon.add_tab(client, name=client.get_name()) # Register client ipycon.register_client(client)
# sub.setsockopt(zmq.SUBSCRIBE, b'engine.1.stderr') # sub.setsockopt(zmq.SUBSCRIBE, b'engine.2.stdout') sub.connect(iopub_url) while True: try: idents,msg = session.recv(sub, mode=0) except KeyboardInterrupt: return # ident always length 1 here topic = idents[0] if msg['msg_type'] == 'stream': # stdout/stderr # stream names are in msg['content']['name'], if you want to handle # them differently print("%s: %s" % (topic, msg['content']['text'])) elif msg['msg_type'] == 'pyerr': # Python traceback c = msg['content'] print(topic + ':') for line in c['traceback']: # indent lines print(' ' + line) if __name__ == '__main__': if len(sys.argv) > 1: cf = sys.argv[1] else: # This gets the security file for the default profile: cf = find_connection_file('ipcontroller-client.json') main(cf)
def km_from_string(s=''): """create kernel manager from IPKernelApp string such as '--shell=47378 --iopub=39859 --stdin=36778 --hb=52668' for IPython 0.11 or just 'kernel-12345.json' for IPython 0.12 """ try: import IPython except ImportError: raise ImportError("Could not find IPython. " + _install_instructions) try: from traitlets.config.loader import KeyValueConfigLoader except ImportError: # IPython <= 3.0 from IPython.config.loader import KeyValueConfigLoader try: from jupyter_client.manager import KernelManager from jupyter_client.connect import find_connection_file except ImportError: # IPython <= 3.0 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 try: from IPython.lib.kernel import find_connection_file except ImportError: # < 0.12, no find_connection_file pass global km, kc, send s = s.replace('--existing', '') if 'connection_file' in KernelManager.class_trait_names(): # 0.12 uses files instead of a collection of ports # include default IPython search path # filefind also allows for absolute paths, in which case the search # is ignored try: # XXX: the following approach will be brittle, depending on what # connection strings will end up looking like in the future, and # whether or not they are allowed to have spaces. I'll have to sync # up with the IPython team to address these issues -pi if '--profile' in s: k,p = s.split('--profile') k = k.lstrip().rstrip() # kernel part of the string p = p.lstrip().rstrip() # profile part of the string fullpath = find_connection_file(k,p) else: 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") return km = KernelManager(connection_file = fullpath) km.load_connection_file() else: if s == '': echo(":IPython 0.11 requires the full connection string") return loader = KeyValueConfigLoader(s.split(), aliases=kernel_aliases) cfg = loader.load_config()['KernelApp'] try: km = KernelManager( shell_address=(ip, cfg['shell_port']), sub_address=(ip, cfg['iopub_port']), stdin_address=(ip, cfg['stdin_port']), hb_address=(ip, cfg['hb_port'])) except KeyError as e: echo(":IPython " +s + " failed", "Info") echo("^-- failed --"+e.message.replace('_port','')+" not specified", "Error") return try: kc = km.client() except AttributeError: # 0.13 kc = km kc.start_channels() try: send = kc.execute except AttributeError: # < 3.0 send = kc.shell_channel.execute #XXX: backwards compatibility for IPython < 0.13 try: import inspect sc = kc.shell_channel num_oinfo_args = len(inspect.getargspec(sc.object_info).args) if num_oinfo_args == 2: # patch the object_info method which used to only take one argument klass = sc.__class__ klass._oinfo_orig = klass.object_info klass.object_info = lambda s,x,y: s._oinfo_orig(x) except: pass #XXX: backwards compatibility for IPython < 1.0 if not hasattr(kc, 'iopub_channel'): kc.iopub_channel = kc.sub_channel # now that we're connect to an ipython kernel, activate completion # machinery, but do so only for the local buffer if the user added the # following line the vimrc: # let g:ipy_completefunc = 'local' vim.command(""" if g:ipy_completefunc == 'global' set completefunc=CompleteIPython elseif g:ipy_completefunc == 'local' setl completefunc=CompleteIPython endif """) # also activate GUI doc balloons if in gvim vim.command(""" if has('balloon_eval') set bexpr=IPythonBalloonExpr() endif """) set_pid() return km
# sub.setsockopt(zmq.SUBSCRIBE, b'engine.2.stdout') sub.connect(iopub_url) while True: try: idents, msg = session.recv(sub, mode=0) except KeyboardInterrupt: return # ident always length 1 here topic = idents[0] if msg['msg_type'] == 'stream': # stdout/stderr # stream names are in msg['content']['name'], if you want to handle # them differently print("%s: %s" % (topic, msg['content']['text'])) elif msg['msg_type'] == 'pyerr': # Python traceback c = msg['content'] print(topic + ':') for line in c['traceback']: # indent lines print(' ' + line) if __name__ == '__main__': if len(sys.argv) > 1: cf = sys.argv[1] else: # This gets the security file for the default profile: cf = find_connection_file('ipcontroller-client.json') main(cf)
import logging import time from queue import Empty # Py 3 from jupyter_client import connect from jupyter_client import BlockingKernelClient logging.basicConfig(level=logging.DEBUG) f = connect.find_connection_file() print("loaded") client = BlockingKernelClient() client.load_connection_file(f) print("prepared") # msg_id = client.execute("print('hello')") msg_id = client.execute("1 + 10") # client.wait_for_ready() res = client.get_shell_msg(msg_id, timeout=1) print(res) print("----------------------------------------") msg = res["msg_id"] for i in range(10): if not client.is_alive(): print("not alived") break
import logging import time from queue import Empty # Py 3 from jupyter_client import connect from jupyter_client import BlockingKernelClient logging.basicConfig(level=logging.DEBUG) f = connect.find_connection_file() print("loaded") client = BlockingKernelClient() client.load_connection_file(f) print("prepared") # msg_id = client.execute("print('hello')") msg_id = client.execute("1 + 10") # client.wait_for_ready() res = client.get_shell_msg(msg_id, timeout=1) print(res) print("----------------------------------------") msg = res["msg_id"] for i in range(10):
'OKBLUE': '\033[94m', 'OKCYAN': '\033[96m', 'OKGREEN': '\033[92m', 'WARNING': '\033[93m', 'FAIL': '\033[91m', 'ENDC': '\033[0m', 'BOLD': '\033[1m', 'UNDERLINE': '\033[4m', } print(lookup[color], end='') print(*args, end='') print(lookup['ENDC']) # setup by automatically finding a running kernel cf = find_connection_file() client = BlockingKernelClient(connection_file=cf) client.load_connection_file() client.start_channels() # simplest usage - execute statments and check if OK msgid = client.execute('a = 2') ret = client.get_shell_msg() status = ret['content']['status'] if status == 'ok': print('statement executed ok') elif status == 'error': ename = ret['content']['ename'] print('there was a %s exception, which will also appear on the ' 'iopub channel' % ename)