Execute a notebook via nbconvert and collect output.
       :returns (parsed nb object, execution errors)
760547
def run_notebook(notebook_path):
    
    dirname, __ = os.path.split(notebook_path)
    os.chdir(dirname)
    with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout:
        args = ["jupyter-nbconvert", "--to", "notebook", "--execute", "--allow-errors",
          "--ExecutePreprocessor.timeout=300",
          "--output", fout.name, notebook_path]
        try:
            subprocess.check_call(args)
        except subprocess.CalledProcessError as e:
            if e.returncode == 1:
              # print the message and ignore error with code 1 as this indicates there were errors in the notebook
              print(e.output)
              pass
            else:
              # all other codes indicate some other problem, rethrow
              raise

        fout.seek(0)
        nb = nbformat.read(fout, nbformat.current_nbformat)

    errors = [output for cell in nb.cells if "outputs" in cell
                     for output in cell["outputs"]\
                     if output.output_type == "error"]

    return nb, errors
752871
def load_nb(cls, inline=True):
        
        LOAD_MIME_TYPE = bokeh.io.notebook.LOAD_MIME_TYPE
        bokeh.io.notebook.LOAD_MIME_TYPE = MIME_TYPES['jlab-hv-load']
        load_notebook(hide_banner=True, resources=INLINE if inline else CDN)
        bokeh.io.notebook.LOAD_MIME_TYPE = LOAD_MIME_TYPE
        bokeh.io.notebook.curstate().output_notebook()
152856
def run(self, batch: mx.io.DataBatch) -> List[mx.nd.NDArray]:
        
        self.module.forward(batch, is_train=False)
        return self.module.get_outputs()
833992
def writef(notebook, nb_file, fmt=None):
    """"""
    if nb_file == '-':
        write(notebook, sys.stdout, fmt)
        return

    _, ext = os.path.splitext(nb_file)
    fmt = copy(fmt or {})
    fmt = long_form_one_format(fmt, update={'extension': ext})

    create_prefix_dir(nb_file, fmt)

    with io.open(nb_file, 'w', encoding='utf-8') as stream:
        write(notebook, stream, fmt)
445820
def load_nb(cls, inline=True):
        
        import matplotlib.pyplot as plt
        backend = plt.get_backend()
        if backend not in ['agg', 'module://ipykernel.pylab.backend_inline']:
            plt.switch_backend('agg')
1277515
def update_notebooks(source_path, dest_path=None, update_html=True, document_new_fns=False,
                     update_nb_links=True, html_path=None, force=False):
    ""
    from .convert2html import convert_nb
    source_path = Path(source_path)

    if source_path.is_file():
        dest_path = source_path.parent if dest_path is None else Path(dest_path)
        html_path = dest_path/'..'/'docs' if html_path is None else Path(html_path)
        doc_path = source_path
        assert source_path.suffix == '.ipynb', 'Must update from notebook or module'
        if document_new_fns:
            mod = import_mod(get_module_from_notebook(source_path))
            if not mod: print('Could not find module for path:', source_path)
            elif mod.__file__.endswith('__init__.py'): pass
            else: update_module_page(mod, dest_path)
        generate_missing_metadata(doc_path)
        if update_nb_links:
            print(f'Updating notebook {doc_path}. Please wait...')
            link_nb(doc_path)
            execute_nb(doc_path, {'metadata': {'path': doc_path.parent}}, show_doc_only=True)
        if update_html:
            check_nbconvert_version()
            html_fn = html_path/doc_path.with_suffix('.html').name
            if not force and html_fn.is_file():
                in_mod  = os.path.getmtime(doc_path)
                out_mod = os.path.getmtime(html_fn)
                if in_mod < out_mod: return
            convert_nb(doc_path, html_path)

    elif (source_path.name.startswith('fastai.')):
        # Do module update
        assert dest_path is not None, 'To update a module, you must specify a destination folder for where notebook resides'
        mod = import_mod(source_path.name)
        if not mod: return print('Could not find module for:', source_path)
        doc_path = Path(dest_path)/(strip_fastai(mod.__name__)+'.ipynb')
        if not doc_path.exists():
            print('Notebook does not exist. Creating:', doc_path)
            create_module_page(mod, dest_path)
        update_notebooks(doc_path, dest_path=dest_path, update_html=update_html, document_new_fns=document_new_fns,
                         update_nb_links=update_nb_links, html_path=html_path)
    elif source_path.is_dir():
        for f in sorted(Path(source_path).glob('*.ipynb')):
            update_notebooks(f, dest_path=dest_path, update_html=update_html, document_new_fns=document_new_fns,
                             update_nb_links=update_nb_links, html_path=html_path)
    else: print('Could not resolve source file:', source_path)
