コード例 #1
0
def executor():
    """
    Creates an executor context in which any call to the executor
    is executed in sequence in the same kernel. For example:
    with executor() as execute:
        execute(create_cell("a = 3"))
        execute(create_cell("print(a)"))
    """
    def execute(cells):
        # If just one cell is passed, put it in a list
        if not isinstance(cells, list) and not isinstance(cells, tuple):
            cells = [cells]

        # Execute all cells
        results = []
        for index, cell in enumerate(cells):
            try:
                results.append(ep.preprocess_cell(cell, {}, index))
            except CellExecutionError as e:
                raise check50.Failure(str(e))

        return results

    # Start an ExecutePreprocessor: https://nbconvert.readthedocs.io/en/latest/execute_api.html
    # https://github.com/jupyter/nbconvert/blob/master/nbconvert/preprocessors/execute.py
    ep = ExecutePreprocessor(timeout=600, kernel_name='python3')

    # https://github.com/jupyter/nbconvert/blob/e49b9a8a220c9f9f87c23764ea7b48eac4707937/nbconvert/preprocessors/execute.py#L23
    # this new dependency on NotebookClient also creates some ugliness on our end
    ep.store_history = True            
    ep.nb = {"cells": {}}

    # Start a Kernel Manager
    with ep.setup_kernel():
        yield execute
コード例 #2
0
        def execute_notebook(notebook_filename, notebook_save_filename,
                             params):
            log = UserMessages()

            with open(notebook_filename) as file_handler:
                notebook = nbformat.read(file_handler, as_version=4)
                b_errors = False
                execute_preprocessor = ExecutePreprocessor(
                    timeout=args.get('cell_timeout'),
                    allow_errors=args.get('allow_errors'))
                kernel_manager = None
                kernel_comm = None
                progress_bar = args.get('enable_progress_bar')

                if params:
                    for nb_cell in notebook.cells:
                        if nb_cell.cell_type == 'code':
                            new_cell_source = utils.substitute_params(
                                nb_cell.source, params)
                            nb_cell.source = new_cell_source
                            break

                try:
                    if progress_bar:

                        progress_bar = widgets.IntProgress(
                            value=0,
                            min=0,
                            max=len(notebook.cells),
                            step=1,
                            bar_style=
                            'info',  # 'success', 'info', 'warning', 'danger' or ''
                            orientation='horizontal')

                        kernel_manager, kernel_comm = start_new_kernel(
                            kernel_name=notebook['metadata']['kernelspec']
                            ['name'])
                        execute_preprocessor.km = kernel_manager
                        execute_preprocessor.kc = kernel_comm
                        execute_preprocessor.nb = notebook

                        display_label = notebook_filename
                        if notebook_save_filename:
                            display_label = display_label + ' : ' + notebook_save_filename
                        display(
                            widgets.HBox(
                                [widgets.Label(display_label), progress_bar]))

                        for idx, nb_cell in enumerate(notebook.cells):
                            execute_preprocessor.preprocess_cell(
                                nb_cell,
                                resources={'metadata': {}},
                                cell_index=idx)
                            progress_bar.value = idx + 1
                    else:
                        log.info("Running Notebook: " + notebook_filename)
                        execute_preprocessor.preprocess(
                            notebook, {'metadata': {}})
                except CellExecutionError:
                    b_errors = True
                    if progress_bar:
                        progress_bar.bar_style = 'danger'
                    raise
                except AttributeError:
                    b_errors = True
                    if progress_bar:
                        progress_bar.bar_style = 'danger'
                    raise
                finally:
                    if notebook_save_filename:
                        with open(notebook_save_filename,
                                  mode='wt') as file_handler:
                            nbformat.write(notebook, file_handler)

                    if kernel_manager or kernel_comm:
                        kernel_comm.stop_channels()
                        kernel_manager.shutdown_kernel()

                    if not b_errors:
                        if progress_bar:
                            progress_bar.bar_style = 'success'
                        else:
                            log.info(notebook_filename +
                                     " was executed successfully.")
                    elif b_errors and not progress_bar:
                        log.error(notebook_filename + " execution failed.")