How to use the nbformat.writes function in nbformat

To help you get started, we’ve selected a few nbformat examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github bokeh / bokeh / tests / unit / bokeh / application / handlers / test_notebook__handlers.py View on Github external
def with_file_object(f):
        nbsource = nbformat.writes(contents)
        f.write(nbsource.encode("UTF-8"))
        f.flush()
        func(f.name)
    with_temporary_file(with_file_object)
github py4ds / nbless / tests / make_temp.py View on Github external
def exec_notebook(tmp_path: Path) -> str:
    """Helper function to create a list of pathlib Path objects."""
    nb = tmp_path / "notebook.ipynb"
    nb.write_text(writes(nbless(make_files(tmp_path), kernel="python3")))
    return nb.as_posix()
github jupyter / nbgrader / nbgrader / nbgraderformat / v3.py View on Github external
def writes_v3(nb, **kwargs):
    MetadataValidatorV3().validate_nb(nb)
    return _writes(nb, **kwargs)
github Metatab / metatab / metatab / jupyter / exporters.py View on Github external
self.lib_dirs = eld.lib_dirs

        efm = ExtractFinalMetatabDoc()
        efm.preprocess(nb_copy, {})

        if not efm.doc:
            raise MetapackError("Notebook does not contain the metatab output from %mt_final_metatab. Check the executed notebook source")

        self.doc = efm.doc

        for section, term, value in self.extra_terms:
            self.doc[section].get_or_new_term(term, value)

        nb, _ = RemoveMetatab().preprocess(nb, {})

        resources['outputs']['notebooks/{}.ipynb'.format(self.package_name)] = nbformat.writes(nb).encode('utf-8')

        return efm.doc.as_csv(), resources
github dean0x7d / pybinding / docs / _ext / nbexport.py View on Github external
def translate(self):
        visitor = NBTranslator(self.document, self.app, self.docpath)
        self.document.walkabout(visitor)
        nb = _finilize_markdown_cells(visitor.nb)

        if self.app.config.nbexport_execute:
            ep = ExecutePreprocessor(allow_errors=True)
            try:
                ep.preprocess(nb, {'metadata': {}})
            except CellExecutionError as e:
                self.app.warn(str(e))

        self.output = nbformat.writes(nb)
github py4ds / nbless / src / cli / nbless_cli.py View on Github external
def nbless_cli(in_files: List[str], kernel: str, out_file: str) -> None:
    """Create an executed Jupyter notebook from markdown and code files.

    :param in_files: The source files used to create a Jupyter notebook file.
    :param kernel: The programming language used to run the notebook.
    :param out_file: The name of the output Jupyter notebook file.
    """
    nb = nbless(in_files, kernel) if kernel else nbless(in_files)
    if out_file:
        nbformat.write(nb, out_file, version=4)
    else:
        sys.stdout.write(nbformat.writes(nb))
github py4ds / nbless / src / cli / nbexec_cli.py View on Github external
def nbexec_cli(in_file: str, kernel: str, out_file: str) -> None:
    """Create an executed notebook without modifying the input notebook.

    :param in_file: The name of the input Jupyter notebook file.
    :param kernel: The programming language used to execute the notebook.
    :param out_file: The name of the output Jupyter notebook file.
    """
    nb = nbexec(in_file, kernel) if kernel else nbexec(in_file)
    if out_file:
        nbformat.write(nb, out_file, version=4)
    else:
        sys.stdout.write(nbformat.writes(nb))
github QuantEcon / sphinxcontrib-jupyter / sphinxcontrib / jupyter / writers / notebook.py View on Github external
def writes(self):
        """
        https://nbformat.readthedocs.io/en/latest/api.html#nbformat.writes
        """
        return nbformat.writes(self.nb)
github nteract / papermill / papermill / iorw.py View on Github external
def write_ipynb(nb, path):
    """Saves a notebook object to the specified path.
    Args:
        nb_node (nbformat.NotebookNode): Notebook object to save.
        notebook_path (str): Path to save the notebook object to.
    """
    papermill_io.write(nbformat.writes(nb), path)
github d2l-ai / d2l-zh / sphinx_plugin.py View on Github external
c['source'] = c['source'].replace(
                        '"../data', '"data').replace("'../data", "'data")
            notedown.run(notebook, timeout)
            print('=== Finished in %f sec'%(time.time()-tic))

        # even that we will check it later, but do it ealier so we can see the
        # error message before evaluating all notebooks
        _check_notebook(notebook)

        # write
        # need to add language info to for syntax highlight
        notebook['metadata'].update({'language_info':{'name':'python'}})
        new_fname = _replace_ext(new_fname, 'ipynb')
        print('=== Convert %s -> %s' % (fname, new_fname))
        with open(new_fname, 'w') as f:
            f.write(nbformat.writes(notebook))

        converted_files.append((fname, new_fname))
    return converted_files