540977
def in_notebook():
    
    try:
        # function returns IPython context, but only in IPython
        ipy = get_ipython()  # NOQA
        # we only want to render rich output in notebooks
        # in terminals we definitely do not want to output HTML
        name = str(ipy.__class__).lower()
        terminal = 'terminal' in name

        # spyder uses ZMQshell, and can appear to be a notebook
        spyder = '_' in os.environ and 'spyder' in os.environ['_']

        # assume we are in a notebook if we are not in
        # a terminal and we haven't been run by spyder
        notebook = (not terminal) and (not spyder)

        return notebook

    except BaseException:
        return False
335014
def execute(self, input_data):
        '''  '''
        raw_bytes = input_data['sample']['raw_bytes']
        zipfile_output = zipfile.ZipFile(StringIO(raw_bytes))
        payload_md5s = []
        for name in zipfile_output.namelist():
            filename = os.path.basename(name)
            payload_md5s.append(self.workbench.store_sample(zipfile_output.read(name), name, 'unknown'))
        return {'payload_md5s': payload_md5s}
690308
def nbopen(filename):
    
    filename = osp.abspath(filename)
    home_dir = get_home_dir()
    server_info = find_best_server(filename)

    if server_info is not None:
        print("Using existing server at", server_info['notebook_dir'])
        return server_info
    else:
        if filename.startswith(home_dir):
            nbdir = home_dir
        else:
            nbdir = osp.dirname(filename)

        print("Starting new server")
        command = [sys.executable, '-m', 'notebook', '--no-browser',
                   '--notebook-dir={}'.format(nbdir),
                   '--NotebookApp.password=',
                   "--KernelSpecManager.kernel_spec_class='{}'".format(
                           KERNELSPEC)]

        if os.name == 'nt':
            creation_flag = 0x08000000  # CREATE_NO_WINDOW
        else:
            creation_flag = 0  # Default value

        if DEV:
            env = os.environ.copy()
            env["PYTHONPATH"] = osp.dirname(get_module_path('spyder'))
            proc = subprocess.Popen(command, creationflags=creation_flag,
                                    env=env)
        else:
            proc = subprocess.Popen(command, creationflags=creation_flag)

        # Kill the server at exit. We need to use psutil for this because
        # Popen.terminate doesn't work when creationflags or shell=True
        # are used.
        def kill_server_and_childs(pid):
            ps_proc = psutil.Process(pid)
            for child in ps_proc.children(recursive=True):
                child.kill()
            ps_proc.kill()

        atexit.register(kill_server_and_childs, proc.pid)

        # Wait ~25 secs for the server to be up
        for _x in range(100):
            server_info = find_best_server(filename)
            if server_info is not None:
                break
            else:
                time.sleep(0.25)

        if server_info is None:
            raise NBServerError()

        return server_info
525647
def preprocess(
        self, nb: "NotebookNode", resources: dict
    ) -> Tuple["NotebookNode", dict]:
        """"""
        for index, cell in enumerate(nb.cells):
            if "## Solution" in cell.source:
                nb.cells[index + 1].source = ""

        return nb, resources
999668
def do_execute(self):
        
        if isinstance(self.input.payload, classes.JavaObject) and self.input.payload.is_serializable:
            copy = serialization.deepcopy(self.input.payload)
            if copy is not None:
                self._output.append(Token(copy))
        else:
            self._output.append(self.input)
        return None
1297180
def execute_command_no_results(self, sock_info, generator):
        
        full_result = {
            "writeErrors": [],
            "writeConcernErrors": [],
            "nInserted": 0,
            "nUpserted": 0,
            "nMatched": 0,
            "nModified": 0,
            "nRemoved": 0,
            "upserted": [],
        }
        # Ordered bulk writes have to be acknowledged so that we stop
        # processing at the first error, even when the application
        # specified unacknowledged writeConcern.
        write_concern = WriteConcern()
        op_id = _randint()
        try:
            self._execute_command(
                generator, write_concern, None,
                sock_info, op_id, False, full_result)
        except OperationFailure:
            pass
