def mock_kernel(): """A pytest fixture that creates a jupyter kernel""" km, kc = start_new_kernel(kernel_name='python3') yield kc, km km.shutdown_kernel(now=True)
def preprocess(self, nb, resources): path = resources.get('metadata', {}).get('path', '') if path == '': path = None from jupyter_client.manager import start_new_kernel if not self.kernel_name: kernel_name = nb.metadata.get('kernelspec', {}).get('name', 'python') else: kernel_name = self.kernel_name self.log.info("Executing notebook with kernel: %s" % kernel_name) self.km, self.kc = start_new_kernel( kernel_name=kernel_name, extra_arguments=self.extra_arguments, stderr=open(os.devnull, 'w'), cwd=path) self.kc.allow_stdin = False try: nb, resources = super(ExecutePreprocessor, self).preprocess(nb, resources) finally: self.kc.stop_channels() self.km.shutdown_kernel(now=True) return nb, resources
def start_new_kernel(**kwargs): """start a new kernel, and return its Manager and Client Integrates with our output capturing for tests. """ kwargs.update(dict(stdout=nose.iptest_stdstreams_fileno(), stderr=STDOUT)) return manager.start_new_kernel(startup_timeout=STARTUP_TIMEOUT, **kwargs)
def check_single_notebook(notebook_filename, timeout=500): """ Checks single notebook being given its full name. (executes cells one-by-one checking there are no exceptions, nothing more is guaranteed) """ with open(notebook_filename) as notebook_file: notebook_content = nbformat.reads(notebook_file.read(), as_version=nbformat.current_nbformat) os.chdir(os.path.dirname(notebook_filename)) _, client = manager.start_new_kernel() for cell in notebook_content.cells: if cell.cell_type == 'code': message_id = client.execute(cell.source) try: message = client.get_shell_msg(message_id, timeout=timeout) except Empty: raise RuntimeError("Cell timed out: \n {}".format( cell.source)) if message['content']['status'] != 'ok': traceback = message['content']['traceback'] description = "Cell failed: '{}'\n\n Traceback:\n{}".format( cell.source, '\n'.join(traceback)) raise RuntimeError(description) client.stop_channels()
def __init__(self, use_exist=False): """ ## Description Initializes the `kernel_manager` and `client` objects and starts the kernel. Also initializes the pretty printer for displaying object properties and execution result payloads. ## Parameters None. """ if not use_exist: # Initialize kernel and client self.kernel_manager, self.client = start_new_kernel() self.send = self.client.execute else: self.kernel_manager = KernelManager( connection_file=find_connection_file()) self.kernel_manager.load_connection_file(find_connection_file()) self.client = self.kernel_manager.client() self.client.start_channels() self.send = self.client.execute # Initialize pretty printer self.pp = PrettyPrinter(indent=2)
def start_new_kernel(**kwargs): """start a new kernel, and return its Manager and Client Integrates with our output capturing for tests. """ stdout = getattr(nose, 'iptest_stdstreams_fileno', open(os.devnull)) kwargs.update(dict(stdout=stdout, stderr=STDOUT)) return manager.start_new_kernel(startup_timeout=STARTUP_TIMEOUT, **kwargs)
def kernel(wd=None, verbose=0): # setup ipython kernel and configure it kernel_manager, kernel_client = manager.start_new_kernel(extra_arguments=["--matplotlib='inline'"]) # apply patches dirname = os.path.dirname(os.path.abspath(__file__)) python_patch_file = os.path.join(dirname, "langs", "python-patch.py") kernel_client.execute("%run " + python_patch_file) # set working directory if wd: kernel_client.execute("cd %s" % wd) input_queue = Queue.Queue() input_thread = threading.Thread(target=add_input, args=(input_queue,)) input_thread.daemon = True input_thread.start() # we're up and running! sys.stdout.write(json.dumps({ "id": "startup-complete", "status": "complete" }) + "\n") while True: if not input_queue.empty(): line = input_queue.get().strip() payload = json.loads(line) uid = payload["id"] args = payload.get("args", []) kwargs = payload.get("kwargs", {}) method = payload.get("method", False) targetStr = payload.get("target", "client") if targetStr == "manager": target = kernel_manager else: target = kernel_client if method: result = getattr(target, method)(*args, **kwargs) sys.stdout.write(json.dumps({ "result": result, "id": uid }) + '\n') try: data = kernel_client.get_iopub_msg(timeout=0.1) sys.stdout.write(json.dumps({"source": "iopub", "result": data}, default=json_serial) + '\n') except Empty: pass try: data = kernel_client.get_shell_msg(timeout=0.1) sys.stdout.write(json.dumps({"source": "shell", "result": data}, default=json_serial) + '\n') except Empty: pass try: data = kernel_client.get_stdin_msg(timeout=0.1) sys.stdout.write(json.dumps({"source": "stdin", "result": data}, default=json_serial) + '\n') except Empty: pass
def start_kernel(self): """ Start IPython kernel """ self.km, self.kc = start_new_kernel( #kernel_name=kernel_name, #extra_arguments=self.extra_arguments, stderr=open(os.devnull, 'w'), cwd=os.getcwd()) self.kc.allow_stdin = False
def get_kernel_client(cluster_id, spark, log): kernel_name, _ = get_kernel_path(cluster_id, spark) if TestRunKernel.CLIENTS.get(kernel_name) is None: km, kc = start_new_kernel( kernel_name=kernel_name, stderr=open("{}{}_std_err.log".format(TEMP, kernel_name), "w"), stdout=open("{}{}_std_out.log".format(TEMP, kernel_name), "w"), ) TestRunKernel.CLIENTS[kernel_name] = (km, kc) return TestRunKernel.CLIENTS[kernel_name]
def start_new_kernel(**kwargs): """start a new kernel, and return its Manager and Client Integrates with our output capturing for tests. """ try: stdout = nose.iptest_stdstreams_fileno() except AttributeError: stdout = open(os.devnull) kwargs.update(dict(stdout=stdout, stderr=STDOUT)) return manager.start_new_kernel(startup_timeout=STARTUP_TIMEOUT, **kwargs)
def start_new_kernel(**kwargs): """start a new kernel, and return its Manager and Client Integrates with our output capturing for tests. """ kwargs['stderr'] = STDOUT try: kwargs['stdout'] = nose.iptest_stdstreams_fileno() except AttributeError: pass return manager.start_new_kernel(startup_timeout=STARTUP_TIMEOUT, **kwargs)
def make_kernel(language): if language in kernels: return kernels[language] #print("starting new kernel for {language}".format(**locals())) from jupyter_client.manager import start_new_kernel name = language_to_kernel(language) manager, client = start_new_kernel(kernel_name=name) kernels[language] = manager, client import atexit atexit.register(lambda: manager.shutdown_kernel(now=True)) return manager, client
def __init__(self, parsed, kernel, source, mode, figdir, outdir): super(JupyterProcessor, self).__init__(parsed, kernel, source, mode, figdir, outdir) self.extra_arguments = None self.timeout = -1 path = os.path.abspath(outdir) self.km, self.kc = start_new_kernel( kernel_name=kernel, extra_arguments=self.extra_arguments, stderr=open(os.devnull, 'w'), cwd=path) self.kc.allow_stdin = False
def kernel_factory(kernel_name: str) -> KernelPair: """ Start a new kernel. Parameters ---------- kernel_name : str Returns ------- KernalPair: namedtuple - km (KernelManager) - kc (KernelClient) """ return KernelPair(*start_new_kernel(kernel_name=kernel_name))
def test_process_killed(self): km, kc = start_new_kernel(kernel_name='swift') kc.execute(""" import Glibc exit(0) """) messages = self.wait_for_idle(kc) had_error = False for message in messages: if message['header']['msg_type'] == 'error': had_error = True self.assertEqual(['Process killed'], message['content']['traceback']) self.assertTrue(had_error)
def __init__(self): """ ## Description Initializes the `kernel_manager` and `client` objects and starts the kernel. Also initializes the pretty printer for displaying object properties and execution result payloads. ## Parameters None. """ ### Initialize kernel and client self.kernel_manager, self.client = start_new_kernel() ### Initialize pretty printer self.pp = PrettyPrinter(indent=2)
def execute_nb(nb): kernel_name = nb.metadata.get('kernelspec', {}).get('name', 'python') km, kc = start_new_kernel( kernel_name=kernel_name, stderr=open(os.devnull, 'w'), cwd=None) kc.allow_stdin = False try: ep = ExecutePreprocessor(timeout=-1, kernel_name=kernel_name) nb, resources = ep.preprocess(nb, {'metadata': {'path': './'}}) finally: kc.stop_channels() km.shutdown_kernel(now=True) return nb
def __init__(self, notebook_path): self.path = notebook_path with open(notebook_path, 'r') as notebook_file: self.json = json.load(notebook_file) self.kernel_name = self.json['metadata']['kernelspec']['name'] self.cells = self.json['cells'] # Might raise kernelspec.NoSuchKernel m, c = kernel_manager.start_new_kernel(kernel_name=self.kernel_name) self.manager = m self.client = c self.run_before = {}
def __init__(self, kernel_name, timeout=DEFAULT_TIMEOUT, **kwargs): r""" Initialize Jupyter kernel object Parameters ---------- kernel_name : str Name of kernel. For R, this is likely to be "ir" (see https://irkernel.github.io/docs/IRkernel). timeout : float, optional Default timeout in seconds. \*\*kwargs : dict Arguments to pass to `start_kernel`. `cwd='some/path'` is one example. """ self.manager, self.client = start_new_kernel(kernel_name=kernel_name, **kwargs) self.timeout = timeout
def kernel(wd=None, verbose=0): '''Setup ipython kernel and configure it''' apply_patches() set_working_directory(wd) input_queue = start_main_thread() kernel_manager, kernel_client = manager.start_new_kernel(extra_arguments=["--matplotlib='inline'"]) while True: if not input_queue.empty(): request_input_line = input_queue.get().strip() request_payload = unwrap_payload(request_input_line) process_request(request_payload, kernel_manager, kernel_client) write_response(kernel_client)
def preprocess(self, nb, resources): """ Preprocess notebook executing each code cell. The input argument `nb` is modified in-place. Parameters ---------- nb : NotebookNode Notebook being executed. resources : dictionary Additional resources used in the conversion process. For example, passing ``{'metadata': {'path': run_path}}`` sets the execution path to ``run_path``. Returns ------- nb : NotebookNode The executed notebook. resources : dictionary Additional resources used in the conversion process. """ path = resources.get('metadata', {}).get('path', '') if path == '': path = None from jupyter_client.manager import start_new_kernel kernel_name = nb.metadata.get('kernelspec', {}).get('name', 'python') if self.kernel_name: kernel_name = self.kernel_name self.log.info("Executing notebook with kernel: %s" % kernel_name) self.km, self.kc = start_new_kernel( kernel_name=kernel_name, extra_arguments=self.extra_arguments, stderr=open(os.devnull, 'w'), cwd=path) self.kc.allow_stdin = False try: nb, resources = super(ExecutePreprocessor, self).preprocess(nb, resources) finally: self.kc.stop_channels() self.km.shutdown_kernel(now=True) return nb, resources
def test_process_killed(self): km, kc = start_new_kernel(kernel_name='swift') kc.execute(""" import Glibc exit(0) """) had_error = False while True: reply = kc.get_iopub_msg(timeout=10) if reply['header']['msg_type'] == 'error': had_error = True self.assertEqual(['Process killed'], reply['content']['traceback']) if reply['header']['msg_type'] == 'status' and \ reply['content']['execution_state'] == 'idle': break self.assertTrue(had_error)
def __init__(self): """ ## Description Initializes the `kernel_manager` and `client` objects and starts the kernel. Also initializes the pretty printer for displaying object properties and execution result payloads. ## Parameters None. """ self.kernel_manager, self.client = start_new_kernel() ### Initialize the channel. Otherwise first execution request is ### not run. self.client.start_channels() #self.client.wait_for_ready() ### Initialize pretty printer self.pp = PrettyPrint(indent=2)
def test_install_after_execute(self): # The kernel is supposed to refuse to install package after executing # code. km, kc = start_new_kernel(kernel_name='swift') kc.execute('1 + 1') self.wait_for_idle(kc) kc.execute(""" %install DummyPackage DummyPackage """) messages = self.wait_for_idle(kc) had_error = False for message in messages: if message['header']['msg_type'] == 'error': had_error = True self.assertIn('Install Error: Packages can only be installed ' 'during the first cell execution.', message['content']['traceback'][0]) self.assertTrue(had_error)
def test_install_after_execute_blank(self): # If the user executes blank code, the kernel is supposed to try # to install packages. In particular, Colab sends a blank execution # request to the kernel when it starts up, and it's important that this # doesn't block package installation. km, kc = start_new_kernel(kernel_name='swift') kc.execute('\n\n\n') self.wait_for_idle(kc) kc.execute(""" %install DummyPackage DummyPackage """) messages = self.wait_for_idle(kc) # DummyPackage doesn't exist, so package installation won't actually # succeed. So we just assert that the kernel tries to install it. stdout = '' for message in messages: if message['header']['msg_type'] == 'stream' and \ message['content']['name'] == 'stdout': stdout += message['content']['text'] self.assertIn('Installing packages:', stdout)
def check_single_notebook(notebook_filename, timeout=500): """ Checks single notebook being given its full name. (executes cells one-by-one checking there are no exceptions, nothing more is guaranteed) """ with open(notebook_filename) as notebook_file: notebook_content = nbformat.reads(notebook_file.read(), as_version=nbformat.current_nbformat) os.chdir(os.path.dirname(notebook_filename)) _, client = manager.start_new_kernel() for cell in notebook_content.cells: if cell.cell_type == 'code': message_id = client.execute(cell.source) try: message = client.get_shell_msg(message_id, timeout=timeout) except Empty: raise RuntimeError("Cell timed out: \n {}".format(cell.source)) if message['content']['status'] != 'ok': traceback = message['content']['traceback'] description = "Cell failed: '{}'\n\n Traceback:\n{}".format(cell.source, '\n'.join(traceback)) raise RuntimeError(description) client.stop_channels()
def kernel(wd=None, verbose=0): # setup ipython kernel and configure it kernel_mgr, kernel_client = manager.start_new_kernel(extra_arguments=["--matplotlib='inline'"]) # apply patches dirname = os.path.dirname(os.path.abspath(__file__)) python_patch_file = os.path.join(dirname, "langs", "python-patch.py") kernel_client.execute("%run " + python_patch_file) # set working directory if wd: kernel_client.execute("cd %s" % wd) input_queue = Queue.Queue() input_thread = threading.Thread(target=add_input, args=(input_queue,)) input_thread.daemon = True input_thread.start() outputs = {} docstring_callbacks = {} os.write(3, '{"status" : "OK"}' + "\n") while True: if not input_queue.empty(): line = input_queue.get().strip() payload = json.loads(line) execution_id = payload['id'] code = payload['code'] complete = payload.get('complete', False) if verbose > 0: sys.stderr.write(line + '\n') if complete==True: msg_id = kernel_client.complete(code) elif complete=='input': msg_id = kernel_client.stdin_channel.execute(code) else: msg_id = kernel_client.execute(code, allow_stdin=False) if code=="interrupt_kernel": sys.stderr.write("interrupting kernel\n") sys.stderr.flush() kernel_mgr.interrupt_kernel() reply = { "id": execution_id, "msg_id": msg_id, "output": "", "status": "complete", "stream": None, "image": None, "error": None } sys.stdout.write(json.dumps(reply) + '\n') sys.stdout.flush() continue outputs[msg_id] = { "id": execution_id, "msg_id": msg_id, "output": "", "stream": None, "image": None, "error": None } sys.stdout.write(json.dumps({ "msg_id": msg_id, "id": execution_id, "code": code }) + '\n') sys.stdout.flush() data = None try: data = kernel_client.get_iopub_msg(timeout=0.1) except Empty: try: data = kernel_client.get_shell_msg(timeout=0.1) except: try: data = kernel_client.get_stdin_msg(timeout=0.1) except: continue parent_msg_id = data['parent_header']['msg_id'] if parent_msg_id not in outputs and parent_msg_id not in docstring_callbacks: continue if verbose > 0: pp.pprint(data, sys.stderr) # handle code execution results if parent_msg_id in docstring_callbacks: if data['header']['msg_type']=="stream": docstring = data['content']['text'] original_parent_msg_id = docstring_callbacks[parent_msg_id]['parent_msg_id'] outputs[original_parent_msg_id]['output2'][parent_msg_id]['docstring'] = docstring outputs[original_parent_msg_id]['n_finished_docstrings'] += 1 del docstring_callbacks[parent_msg_id] if outputs[original_parent_msg_id]['n_finished_docstrings']==len(outputs[original_parent_msg_id]['output2']): outputs[original_parent_msg_id]['output'] = outputs[original_parent_msg_id]['output2'].values() sys.stdout.write(json.dumps(outputs[original_parent_msg_id]) + '\n') sys.stdout.flush() del outputs[original_parent_msg_id] continue # if this is the last one that needs a docstring, then send back everything else: continue elif 'execution_state' in data['content']: if data['content']['execution_state']=='idle': if data['parent_header']['msg_type']=='execute_request': outputs[parent_msg_id]['status'] = 'complete' outputs[parent_msg_id]['stream'] = None outputs[parent_msg_id]['image'] = None outputs[parent_msg_id]['error'] = None sys.stdout.write(json.dumps(outputs[parent_msg_id]) + '\n') sys.stdout.flush() del outputs[parent_msg_id] continue elif data['header']['msg_type']=="execute_result": outputs[parent_msg_id]['output'] = data['content']['data'].get('text/plain', '') outputs[parent_msg_id]['stream'] = data['content']['data'].get('text/plain', '') elif data['header']['msg_type']=="display_data": if 'image/png' in data['content']['data']: outputs[parent_msg_id]['image'] = data['content']['data']['image/png'] elif 'text/html' in data['content']['data']: outputs[parent_msg_id]['html'] = data['content']['data']['text/html'] elif data['header']['msg_type']=="stream": outputs[parent_msg_id]['output'] += data['content'].get('text', '') outputs[parent_msg_id]['stream'] = data['content'].get('text', '') elif data['header']['msg_type']=="error": outputs[parent_msg_id]['error'] = "\n".join(data['content']['traceback']) elif data['header']['msg_type']=="input_request": outputs[parent_msg_id]['status'] = 'input' outputs[parent_msg_id]['stream'] = data['content'].get('prompt', '') sys.stdout.write(json.dumps(outputs[parent_msg_id]) + '\n') sys.stdout.flush() # TODO: figure out why this is here... outputs[parent_msg_id]['image'] = None outputs[parent_msg_id]['stream'] = None # handle autocomplete matches if 'matches' in data['content'] and data['msg_type']=='complete_reply' and data['parent_header']['msg_id']==msg_id: results = [] results2 = OrderedDict() for completion in data['content']['matches']: result = { 'value': completion, 'dtype': '---' } text = result['value'] dtype = '' dtype = None if '.' in code: dtype = 'function' # get docstring metadata for each suggestion msg_id = kernel_client.execute('print(%s.__doc__)' % completion) results2[msg_id] = { 'text': text, 'dtype': result['dtype'], 'docstring': None } docstring_callbacks[msg_id] = { 'parent_msg_id': parent_msg_id, 'docstring': None} results.append(result) func_args = "[%s]" % ", ".join([completion for completion in data['content']['matches']]) msg_id = kernel_client.execute('%s.__doc__)' % completion) msg_id = kernel_client.execute("__get_metadata(%s)" % func_args) docstring_callbacks[msg_id] = None outputs[parent_msg_id]['output'] = results outputs[parent_msg_id]['output2'] = results2 outputs[parent_msg_id]['n_finished_docstrings'] = 0 outputs[parent_msg_id]['status'] = 'complete'
def startKernel(kernel_name): return start_new_kernel(kernel_name=kernel_name)
def start_new_kernel(kernel='python', **kwargs): """start a new kernel, and return its Manager and Client """ return manager.start_new_kernel(startup_timeout=STARTUP_TIMEOUT, kernel_name=kernel, **kwargs)
def kernel(wd=None, verbose=0): # setup ipython kernel and configure it kernel_mgr, kernel_client = manager.start_new_kernel( extra_arguments=["--matplotlib='inline'"]) # apply patches dirname = os.path.dirname(os.path.abspath(__file__)) python_patch_file = os.path.join(dirname, "langs", "python-patch.py") kernel_client.execute("%run " + python_patch_file) # set working directory if wd: kernel_client.execute("cd %s" % wd) input_queue = Queue.Queue() input_thread = threading.Thread(target=add_input, args=(input_queue, )) input_thread.daemon = True input_thread.start() outputs = {} docstring_callbacks = {} # we're up and running! sys.stdout.write( json.dumps({ "id": "startup-complete", "status": "complete" }) + "\n") sys.stdout.flush() while True: if not input_queue.empty(): line = input_queue.get().strip() payload = json.loads(line) execution_id = payload['id'] code = payload['code'] complete = payload.get('complete', False) if verbose > 0: sys.stderr.write(line + '\n') if complete == True: msg_id = kernel_client.complete(code) elif complete == 'input': msg_id = kernel_client.stdin_channel.execute(code) else: msg_id = kernel_client.execute(code, allow_stdin=True) if code == "interrupt_kernel": sys.stderr.write("interrupting kernel\n") sys.stderr.flush() kernel_mgr.interrupt_kernel() reply = { "id": execution_id, "msg_id": msg_id, "output": "", "status": "complete", "stream": None, "image": None, "error": None } sys.stdout.write(json.dumps(reply) + '\n') sys.stdout.flush() continue outputs[msg_id] = { "id": execution_id, "msg_id": msg_id, "output": "", "stream": None, "image": None, "error": None } sys.stdout.write( json.dumps({ "msg_id": msg_id, "id": execution_id, "code": code }) + '\n') sys.stdout.flush() data = None try: data = kernel_client.get_iopub_msg(timeout=0.1) except Empty: try: data = kernel_client.get_shell_msg(timeout=0.1) except Empty: try: data = kernel_client.get_stdin_msg(timeout=0.1) except: continue parent_msg_id = data['parent_header']['msg_id'] if parent_msg_id not in outputs and parent_msg_id not in docstring_callbacks: continue if verbose > 0: pp.pprint(data, sys.stderr) sys.stderr.flush() # handle code execution results if parent_msg_id in docstring_callbacks: original_parent_msg_id = docstring_callbacks[parent_msg_id] if data['header']['msg_type'] == "stream": docstring = data['content']['text'] outputs[original_parent_msg_id]['output'] += docstring continue elif 'execution_state' in data['content']: # if this is the last one that needs a docstring, then send back everything outputs[original_parent_msg_id]['status'] = 'complete' if data['content']['execution_state'] == 'idle': sys.stdout.write( json.dumps(outputs[original_parent_msg_id]) + '\n') sys.stdout.flush() del outputs[original_parent_msg_id] del docstring_callbacks[parent_msg_id] continue else: continue else: continue elif 'execution_state' in data['content']: if data['content']['execution_state'] == 'idle': if data['parent_header']['msg_type'] == 'execute_request': outputs[parent_msg_id]['status'] = 'complete' outputs[parent_msg_id]['stream'] = None outputs[parent_msg_id]['image'] = None outputs[parent_msg_id]['error'] = None sys.stdout.write(json.dumps(outputs[parent_msg_id]) + '\n') sys.stdout.flush() del outputs[parent_msg_id] continue elif data['header']['msg_type'] == "execute_result": outputs[parent_msg_id]['output'] = data['content']['data'].get( 'text/plain', '') outputs[parent_msg_id]['stream'] = data['content']['data'].get( 'text/plain', '') elif data['header']['msg_type'] == "display_data": if 'image/png' in data['content']['data']: outputs[parent_msg_id]['image'] = data['content']['data'][ 'image/png'] elif 'text/html' in data['content']['data']: outputs[parent_msg_id]['html'] = data['content']['data'][ 'text/html'] elif data['header']['msg_type'] == "stream": outputs[parent_msg_id]['output'] += data['content'].get('text', '') outputs[parent_msg_id]['stream'] = data['content'].get('text', '') elif data['header']['msg_type'] == "error": outputs[parent_msg_id]['error'] = "\n".join( data['content']['traceback']) elif data['header']['msg_type'] == "input_request": outputs[parent_msg_id]['status'] = 'input' outputs[parent_msg_id]['stream'] = data['content'].get( 'prompt', '') sys.stdout.write(json.dumps(outputs[parent_msg_id]) + '\n') sys.stdout.flush() # TODO: figure out why this is here... outputs[parent_msg_id]['image'] = None outputs[parent_msg_id]['stream'] = None # handle autocomplete matches if 'matches' in data['content'] and data[ 'msg_type'] == 'complete_reply' and data['parent_header'][ 'msg_id'] == msg_id: # we're going to get all the docstrings for our autocomplete options names = [] for completion in data['content']['matches']: names.append("'" + completion + "'") names = "[%s]" % ", ".join(names) cmd = '__get_docstrings(globals(), %s, %r)' % (names, "." in code) msg_id = kernel_client.execute(cmd) docstring_callbacks[msg_id] = parent_msg_id outputs[parent_msg_id]['output'] = ''
def setUpClass(cls): cls.km, cls.kc = start_new_kernel(kernel_name=cls.kernel_name)
def _init_kernel(self): km, kc = start_new_kernel(kernel_name='swift') self.km = km self.kc = kc
def __init__(self): self.kernel_manager, self.kernel_client = manager.start_new_kernel()
def kernel(wd=None, verbose=0): # setup ipython kernel and configure it kernel_manager, kernel_client = manager.start_new_kernel( extra_arguments=["--matplotlib='inline'"]) current_timeout_min = 0.0005 current_timeout_max = 0.01 current_timeout = current_timeout_max acceptable_types = [ "execute_input", "stream", "display_data", "error", "execute_result", "execute_reply", "complete_reply" ] # apply patches dirname = os.path.dirname(os.path.abspath(__file__)) python_patch_file = os.path.join(dirname, "langs", "python-patch.py") kernel_client.execute("%run " + python_patch_file, { "silent": True, "store_history": False }) # set working directory if wd: kernel_client.execute("cd %s" % wd) input_queue = Queue.Queue() input_thread = threading.Thread(target=add_input, args=(input_queue, )) input_thread.daemon = True input_thread.start() # we're up and running! sys.stdout.write( json.dumps({ "id": "startup-complete", "status": "complete" }) + "\n") should_continue = True while should_continue: if not input_queue.empty(): current_timeout = current_timeout_min line = input_queue.get().strip() payload = json.loads(line) uid = payload["id"] args = payload.get("args", []) kwargs = payload.get("kwargs", {}) method = payload.get("method", False) target_str = payload.get("target", "client") exec_eval = payload.get("exec_eval", False) if target_str == "manager": target = kernel_manager else: target = kernel_client if method: if getattr(target, method, False): result = getattr(target, method)(*args, **kwargs) if result: sys.stdout.write( json.dumps({ "source": "link", "result": result, "id": uid }) + '\n') else: sys.stdout.write( json.dumps({ "error": "Missing method " + method, "id": uid }) + '\n') if exec_eval: result = eval(exec_eval) sys.stdout.write( json.dumps({ "source": "eval", "result": result, "id": uid }) + '\n') try: data = kernel_client.get_shell_msg(timeout=current_timeout) content = data.get('content', False) if content: payload = content.get('payload', False) if payload: try: first = payload[0] if first: source = first.get('source', False) keepkernel = first.get('keepkernel', False) if source == 'ask_exit' and keepkernel == False: should_continue = False except IndexError: pass msg_type = data.get('msg_type', False) if msg_type == 'shutdown_reply': shutdown_restart = content.get('restart', False) if not shutdown_restart: should_continue = False sys.stdout.write( json.dumps( { "source": "shell", "result": data, "should_continue": should_continue }, default=json_serial) + '\n') current_timeout = current_timeout_min except Empty: pass try: while True: data = kernel_client.get_iopub_msg(timeout=current_timeout) sys.stdout.write( json.dumps({ "source": "iopub", "result": data }, default=json_serial) + '\n') sys.stdout.flush() current_timeout = current_timeout_min except Empty: pass try: data = kernel_client.get_stdin_msg(timeout=current_timeout) sys.stdout.write( json.dumps({ "source": "stdin", "result": data }, default=json_serial) + '\n') sys.stdout.flush() current_timeout = current_timeout_min except Empty: pass current_timeout = min(current_timeout * 1.1, current_timeout_max)
def kernel(wd=None, verbose=0): # setup ipython kernel and configure it kernel_manager, kernel_client = manager.start_new_kernel(extra_arguments=["--matplotlib='inline'"]) current_timeout_min = 0.0005 current_timeout_max = 0.01 current_timeout = current_timeout_max acceptable_types = [ "execute_input", "stream", "display_data", "error", "execute_result", "execute_reply", "complete_reply" ] # apply patches dirname = os.path.dirname(os.path.abspath(__file__)) python_patch_file = os.path.join(dirname, "langs", "python-patch.py") kernel_client.execute("%run " + python_patch_file, {"silent":True, "store_history":False}) # set working directory if wd: kernel_client.execute("cd %s" % wd) input_queue = Queue.Queue() input_thread = threading.Thread(target=add_input, args=(input_queue,)) input_thread.daemon = True input_thread.start() # we're up and running! sys.stdout.write(json.dumps({ "id": "startup-complete", "status": "complete" }) + "\n") while True: if not input_queue.empty(): current_timeout = current_timeout_min line = input_queue.get().strip() payload = json.loads(line) uid = payload["id"] args = payload.get("args", []) kwargs = payload.get("kwargs", {}) method = payload.get("method", False) target_str = payload.get("target", "client") exec_eval = payload.get("exec_eval", False) if target_str == "manager": target = kernel_manager else: target = kernel_client if method: if getattr(target, method, False): result = getattr(target, method)(*args, **kwargs) sys.stdout.write(json.dumps({"source": "link", "result": result, "id": uid }) + '\n') else: sys.stdout.write(json.dumps({ "error": "Missing method " + method, "id": uid }) + '\n') if exec_eval: result = eval(exec_eval) sys.stdout.write(json.dumps({ "source": "eval", "result": result, "id": uid }) + '\n') try: while True: data = kernel_client.get_iopub_msg(timeout=current_timeout) if data.get("msg_type") in acceptable_types: sys.stdout.write(json.dumps({"source": "iopub", "result": data}, default=json_serial) + '\n') sys.stdout.flush() current_timeout = current_timeout_min except Empty: pass try: data = kernel_client.get_shell_msg(timeout=current_timeout) sys.stdout.write(json.dumps({"source": "shell", "result": data}, default=json_serial) + '\n') current_timeout = current_timeout_min except Empty: pass try: data = kernel_client.get_stdin_msg(timeout=current_timeout) sys.stdout.write(json.dumps({"source": "stdin", "result": data}, default=json_serial) + '\n') current_timeout = current_timeout_min except Empty: pass current_timeout = min(current_timeout * 1.1, current_timeout_max)
def kernel(wd=None, verbose=0): # setup ipython kernel and configure it kernel_mgr, kernel_client = manager.start_new_kernel(extra_arguments=["--matplotlib='inline'"]) # apply patches dirname = os.path.dirname(os.path.abspath(__file__)) python_patch_file = os.path.join(dirname, "langs", "python-patch.py") kernel_client.execute("%run " + python_patch_file) # set working directory if wd: kernel_client.execute("cd %s" % wd) input_queue = Queue.Queue() input_thread = threading.Thread(target=add_input, args=(input_queue,)) input_thread.daemon = True input_thread.start() outputs = {} docstring_callbacks = {} # we're up and running! sys.stdout.write(json.dumps({ "id": "startup-complete", "status": "complete" }) + "\n") sys.stdout.flush() while True: if not input_queue.empty(): line = input_queue.get().strip() payload = json.loads(line) execution_id = payload['id'] code = payload['code'] complete = payload.get('complete', False) if verbose > 0: sys.stderr.write(line + '\n') if complete==True: msg_id = kernel_client.complete(code) elif complete=='input': msg_id = kernel_client.stdin_channel.execute(code) else: msg_id = kernel_client.execute(code, allow_stdin=False) if code=="interrupt_kernel": sys.stderr.write("interrupting kernel\n") sys.stderr.flush() kernel_mgr.interrupt_kernel() reply = { "id": execution_id, "msg_id": msg_id, "output": "", "status": "complete", "stream": None, "image": None, "error": None } sys.stdout.write(json.dumps(reply) + '\n') sys.stdout.flush() continue outputs[msg_id] = { "id": execution_id, "msg_id": msg_id, "output": "", "stream": None, "image": None, "error": None } sys.stdout.write(json.dumps({ "msg_id": msg_id, "id": execution_id, "code": code }) + '\n') sys.stdout.flush() data = None try: data = kernel_client.get_iopub_msg(timeout=0.1) except Empty: try: data = kernel_client.get_shell_msg(timeout=0.1) except: try: data = kernel_client.get_stdin_msg(timeout=0.1) except: continue parent_msg_id = data['parent_header']['msg_id'] if parent_msg_id not in outputs and parent_msg_id not in docstring_callbacks: continue if verbose > 0: pp.pprint(data, sys.stderr) # handle code execution results if parent_msg_id in docstring_callbacks: original_parent_msg_id = docstring_callbacks[parent_msg_id] if data['header']['msg_type']=="stream": docstring = data['content']['text'] outputs[original_parent_msg_id]['output'] += docstring continue elif 'execution_state' in data['content']: # if this is the last one that needs a docstring, then send back everything outputs[original_parent_msg_id]['status'] = 'complete' if data['content']['execution_state']=='idle': sys.stdout.write(json.dumps(outputs[original_parent_msg_id]) + '\n') sys.stdout.flush() del outputs[original_parent_msg_id] del docstring_callbacks[parent_msg_id] continue else: continue else: continue elif 'execution_state' in data['content']: if data['content']['execution_state']=='idle': if data['parent_header']['msg_type']=='execute_request': outputs[parent_msg_id]['status'] = 'complete' outputs[parent_msg_id]['stream'] = None outputs[parent_msg_id]['image'] = None outputs[parent_msg_id]['error'] = None sys.stdout.write(json.dumps(outputs[parent_msg_id]) + '\n') sys.stdout.flush() del outputs[parent_msg_id] continue elif data['header']['msg_type']=="execute_result": outputs[parent_msg_id]['output'] = data['content']['data'].get('text/plain', '') outputs[parent_msg_id]['stream'] = data['content']['data'].get('text/plain', '') elif data['header']['msg_type']=="display_data": if 'image/png' in data['content']['data']: outputs[parent_msg_id]['image'] = data['content']['data']['image/png'] elif 'text/html' in data['content']['data']: outputs[parent_msg_id]['html'] = data['content']['data']['text/html'] elif data['header']['msg_type']=="stream": outputs[parent_msg_id]['output'] += data['content'].get('text', '') outputs[parent_msg_id]['stream'] = data['content'].get('text', '') elif data['header']['msg_type']=="error": outputs[parent_msg_id]['error'] = "\n".join(data['content']['traceback']) elif data['header']['msg_type']=="input_request": outputs[parent_msg_id]['status'] = 'input' outputs[parent_msg_id]['stream'] = data['content'].get('prompt', '') sys.stdout.write(json.dumps(outputs[parent_msg_id]) + '\n') sys.stdout.flush() # TODO: figure out why this is here... outputs[parent_msg_id]['image'] = None outputs[parent_msg_id]['stream'] = None # handle autocomplete matches if 'matches' in data['content'] and data['msg_type']=='complete_reply' and data['parent_header']['msg_id']==msg_id: # we're going to get all the docstrings for our autocomplete options names = [] for completion in data['content']['matches']: names.append("'" + completion + "'") names = "[%s]" % ", ".join(names) cmd = '__get_docstrings(globals(), %s, %r)' % (names, "." in code) msg_id = kernel_client.execute(cmd) docstring_callbacks[msg_id] = parent_msg_id outputs[parent_msg_id]['output'] = ''
import os from jupyter_client.manager import start_new_kernel km, kc = start_new_kernel( kernel_name="python3", stderr=open(os.devnull, 'w'), cwd="F:\\Documents\\GitHub\\metabook\\server") message = """ import matplotlib.pyplot as plt import numpy as np t = np.arange(0.0, 2.0, 0.01) s = np.sin(2*np.pi*t) plt.plot(t, s) plt.xlabel('time (s)') plt.ylabel('voltage (mV)') plt.title('About as simple as it gets, folks') plt.grid(True) plt.show() """ sent_msg_id = kc.execute(message) reply = kc.get_shell_msg(sent_msg_id) data = kc.iopub_channel.get_msg(timeout=4) message = "plt"
def kernel(wd=None, verbose=0): # setup ipython kernel and configure it kernel_manager, kernel_client = manager.start_new_kernel(extra_arguments=["--matplotlib='inline'"]) current_timeout_min = 0.0005 current_timeout_max = 0.01 current_timeout = current_timeout_max acceptable_types = [ "execute_input", "stream", "display_data", "error", "execute_result", "execute_reply", "complete_reply" ] input_queue = Queue.Queue() input_thread = threading.Thread(target=add_input, args=(input_queue,)) input_thread.daemon = True input_thread.start() # we're up and running! sys.stdout.write(json.dumps({ "id": "startup-complete", "status": "complete" }) + "\n") should_continue = True while should_continue: if not input_queue.empty(): current_timeout = current_timeout_min line = input_queue.get().strip() payload = json.loads(line) uid = payload["id"] args = payload.get("args", []) kwargs = payload.get("kwargs", {}) method = payload.get("method", False) target_str = payload.get("target", "client") exec_eval = payload.get("exec_eval", False) if target_str == "manager": target = kernel_manager else: target = kernel_client if method: if getattr(target, method, False): result = getattr(target, method)(*args, **kwargs) if result: sys.stdout.write(json.dumps({"source": "link", "result": result, "id": uid }) + '\n') else: sys.stdout.write(json.dumps({ "error": "Missing method " + method, "id": uid }) + '\n') if exec_eval: result = eval(exec_eval) sys.stdout.write(json.dumps({ "source": "eval", "result": result, "id": uid }) + '\n') try: data = kernel_client.get_shell_msg(timeout=current_timeout) content = data.get('content', False) if content: payload = content.get('payload', False) if payload: try: first = payload[0] if first: source = first.get('source', False) keepkernel = first.get('keepkernel', False) if source == 'ask_exit' and keepkernel == False: should_continue = False except IndexError: pass msg_type = data.get('msg_type', False) if msg_type == 'shutdown_reply': shutdown_restart = content.get('restart', False) if not shutdown_restart: should_continue = False sys.stdout.write(json.dumps({"source": "shell", "result": data, "should_continue": should_continue}, default=json_serial) + '\n') current_timeout = current_timeout_min except Empty: pass try: while True: data = kernel_client.get_iopub_msg(timeout=current_timeout) sys.stdout.write(json.dumps({"source": "iopub", "result": data}, default=json_serial) + '\n') sys.stdout.flush() current_timeout = current_timeout_min except Empty: pass try: data = kernel_client.get_stdin_msg(timeout=current_timeout) sys.stdout.write(json.dumps({"source": "stdin", "result": data}, default=json_serial) + '\n') sys.stdout.flush() current_timeout = current_timeout_min except Empty: pass current_timeout = min(current_timeout * 1.1, current_timeout_max)
def kernel(wd=None, verbose=0): # setup ipython kernel and configure it kernel_mgr, kernel_client = manager.start_new_kernel() # apply patches kernel_client.execute("%matplotlib inline") dirname = os.path.dirname(os.path.abspath(__file__)) python_patch_file = os.path.join(dirname, "langs", "python-patch.py") kernel_client.execute("%run " + python_patch_file) # set working directory if wd: kernel_client.execute("cd %s" % wd) input_queue = Queue.Queue() input_thread = threading.Thread(target=add_input, args=(input_queue, )) input_thread.daemon = True input_thread.start() outputs = {} while True: if not input_queue.empty(): line = input_queue.get().strip() payload = json.loads(line) execution_id = payload['id'] code = payload['code'] complete = payload.get('complete', False) if verbose > 0: sys.stderr.write(line + '\n') if complete == True: msg_id = kernel_client.complete(code) elif complete == 'input': msg_id = kernel_client.stdin_channel.execute(code) else: msg_id = kernel_client.execute(code, allow_stdin=False) if code == "interrupt_kernel": sys.stderr.write("interrupting kernel\n") sys.stderr.flush() kernel_mgr.interrupt_kernel() reply = { "id": execution_id, "msg_id": msg_id, "output": "", "status": "complete", "stream": None, "image": None, "error": None } sys.stdout.write(json.dumps(reply) + '\n') sys.stdout.flush() continue outputs[msg_id] = { "id": execution_id, "msg_id": msg_id, "output": "", "stream": None, "image": None, "error": None } sys.stdout.write( json.dumps({ "msg_id": msg_id, "id": execution_id, "code": code }) + '\n') sys.stdout.flush() data = None try: data = kernel_client.get_iopub_msg(timeout=0.1) except Empty: try: data = kernel_client.get_shell_msg(timeout=0.1) except: try: data = kernel_client.get_stdin_msg(timeout=0.1) except: continue parent_msg_id = data['parent_header']['msg_id'] if parent_msg_id not in outputs: continue if verbose > 0: pp.pprint(data, sys.stderr) # handle code execution results if 'execution_state' in data['content']: if data['content']['execution_state'] == 'idle': if data['parent_header']['msg_type'] == 'execute_request': outputs[parent_msg_id]['status'] = 'complete' outputs[parent_msg_id]['stream'] = None outputs[parent_msg_id]['image'] = None outputs[parent_msg_id]['error'] = None sys.stdout.write(json.dumps(outputs[parent_msg_id]) + '\n') sys.stdout.flush() del outputs[parent_msg_id] continue elif data['header']['msg_type'] == "execute_result": outputs[parent_msg_id]['output'] = data['content']['data'].get( 'text/plain', '') outputs[parent_msg_id]['stream'] = data['content']['data'].get( 'text/plain', '') elif data['header']['msg_type'] == "display_data": if 'image/png' in data['content']['data']: outputs[parent_msg_id]['image'] = data['content']['data'][ 'image/png'] elif 'text/html' in data['content']['data']: outputs[parent_msg_id]['html'] = data['content']['data'][ 'text/html'] elif data['header']['msg_type'] == "stream": outputs[parent_msg_id]['output'] += data['content'].get('text', '') outputs[parent_msg_id]['stream'] = data['content'].get('text', '') elif data['header']['msg_type'] == "error": outputs[parent_msg_id]['error'] = "\n".join( data['content']['traceback']) elif data['header']['msg_type'] == "input_request": outputs[parent_msg_id]['status'] = 'input' outputs[parent_msg_id]['stream'] = data['content'].get( 'prompt', '') sys.stdout.write(json.dumps(outputs[parent_msg_id]) + '\n') sys.stdout.flush() # TODO: figure out why this is here... outputs[parent_msg_id]['image'] = None outputs[parent_msg_id]['stream'] = None # handle autocomplete matches if 'matches' in data['content'] and data[ 'msg_type'] == 'complete_reply' and data['parent_header'][ 'msg_id'] == msg_id: results = [] for completion in data['content']['matches']: result = {'value': completion, 'dtype': '---'} if '.' in code: # result['text'] = result['value'] # '.'.join(result['value'].split('.')[1:]) result['text'] = result['value'] #.split('.')[-1] result['dtype'] = 'function' else: result['text'] = result['value'] result['dtype'] = '' # type(globals().get(code)).__name__ results.append(result) outputs[parent_msg_id]['output'] = results outputs[parent_msg_id]['status'] = 'complete' sys.stdout.write(json.dumps(outputs[parent_msg_id]) + '\n') sys.stdout.flush() del outputs[parent_msg_id]
def kernel(wd=None, verbose=0): # setup ipython kernel and configure it kernel_mgr, kernel_client = manager.start_new_kernel(extra_arguments=["--matplotlib='inline'"]) # apply patches dirname = os.path.dirname(os.path.abspath(__file__)) python_patch_file = os.path.join(dirname, "langs", "python-patch.py") kernel_client.execute("%run " + python_patch_file) # set working directory if wd: kernel_client.execute("cd %s" % wd) input_queue = Queue.Queue() input_thread = threading.Thread(target=add_input, args=(input_queue,)) input_thread.daemon = True input_thread.start() outputs = {} while True: if not input_queue.empty(): line = input_queue.get().strip() payload = json.loads(line) execution_id = payload['id'] code = payload['code'] complete = payload.get('complete', False) if verbose > 0: sys.stderr.write(line + '\n') if complete==True: msg_id = kernel_client.complete(code) elif complete=='input': msg_id = kernel_client.stdin_channel.execute(code) else: msg_id = kernel_client.execute(code, allow_stdin=False) if code=="interrupt_kernel": sys.stderr.write("interrupting kernel\n") sys.stderr.flush() kernel_mgr.interrupt_kernel() reply = { "id": execution_id, "msg_id": msg_id, "output": "", "status": "complete", "stream": None, "image": None, "error": None } sys.stdout.write(json.dumps(reply) + '\n') sys.stdout.flush() continue outputs[msg_id] = { "id": execution_id, "msg_id": msg_id, "output": "", "stream": None, "image": None, "error": None } sys.stdout.write(json.dumps({ "msg_id": msg_id, "id": execution_id, "code": code }) + '\n') sys.stdout.flush() data = None try: data = kernel_client.get_iopub_msg(timeout=0.1) except Empty: try: data = kernel_client.get_shell_msg(timeout=0.1) except: try: data = kernel_client.get_stdin_msg(timeout=0.1) except: continue parent_msg_id = data['parent_header']['msg_id'] if parent_msg_id not in outputs: continue if verbose > 0: pp.pprint(data, sys.stderr) # handle code execution results if 'execution_state' in data['content']: if data['content']['execution_state']=='idle': if data['parent_header']['msg_type']=='execute_request': outputs[parent_msg_id]['status'] = 'complete' outputs[parent_msg_id]['stream'] = None outputs[parent_msg_id]['image'] = None outputs[parent_msg_id]['error'] = None sys.stdout.write(json.dumps(outputs[parent_msg_id]) + '\n') sys.stdout.flush() del outputs[parent_msg_id] continue elif data['header']['msg_type']=="execute_result": outputs[parent_msg_id]['output'] = data['content']['data'].get('text/plain', '') outputs[parent_msg_id]['stream'] = data['content']['data'].get('text/plain', '') elif data['header']['msg_type']=="display_data": if 'image/png' in data['content']['data']: outputs[parent_msg_id]['image'] = data['content']['data']['image/png'] elif 'text/html' in data['content']['data']: outputs[parent_msg_id]['html'] = data['content']['data']['text/html'] elif data['header']['msg_type']=="stream": outputs[parent_msg_id]['output'] += data['content'].get('text', '') outputs[parent_msg_id]['stream'] = data['content'].get('text', '') elif data['header']['msg_type']=="error": outputs[parent_msg_id]['error'] = "\n".join(data['content']['traceback']) elif data['header']['msg_type']=="input_request": outputs[parent_msg_id]['status'] = 'input' outputs[parent_msg_id]['stream'] = data['content'].get('prompt', '') sys.stdout.write(json.dumps(outputs[parent_msg_id]) + '\n') sys.stdout.flush() # TODO: figure out why this is here... outputs[parent_msg_id]['image'] = None outputs[parent_msg_id]['stream'] = None # handle autocomplete matches if 'matches' in data['content'] and data['msg_type']=='complete_reply' and data['parent_header']['msg_id']==msg_id: results = [] for completion in data['content']['matches']: result = { 'value': completion, 'dtype': '---' } if '.' in code: # result['text'] = result['value'] # '.'.join(result['value'].split('.')[1:]) result['text'] = result['value'] #.split('.')[-1] result['dtype'] = 'function' else: result['text'] = result['value'] result['dtype'] = '' # type(globals().get(code)).__name__ results.append(result) outputs[parent_msg_id]['output'] = results outputs[parent_msg_id]['status'] = 'complete' sys.stdout.write(json.dumps(outputs[parent_msg_id]) + '\n') sys.stdout.flush() del outputs[parent_msg_id]
import logging from jupyter_client.manager import start_new_kernel logging.basicConfig(level=logging.DEBUG) manager, client = start_new_kernel() client.execute("x = 1 + 2") client.execute("print('hello world', x)")