def get_client(cf, profile=None): """ Usage: >>> kc = get_client('kernel-143a2687-f294-42b1-bdcb-6f1cc2f4cc87.json', 'dale') >>> data = kc.execute("'123'") >>> data {u'text/plain': u'123'} """ connection_file = find_connection_file(cf, profile=profile) km = KernelManager(connection_file=connection_file) km.load_connection_file() client = km.client() return KernelClient(client)
def preprocess(self, nb, resources): kernel_name = nb.metadata.get('kernelspec', {}).get('name', 'python') self.km = KernelManager(kernel_name=kernel_name) self.km.start_kernel(startup_timeout=60, extra_arguments=self.extra_arguments) self.kc = self.km.client() self.kc.start_channels(stdin=False) self.kc.wait_for_ready() try: self.log.info("Executing notebook with kernel: %s" % kernel_name) nb, resources = super(ExecutePreprocessor, self).preprocess(nb, resources) finally: self.kc.stop_channels() self.km.shutdown_kernel(now=True) return nb, resources
def __enter__(self): self.km = KernelManager() self.km.start_kernel(extra_arguments=self.extra_arguments, stderr=open(os.devnull, 'w')) self.kc = self.km.client() self.kc.start_channels() self.iopub = self.kc.iopub_channel self.shell = self.kc.shell_channel # run %pylab inline, because some notebooks assume this # even though they shouldn't self.shell.send("pass") self.shell.get_msg() while True: try: self.iopub.get_msg(timeout=1) except Empty: break return self