586846
def notebook_executed(pth):
    """"""

    nb = nbformat.read(pth, as_version=4)
    for n in range(len(nb['cells'])):
        if nb['cells'][n].cell_type == 'code' and \
                nb['cells'][n].execution_count is None:
            return False
    return True
458566
def write_nb(root, nb_name, cells):
    
    nb = new_notebook(cells=cells,
                      metadata={
                        'language': 'python',
                        })
    nb_path = os.path.join(root, '%s.ipynb' % nb_name)
    with codecs.open(nb_path, encoding='utf-8', mode='w') as nb_file:
        nbformat.write(nb, nb_file, NB_VERSION)
    print("Created Jupyter notebook at:\n%s" % nb_path)
543012
def execute_notebook(self, data):
        
        self.create_notebook(data)
        self.run_notebook()
        self.wait_for_notebook_to_execute()
        body = self.get_executed_notebook()

        err = False
        output = []
        for paragraph in body['paragraphs']:
            if 'results' in paragraph and paragraph['results']['code'] == 'ERROR':
                output.append(paragraph['results']['msg'][0]['data'])
                err = True

            elif 'result' in paragraph and paragraph['result']['code'] == 'ERROR':
                output.append(paragraph['result']['msg'])
                err = True

        [print(e.strip() + '\n', file=sys.stderr) for e in output if e]

        if err:
            sys.exit(1)

        if not self.output_path:
            print(json.dumps(body, indent=2))
        else:
            self.save_notebook(body)
421597
def script_post_save(model, os_path, contents_manager, **kwargs):
    
    from nbconvert.exporters.script import ScriptExporter

    if model['type'] != 'notebook':
        return

    global _script_exporter

    if _script_exporter is None:
        _script_exporter = ScriptExporter(parent=contents_manager)

    log = contents_manager.log

    base, ext = os.path.splitext(os_path)
    # py_fname = base + '.py'
    script, resources = _script_exporter.from_filename(os_path)
    script_fname = base + resources.get('output_extension', '.txt')
    log.info("Saving script /%s", to_api_path(script_fname, contents_manager.root_dir))

    with io.open(script_fname, 'w', encoding='utf-8') as f:
        f.write(script)
606688
def register(nbtool):
    
    global _py_funcs
    _lazy_init()

    # Save references to the tool's load() and render() functions
    load_key = nbtool.origin + '|' + nbtool.id + '|load'
    render_key = nbtool.origin + '|' + nbtool.id + '|render'
    _py_funcs[load_key] = nbtool.load
    _py_funcs[render_key] = nbtool.render

    # Clean optional metadata for inclusion in JavaScript
    clean_description = "null" if nbtool.description is None else '"' + nbtool.description.replace('"','\\"') + '"'
    clean_version = "null" if nbtool.version is None else '"' + nbtool.version.replace('"','\\"') + '"'
    clean_tags = "null" if nbtool.tags is None else json.dumps(nbtool.tags)
    clean_attributes = "null" if nbtool.attributes is None else json.dumps(nbtool.attributes)

    # Pass the metadata to JavaScript
    IPython.display.display_javascript("""
        console.log('ok');
        NBToolManager.instance().register(new NBToolManager.NBTool({
            origin: "%s",
            id: "%s",
            name: "%s",
            description: %s,
            version: %s,
            tags: %s,
            attributes: %s,
            load: function() {
                var x = Jupyter.notebook.kernel.execute('nbtools._py_funcs["%s"]()',
                    {
                        iopub: {
                            output: function(response) {
                                // Print the return value of the Python code to the console
                                console.log(response.content.data["text/plain"]);
                            }
                        }
                    },
                    {
                        silent: false,
                        store_history: false,
                        stop_on_error: true
                    });
                return true;
            },
            render: function() {
                var x = Jupyter.notebook.kernel.execute('nbtools._py_funcs["%s"]()',
                    {
                        iopub: {
                            output: function(response) {
                                // Print the return value of the Python code to the console
                                console.log(response.content.data["text/plain"]);
                            }
                        }
                    },
                    {
                        silent: false,
                        store_history: false,
                        stop_on_error: true
                    });
                return null;
            },
        }));
    """ % (nbtool.origin, nbtool.id, nbtool.name,
           clean_description, clean_version, clean_tags, clean_attributes,
           load_key, render_key), raw=True)
    return True