def test_execute(self): """ Does executing code in an in-process kernel work? """ km = InProcessKernelManager() km.start_kernel() kc = BlockingInProcessKernelClient(kernel=km.kernel) kc.start_channels() kc.execute('foo = 1') self.assertEquals(km.kernel.shell.user_ns['foo'], 1)
def test_interface(self): """ Does the in-process kernel manager implement the basic KM interface? """ km = InProcessKernelManager() self.assert_(not km.has_kernel) km.start_kernel() self.assert_(km.has_kernel) self.assert_(km.kernel is not None) kc = km.client() self.assert_(not kc.channels_running) kc.start_channels() self.assert_(kc.channels_running) old_kernel = km.kernel km.restart_kernel() self.assertIsNotNone(km.kernel) self.assertNotEquals(km.kernel, old_kernel) km.shutdown_kernel() self.assert_(not km.has_kernel) self.assertRaises(NotImplementedError, km.interrupt_kernel) self.assertRaises(NotImplementedError, km.signal_kernel, 9) kc.stop_channels() self.assert_(not kc.channels_running)
def test_complete(self): """ Does requesting completion from an in-process kernel work? """ km = InProcessKernelManager() km.start_kernel() kc = BlockingInProcessKernelClient(kernel=km.kernel) kc.start_channels() km.kernel.shell.push({"my_bar": 0, "my_baz": 1}) kc.complete("my_ba", "my_ba", 5) msg = kc.get_shell_msg() self.assertEqual(msg["header"]["msg_type"], "complete_reply") self.assertEqual(sorted(msg["content"]["matches"]), ["my_bar", "my_baz"])
def test_complete(self): """ Does requesting completion from an in-process kernel work? """ km = InProcessKernelManager() km.start_kernel() kc = BlockingInProcessKernelClient(kernel=km.kernel) kc.start_channels() km.kernel.shell.push({'my_bar': 0, 'my_baz': 1}) kc.complete('my_ba', 'my_ba', 5) msg = kc.get_shell_msg() self.assertEqual(msg['header']['msg_type'], 'complete_reply') self.assertEqual(sorted(msg['content']['matches']), ['my_bar', 'my_baz'])
def test_object_info(self): """ Does requesting object information from an in-process kernel work? """ km = InProcessKernelManager() km.start_kernel() kc = BlockingInProcessKernelClient(kernel=km.kernel) kc.start_channels() km.kernel.shell.user_ns['foo'] = 1 kc.object_info('foo') msg = kc.get_shell_msg() self.assertEquals(msg['header']['msg_type'], 'object_info_reply') self.assertEquals(msg['content']['name'], 'foo') self.assertEquals(msg['content']['type_name'], 'int')
def get_kernel_pointer(buffer): lisp.message("getting kernel for " + buffer) if buffer not in kernels: lisp.message("creating new " + buffer) km = InProcessKernelManager() km.start_kernel() kc = BlockingInProcessKernelClient(kernel=km.kernel) kc.start_channels() kernel = InProcessKernel() kernels[buffer] = (kc, kernel, kernel.shell.get_ipython()) # run this so that plt, np pointers are ready kc.shell_channel.execute('%pylab inline') return kernels[buffer]
def get_kernel_pointer(buffer): lisp.message("getting kernel for " + buffer) if buffer not in kernels: lisp.message("creating new " + buffer) km = InProcessKernelManager() km.start_kernel() kc = BlockingInProcessKernelClient(kernel=km.kernel) kc.start_channels() kernel = InProcessKernel() kernels[buffer] = (kc,kernel,kernel.shell.get_ipython()) # run this so that plt, np pointers are ready kc.shell_channel.execute('%pylab inline') return kernels[buffer]
def test_object_info(self): """ Does requesting object information from an in-process kernel work? """ km = InProcessKernelManager() km.start_kernel() kc = BlockingInProcessKernelClient(kernel=km.kernel) kc.start_channels() km.kernel.shell.user_ns["foo"] = 1 kc.object_info("foo") msg = kc.get_shell_msg() self.assertEquals(msg["header"]["msg_type"], "object_info_reply") self.assertEquals(msg["content"]["name"], "foo") self.assertEquals(msg["content"]["type_name"], "int")
def test_history(self): """ Does requesting history from an in-process kernel work? """ km = InProcessKernelManager() km.start_kernel() kc = BlockingInProcessKernelClient(kernel=km.kernel) kc.start_channels() kc.execute('%who') kc.history(hist_access_type='tail', n=1) msg = kc.shell_channel.get_msgs()[-1] self.assertEquals(msg['header']['msg_type'], 'history_reply') history = msg['content']['history'] self.assertEquals(len(history), 1) self.assertEquals(history[0][2], '%who')
def test_history(self): """ Does requesting history from an in-process kernel work? """ km = InProcessKernelManager() km.start_kernel() kc = BlockingInProcessKernelClient(kernel=km.kernel) kc.start_channels() kc.execute("%who") kc.history(hist_access_type="tail", n=1) msg = kc.shell_channel.get_msgs()[-1] self.assertEquals(msg["header"]["msg_type"], "history_reply") history = msg["content"]["history"] self.assertEquals(len(history), 1) self.assertEquals(history[0][2], "%who")
def test_inspect(self): """ Does requesting object information from an in-process kernel work? """ km = InProcessKernelManager() km.start_kernel() kc = BlockingInProcessKernelClient(kernel=km.kernel) kc.start_channels() km.kernel.shell.user_ns['foo'] = 1 kc.inspect('foo') msg = kc.get_shell_msg() self.assertEqual(msg['header']['msg_type'], 'inspect_reply') content = msg['content'] assert content['found'] text = content['data']['text/plain'] self.assertIn('int', text)
def __init__(self): self.km = InProcessKernelManager() self.km.start_kernel() if platform.system() == 'Darwin': # There is sometimes a race condition where the first # execute command hits the kernel before it's ready. # It appears to happen only on Darwin (Mac OS) and an # easy (but clumsy) way to mitigate it is to sleep # for a second. sleep(1) self.kc = self.km.client() self.kc.start_channels() self.shell = self.kc.shell_channel self.iopub = self.kc.iopub_channel self.kc.kernel.shell.enable_matplotlib('inline') self.shell.execute(WARMUP) print("Result: {!r}".format(self.shell.get_msg())) try: while True: msg = self.iopub.get_msg(timeout=0) print("iopub Message: {!r}".format(msg)) except Empty: pass
class InProcessKernelTestCase(unittest.TestCase): def setUp(self): self.km = InProcessKernelManager() self.km.start_kernel() self.kc = self.km.client() self.kc.start_channels() self.kc.wait_for_ready() @skipif_not_matplotlib def test_pylab(self): """Does %pylab work in the in-process kernel?""" kc = self.kc kc.execute('%pylab') out, err = assemble_output(kc.iopub_channel) self.assertIn('matplotlib', out) def test_raw_input(self): """ Does the in-process kernel handle raw_input correctly? """ io = StringIO('foobar\n') sys_stdin = sys.stdin sys.stdin = io try: if py3compat.PY3: self.kc.execute('x = input()') else: self.kc.execute('x = raw_input()') finally: sys.stdin = sys_stdin self.assertEqual(self.km.kernel.shell.user_ns.get('x'), 'foobar') def test_stdout(self): """ Does the in-process kernel correctly capture IO? """ kernel = InProcessKernel() with capture_output() as io: kernel.shell.run_cell('print("foo")') self.assertEqual(io.stdout, 'foo\n') kc = BlockingInProcessKernelClient(kernel=kernel, session=kernel.session) kernel.frontends.append(kc) kc.execute('print("bar")') out, err = assemble_output(kc.iopub_channel) self.assertEqual(out, 'bar\n')
class InProcessKernelTestCase(unittest.TestCase): def setUp(self): self.km = InProcessKernelManager() self.km.start_kernel() self.kc = BlockingInProcessKernelClient(kernel=self.km.kernel) self.kc.start_channels() @skipif_not_matplotlib def test_pylab(self): """ Does pylab work in the in-process kernel? """ kc = self.kc kc.execute('%pylab') msg = get_stream_message(kc) self.assert_('matplotlib' in msg['content']['data']) def test_raw_input(self): """ Does the in-process kernel handle raw_input correctly? """ io = StringIO('foobar\n') sys_stdin = sys.stdin sys.stdin = io try: if py3compat.PY3: self.kc.execute('x = input()') else: self.kc.execute('x = raw_input()') finally: sys.stdin = sys_stdin self.assertEqual(self.km.kernel.shell.user_ns.get('x'), 'foobar') def test_stdout(self): """ Does the in-process kernel correctly capture IO? """ kernel = InProcessKernel() with capture_output() as io: kernel.shell.run_cell('print("foo")') self.assertEqual(io.stdout, 'foo\n') kc = BlockingInProcessKernelClient(kernel=kernel) kernel.frontends.append(kc) kc.shell_channel.execute('print("bar")') msg = get_stream_message(kc) self.assertEqual(msg['content']['data'], 'bar\n')
class InProcessKernelTestCase(unittest.TestCase): def setUp(self): self.km = InProcessKernelManager() self.km.start_kernel() self.kc = BlockingInProcessKernelClient(kernel=self.km.kernel) self.kc.start_channels() @skipif_not_matplotlib def test_pylab(self): """ Does pylab work in the in-process kernel? """ kc = self.kc kc.execute('%pylab') msg = get_stream_message(kc) self.assert_('Welcome to pylab' in msg['content']['data']) def test_raw_input(self): """ Does the in-process kernel handle raw_input correctly? """ io = StringIO('foobar\n') sys_stdin = sys.stdin sys.stdin = io try: if py3compat.PY3: self.kc.execute('x = input()') else: self.kc.execute('x = raw_input()') finally: sys.stdin = sys_stdin self.assertEqual(self.km.kernel.shell.user_ns.get('x'), 'foobar') def test_stdout(self): """ Does the in-process kernel correctly capture IO? """ kernel = InProcessKernel() with capture_output() as io: kernel.shell.run_cell('print("foo")') self.assertEqual(io.stdout, 'foo\n') kc = BlockingInProcessKernelClient(kernel=kernel) kernel.frontends.append(kc) kc.shell_channel.execute('print("bar")') msg = get_stream_message(kc) self.assertEqual(msg['content']['data'], 'bar\n')
def setUp(self): self.km = InProcessKernelManager() self.km.start_kernel() self.kc = BlockingInProcessKernelClient(kernel=self.km.kernel) self.kc.start_channels() self.kc.wait_for_ready()
def setUp(self): self.km = InProcessKernelManager() self.km.start_kernel() self.kc = BlockingInProcessKernelClient(kernel=self.km.kernel) self.kc.start_channels()
def setUp(self): self.km = InProcessKernelManager() self.km.start_kernel() self.kc = self.km.client() self.kc.start_channels() self.kc.wait_for_ready()
from Pymacs import lisp import re, sys, time, os interactions = {} kernels = {} from IPython.testing.globalipapp import get_ipython from IPython.utils.io import capture_output from IPython.kernel.inprocess.manager import InProcessKernelManager km = InProcessKernelManager() ip = get_ipython() def run_cell(cmd): with capture_output() as io: res = ip.run_cell(cmd) res_out = io.stdout return res_out ip.run_cell('import numpy as np') ip.run_cell('import matplotlib.pylab as plt') ip.run_cell('%load_ext autoreload') ip.run_cell('%autoreload 2') # make digits into length two - i.e. 1 into 01 def two_digit(i):
class NotebookRunner(object): # The kernel communicates with mime-types while the notebook # uses short labels for different cell types. We'll use this to # map from kernel types to notebook format types. MIME_MAP = { 'image/jpeg': 'jpeg', 'image/png': 'png', 'text/plain': 'text', 'text/html': 'html', 'text/latex': 'latex', 'application/javascript': 'html', } def __init__(self): self.km = InProcessKernelManager() self.km.start_kernel() if platform.system() == 'Darwin': # There is sometimes a race condition where the first # execute command hits the kernel before it's ready. # It appears to happen only on Darwin (Mac OS) and an # easy (but clumsy) way to mitigate it is to sleep # for a second. sleep(1) self.kc = self.km.client() self.kc.start_channels() self.shell = self.kc.shell_channel self.iopub = self.kc.iopub_channel self.kc.kernel.shell.enable_matplotlib('inline') self.shell.execute(WARMUP) print("Result: {!r}".format(self.shell.get_msg())) try: while True: msg = self.iopub.get_msg(timeout=0) print("iopub Message: {!r}".format(msg)) except Empty: pass def __del__(self): self.kc.stop_channels() self.km.shutdown_kernel() def run_cell(self, cell, autosave): ''' Run a notebook cell and update the output of that cell in-place. ''' logging.info('Running cell:\n%s\n', cell.input) self.shell.execute(cell.input) cell['outputs'] = [] while True: try: msg = self.iopub.get_msg(timeout=1) if msg['msg_type'] == 'status': if msg['content']['execution_state'] == 'idle': break except Empty: pass content = msg['content'] msg_type = msg['msg_type'] out = NotebookNode(output_type=msg_type) if 'execution_count' in content: cell['prompt_number'] = content['execution_count'] - 1 out.prompt_number = content['execution_count'] - 1 if msg_type in ['status', 'pyin']: continue elif msg_type == 'stream': out.stream = content['name'] out.text = content['data'] #print(out.text, end='') elif msg_type in ('display_data', 'pyout'): for mime, data in content['data'].items(): try: attr = self.MIME_MAP[mime] except KeyError: raise NotImplementedError('unhandled mime type: %s' % mime) setattr(out, attr, data) #print(data, end='') elif msg_type == 'pyerr': out.ename = content['ename'] out.evalue = content['evalue'] out.traceback = content['traceback'] #logging.error('\n'.join(content['traceback'])) else: raise NotImplementedError('unhandled iopub message: %s' % msg_type) cell['outputs'].append(out) if autosave: self.save_notebook(autosave) reply = self.shell.get_msg() status = reply['content']['status'] if status == 'error': logging.info('Cell raised uncaught exception: \n%s', '\n'.join(reply['content']['traceback'])) raise NotebookError() else: logging.info('Cell returned') def iter_code_cells(self): ''' Iterate over the notebook cells containing code. ''' for ws in self.nb.worksheets: for cell in ws.cells: if cell.cell_type == 'code': yield cell def run_notebook(self, nb_in, skip_exceptions=False, autosave=None): ''' Run all the cells of a notebook in order and update the outputs in-place. If ``skip_exceptions`` is set, then if exceptions occur in a cell, the subsequent cells are run (by default, the notebook execution stops). ''' self.nb = read(open(nb_in), 'json') for cell in self.iter_code_cells(): cell['outputs'] = [] if 'prompt_number' in cell: del cell['prompt_number'] if autosave is not None: self.save_notebook(autosave) for cell in self.iter_code_cells(): try: self.run_cell(cell, autosave = autosave) except NotebookError: if not skip_exceptions: raise if autosave is not None: self.save_notebook(autosave) def save_notebook(self, nb_out): logging.info('Saving to %s', nb_out) write(self.nb, open(nb_out, 'w'), 'json')