Beispiel #1
0
def start():
    session_id = uniq_id()
    print "starting session: %s"%session_id

    mgr, cli = start_new_kernel()
    SESSIONS[session_id] = Session(session_id, mgr, cli)
    return session_id
Beispiel #2
0
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)
Beispiel #3
0
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)
Beispiel #4
0
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)
    """
    # do we need validation of JSON?
    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()
Beispiel #5
0
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)
    """
    # do we need validation of JSON?
    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()
Beispiel #6
0
    def preprocess(self, nb, resources):
        path = resources.get('metadata', {}).get('path', '')
        if path == '':
            path = None

        from IPython.kernel.manager import start_new_kernel
        kernel_name = nb.metadata.get('kernelspec', {}).get('name', 'python')
        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 __init__(self):
     self.km, self.kc = start_new_kernel(extra_arguments=['--matplotlib=inline'],
                                         stderr=open(os.devnull, 'w'))
     # We need iopub to read every line in the cells
     self.iopub = self.kc.iopub_channel
            continue

        """
        Now we get the reply from the piece of code executed
        and analyse the outputs
        """
        reply = msg['content']
        out = NotebookNode(output_type=msg_type)

        print '--------------------------- CELL --------------------------'
        print reply
        print '------------------------- ORIGINAL CELL -------------------'
        print cell.outputs


km, kc = start_new_kernel(extra_arguments=['--matplotlib=inline'],
                          stderr=open(os.devnull, 'w'))


# def start_global_kernel(kernel='python'):
#     """start the global kernel (if it isn't running) and return its client"""
#     global KM, KC
#     if KM is None:
#         KM, KC = start_new_kernel(kernel)
#         atexit.register(stop_global_kernel)
#     else:
#         flush_channels(KC)
#     return KC

# km, kc = start_new_kernel('python')

# We need iopub to read every line in the cells
Beispiel #9
0
 def __init__(self):
     self.km, self.kc = start_new_kernel(
         extra_arguments=['--matplotlib=inline'],
         stderr=open(os.devnull, 'w'))
     # We need iopub to read every line in the cells
     self.iopub = self.kc.iopub_channel