Python nbformat.writes() Examples

The following are 24 code examples of nbformat.writes(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module nbformat , or try the search function .
Example #1
Source File: pandoc.py    From jupytext with MIT License 6 votes vote down vote up
def notebook_to_md(notebook):
    """Convert a notebook to its Markdown representation, using Pandoc"""
    tmp_file = tempfile.NamedTemporaryFile(delete=False)
    tmp_file.write(ipynb_writes(notebook).encode("utf-8"))
    tmp_file.close()

    pandoc(
        u"--from ipynb --to markdown -s --atx-headers --wrap=preserve --preserve-tabs",
        tmp_file.name,
        tmp_file.name,
    )

    with open(tmp_file.name, encoding="utf-8") as opened_file:
        text = opened_file.read()

    os.unlink(tmp_file.name)
    return "\n".join(text.splitlines()) 
Example #2
Source File: Benchmarking Jupytext.py    From jupytext with MIT License 6 votes vote down vote up
def sample_perf(nb, n=30):
    samples = pd.DataFrame(
        pd.np.NaN,
        index=pd.MultiIndex.from_product(
            (range(n), ['nbformat'] + JUPYTEXT_FORMATS), names=['sample', 'implementation']),
        columns=pd.Index(['size', 'read', 'write'], name='measure'))

    for i, fmt in samples.index:
        t0 = time.time()
        if fmt == 'nbformat':
            text = nbformat.writes(nb)
        else:
            text = jupytext.writes(nb, fmt)
        t1 = time.time()
        samples.loc[(i, fmt), 'write'] = t1 - t0
        samples.loc[(i, fmt), 'size'] = len(text)
        t0 = time.time()
        if fmt == 'nbformat':
            nbformat.reads(text, as_version=4)
        else:
            jupytext.reads(text, fmt)
        t1 = time.time()
        samples.loc[(i, fmt), 'read'] = t1 - t0
    return samples 
Example #3
Source File: build.py    From d2l-book with Apache License 2.0 6 votes vote down vote up
def process_and_eval_notebook(input_fn, output_fn, run_cells, timeout=20*60,
                              lang='python', tab=None, default_tab=None):
    with open(input_fn, 'r') as f:
        md = f.read()
    nb = notebook.read_markdown(md)
    if tab:
        # get the tab
        nb = notebook.split_markdown_cell(nb)
        nb = notebook.get_tab_notebook(nb, tab, default_tab)
        if not nb:
            logging.info(f"Skip to eval tab {tab} for {input_fn}")
            # write an emtpy file to track the dependencies
            open(output_fn, 'w')
            return
    # evaluate
    if run_cells:
        # change to the notebook directory to resolve the relpaths properly
        cwd = os.getcwd()
        os.chdir(os.path.join(cwd, os.path.dirname(output_fn)))
        notedown.run(nb, timeout)
        os.chdir(cwd)
    # write
    nb['metadata'].update({'language_info':{'name':lang}})
    with open(output_fn, 'w') as f:
        f.write(nbformat.writes(nb)) 
Example #4
Source File: build.py    From d2l-book with Apache License 2.0 6 votes vote down vote up
def update_ipynb_toc(root):
    """Change the toc code block into a list of clickable links"""
    notebooks = find_files('**/*.ipynb', root)
    for fn in notebooks:
        nb = notebook.read(fn)
        if not nb:
            continue
        for cell in nb.cells:
            if (cell.cell_type == 'markdown' and '```toc' in cell.source):
                md_cells = markdown.split_markdown(cell.source)
                for c in md_cells:
                    if c['type'] == 'code' and c['class'] == 'toc':
                        toc = []
                        for l in c['source'].split('\n'):
                            if l and not l.startswith(':'):
                                toc.append(' - [%s](%s.ipynb)'%(l,l))
                        c['source'] = '\n'.join(toc)
                        c['type'] = 'markdown'
                cell.source = markdown.join_markdown_cells(md_cells)
        with open(fn, 'w') as f:
            f.write(nbformat.writes(nb)) 
Example #5
Source File: nbmerge.py    From nnabla with Apache License 2.0 6 votes vote down vote up
def merge_notebooks(filenames):
    merged = None
    for fname in filenames:
        with io.open(fname, 'r', encoding='utf-8') as f:
            nb = nbformat.read(f, as_version=4)
        if merged is None:
            merged = nb
        else:
            # TODO: add an optional marker between joined notebooks
            # like an horizontal rule, for example, or some other arbitrary
            # (user specified) markdown cell)
            merged.cells.extend(nb.cells)
    if not hasattr(merged.metadata, 'name'):
        merged.metadata.name = ''
    merged.metadata.name += "_merged"
    print(nbformat.writes(merged)) 
Example #6
Source File: jupyterpdf.py    From sphinxcontrib-jupyter with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def write_doc(self, docname, doctree):
        # work around multiple string % tuple issues in docutils;
        # replace tuples in attribute values with lists
        doctree = doctree.deepcopy()
        destination = docutils.io.StringOutput(encoding="utf-8")

        ### output notebooks for executing for single pdfs, the urlpath should be set to website url
        self.writer._set_ref_urlpath(self.config["jupyter_pdf_urlpath"])
        self.writer._set_jupyter_download_nb_image_urlpath(None)
        self.writer.write(doctree, destination)

        # get a NotebookNode object from a string
        nb = nbformat.reads(self.writer.output, as_version=4)
        nb = self.update_Metadata(nb)

        ### execute the notebook - keep it forcefully on
        strDocname = str(docname)
        if strDocname in self.execution_vars['dependency_lists'].keys():
            self.execution_vars['delayed_notebooks'].update({strDocname: nb})
        else:        
            self._execute_notebook_class.execute_notebook(self, nb, docname, self.execution_vars, self.execution_vars['futures'])

        ### mkdir if the directory does not exist
        outfilename = os.path.join(self.outdir, os_path(docname) + self.out_suffix)
        ensuredir(os.path.dirname(outfilename))

        try:
            with codecs.open(outfilename, "w", "utf-8") as f:
                self.writer.output = nbformat.writes(nb, version=4)
                f.write(self.writer.output)
        except (IOError, OSError) as err:
            self.logger.warning("error writing file %s: %s" % (outfilename, err)) 
Example #7
Source File: notebook.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def from_notebook_node(self, nb, resources=None, **kw):
        nb_copy, resources = super(NotebookExporter, self).from_notebook_node(nb, resources, **kw)
        if self.nbformat_version != nb_copy.nbformat:
            resources['output_suffix'] = '.v%i' % self.nbformat_version
        else:
            resources['output_suffix'] = '.nbconvert'
        output = nbformat.writes(nb_copy, version=self.nbformat_version)
        if not output.endswith("\n"):
            output = output + "\n"
        return output, resources 
Example #8
Source File: sagemaker.py    From d2l-book with Apache License 2.0 5 votes vote down vote up
def generate_notebooks(self, eval_dir, sagemaker_dir, tab):
        if not self._valid:
            return
        utils.run_cmd(['rm -rf', sagemaker_dir])
        utils.run_cmd(['cp -r', eval_dir, sagemaker_dir])
        notebooks = utils.find_files('**/*.ipynb', sagemaker_dir)
        for fn in notebooks:
            nb = notebook.read(fn)
            if not nb:
                continue
            colab.update_notebook_kernel(nb, self._kernel[tab])
            colab.insert_additional_installation(nb, self._libs[tab], self.config['libs_header'])
            with open(fn, 'w') as f:
                f.write(nbformat.writes(nb)) 
Example #9
Source File: colab.py    From d2l-book with Apache License 2.0 5 votes vote down vote up
def generate_notebooks(self, eval_dir, colab_dir, tab):
        if not self._valid:
            return
        # copy notebook fron eval_dir to colab_dir
        utils.run_cmd(['rm -rf', colab_dir])
        utils.run_cmd(['cp -r', eval_dir, colab_dir])
        notebooks = utils.find_files('**/*.ipynb', colab_dir)
        for fn in notebooks:
            nb = notebook.read(fn)
            if not nb:
                continue
            # Use Python3 as the kernel
            update_notebook_kernel(nb, "python3", "Python 3")
            # Check if GPU is needed
            use_gpu = False
            for cell in nb.cells:
                if cell.cell_type == 'code':
                    if self.config['gpu_pattern'] in cell.source:
                        use_gpu = True
                        break
            if use_gpu:
                nb['metadata'].update({"accelerator": "GPU"})
                logging.info('Use GPU for '+fn)
            # Update SVG image URLs
            if self.config['replace_svg_url']:
                _update_svg_urls(nb, self.config['replace_svg_url'], fn, colab_dir)
            insert_additional_installation(nb, self._libs[tab], self.config['libs_header'])
            with open(fn, 'w') as f:
                f.write(nbformat.writes(nb)) 
Example #10
Source File: PEP8NotebookBear.py    From coala-bears with GNU Affero General Public License v3.0 5 votes vote down vote up
def notebook_node_to_string_list(notebook_node):
    """
    Writes a NotebookNode to a list of strings.

    :param notebook_node: The notebook as NotebookNode to write.
    :return:              The notebook as list of strings (linewise).
    """
    return nbformat.writes(notebook_node, nbformat.NO_CONVERT).splitlines(True) 
Example #11
Source File: iota_run_nb.py    From aws-iot-analytics-notebook-containers with Apache License 2.0 5 votes vote down vote up
def _write_output_to_s3(context):
    ipynb_json = nbformat.writes(context.nb)
    _write_to_s3(context.output_ipynb_s3_uri, ipynb_json, context.s3)
    basic_html, _ = HTMLExporter(template_file='basic').from_notebook_node(context.nb)
    _write_to_s3(context.output_html_s3_uri, basic_html, context.s3) 
Example #12
Source File: __init__.py    From jgscm with MIT License 5 votes vote down vote up
def _save_notebook(self, path, nb):
        """
        Uploads notebook to GCS.
        :param path: blob path.
        :param nb: :class:`nbformat.notebooknode.NotebookNode` instance.
        :return: created :class:`google.cloud.storage.Blob`.
        """
        bucket_name, bucket_path = self._parse_path(path)
        bucket = self._get_bucket(bucket_name, throw=True)
        data = nbformat.writes(nb, version=nbformat.NO_CONVERT)
        blob = bucket.blob(bucket_path)
        blob.upload_from_string(data, "application/x-ipynb+json")
        return blob 
Example #13
Source File: jupyter.py    From sphinxcontrib-jupyter with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def translate(self):
        self.document.settings.newlines = \
            self.document.settings.indents = \
            self.builder.env.config.xml_pretty

        visitor = self.translator_class(self.builder, self.document)

        self.document.walkabout(visitor)
        self.output = nbformat.writes(visitor.output) 
Example #14
Source File: ipynb.py    From knowledge-repo with Apache License 2.0 5 votes vote down vote up
def from_file(self, filename):
        import nbformat
        from nbconvert import MarkdownExporter
        from jinja2 import DictLoader
        from traitlets.config import Config

        c = Config()
        # c.ExtractOutputPreprocessor.extract_output_types = set()
        c.ExtractOutputPreprocessor.output_filename_template = 'images/{unique_key}_{cell_index}_{index}{extension}'
        c.NbConvertBase.display_data_priority = ['application/javascript', 'text/html', 'text/markdown',
                                                 'image/svg+xml', 'text/latex', 'image/png', 'image/jpeg',
                                                 'text/plain']

        nb = nbformat.read(filename, as_version=4)

        dl = DictLoader({'full.tpl': TEMPLATE})
        md_exporter = MarkdownExporter(config=c, extra_loaders=[
                                       dl], template_file='full.tpl')
        (body, resources) = md_exporter.from_notebook_node(nb)

        self.kp_write(body, images={name.split(
            'images/')[1]: data for name, data in resources.get('outputs', {}).items()})

        # Add cleaned ipynb file
        for cell in nb['cells']:
            if cell['cell_type'] == 'code':
                cell['outputs'] = []  # remove output data
                cell['execution_count'] = None  # reset to not executed
        self.kp.write_src(os.path.basename(filename), nbformat.writes(nb)) 
Example #15
Source File: _notebook.py    From podoc with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def dumps(self, nb):
        return nbformat.writes(nb, _NBFORMAT_VERSION) 
Example #16
Source File: hdfsio.py    From hdfscontents with Apache License 2.0 5 votes vote down vote up
def _save_notebook(self, hdfs_path, nb):
        """Save a notebook to an os_path."""
        # Convert the notebook to unicode string
        notebook_json = nbformat.writes(nb, version=nbformat.NO_CONVERT)

        with self.atomic_writing(hdfs_path) as f:
            # Write the notebook on hdfs
            f.write(notebook_json.encode('utf-8')) 
Example #17
Source File: hdfsio.py    From hdfscontents with Apache License 2.0 5 votes vote down vote up
def atomic_writing(self, hdfs_path):
        """wrapper around atomic_writing that turns permission errors to 403.
        Depending on flag 'use_atomic_writing', the wrapper perform an actual atomic writing or
        simply writes the file (whatever an old exists or not)"""
        with self.perm_to_403(hdfs_path):
            if self.use_atomic_writing:
                with atomic_writing(self.hdfs, hdfs_path) as f:
                    yield f
            else:
                with _simple_writing(self.hdfs, hdfs_path) as f:
                    yield f 
Example #18
Source File: hdfsio.py    From hdfscontents with Apache License 2.0 5 votes vote down vote up
def path_to_intermediate(path):
    """Name of the intermediate file used in atomic writes.
    The .~ prefix will make Dropbox ignore the temporary file."""
    dirname, basename = os.path.split(path)
    return os.path.join(dirname, '.~' + basename) 
Example #19
Source File: cli.py    From dagster with Apache License 2.0 5 votes vote down vote up
def get_notebook_scaffolding(kernelspec):
    check.dict_param(kernelspec, 'kernelspec', key_type=str, value_type=str)

    notebook = nbformat.v4.new_notebook()

    notebook.cells = [get_import_cell(), get_parameters_cell()]

    metadata = {'celltoolbar': 'Tags', 'kernelspec': kernelspec}

    notebook.metadata = metadata

    return nbformat.writes(notebook) 
Example #20
Source File: execute.py    From jupyter-sphinx with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def write_notebook_output(notebook, output_dir, notebook_name, location=None):
    """Extract output from notebook cells and write to files in output_dir.

    This also modifies 'notebook' in-place, adding metadata to each cell that
    maps output mime-types to the filenames the output was saved under.
    """
    resources = dict(unique_key=os.path.join(output_dir, notebook_name), outputs={})

    # Modifies 'resources' in-place
    ExtractOutputPreprocessor().preprocess(notebook, resources)
    # Write the cell outputs to files where we can (images and PDFs),
    # as well as the notebook file.
    FilesWriter(build_directory=output_dir).write(
        nbformat.writes(notebook),
        resources,
        os.path.join(output_dir, notebook_name + ".ipynb"),
    )
    # Write a script too.  Note that utf-8 is the de facto
    # standard encoding for notebooks. 
    ext = notebook.metadata.get("language_info", {}).get("file_extension", None)
    if ext is None:
        ext = ".txt"
        js.logger.warning(
            "Notebook code has no file extension metadata, " "defaulting to `.txt`",
            location=location,
        )
    contents = "\n\n".join(cell.source for cell in notebook.cells)
    with open(os.path.join(output_dir, notebook_name + ext), "w",
              encoding = "utf8") as f:
        f.write(contents) 
Example #21
Source File: fsmanager.py    From jupyter-fs with Apache License 2.0 5 votes vote down vote up
def _save_notebook(self, path, nb):
        """Save a notebook to an os_path."""
        s = nbformat.writes(nb, version=nbformat.NO_CONVERT)
        self._pyfilesystem_instance.writetext(path, s) 
Example #22
Source File: test_bundler.py    From jupyter-docx-bundler with MIT License 5 votes vote down vote up
def test_jupyter_nbconvert_cli(tmpdir, download_notebook):
    ipynb_filename = os.path.join(tmpdir, 'download_notebook.ipynb')
    with open(ipynb_filename, 'w', encoding='utf8') as file:
        file.write(nbformat.writes(download_notebook))

    app = nbconvertapp.NbConvertApp()
    app.initialize(argv=[ipynb_filename, '--to', 'jupyter_docx_bundler.DocxExporter'])
    app.convert_notebooks() 
Example #23
Source File: jupytext.py    From jupytext with MIT License 4 votes vote down vote up
def writes(notebook, fmt, version=nbformat.NO_CONVERT, **kwargs):
    """"
    Write a notebook to a file name or a file object

    :param notebook: the notebook
    :param fmt: the jupytext format like `md`, `py:percent`, ...
    :param version: see nbformat.writes
    :param kwargs: (not used) additional parameters for nbformat.writes
    :return: the text representation of the notebook
    """
    metadata = deepcopy(notebook.metadata)
    rearrange_jupytext_metadata(metadata)
    fmt = copy(fmt)
    fmt = long_form_one_format(fmt, metadata)
    ext = fmt["extension"]
    format_name = fmt.get("format_name")

    jupytext_metadata = metadata.get("jupytext", {})

    if ext == ".ipynb":
        # Remove jupytext section if empty
        jupytext_metadata.pop("text_representation", {})
        if not jupytext_metadata:
            metadata.pop("jupytext", {})
        return nbformat.writes(
            NotebookNode(
                nbformat=notebook.nbformat,
                nbformat_minor=notebook.nbformat_minor,
                metadata=metadata,
                cells=notebook.cells,
            ),
            version,
            **kwargs
        )

    if not format_name:
        format_name = format_name_for_ext(metadata, ext, explicit_default=False)

    if format_name:
        fmt["format_name"] = format_name
        update_jupytext_formats_metadata(metadata, fmt)

    writer = TextNotebookConverter(fmt)
    return writer.writes(notebook, metadata) 
Example #24
Source File: jupytext.py    From jupytext with MIT License 4 votes vote down vote up
def write(nb, fp, version=nbformat.NO_CONVERT, fmt=None, **kwargs):
    """"
    Write a notebook to a file name or a file object

    :param nb: the notebook
    :param fp: a file name or a file object
    :param version: see nbformat.write
    :param fmt: (optional if fp is a file name) the jupytext format like `md`, `py:percent`, ...
    :param kwargs: (not used) additional parameters for nbformat.write
    """
    if fp == "-":
        # Use sys.stdout.buffer when possible, and explicit utf-8 encoding, cf. #331
        content = writes(nb, version=version, fmt=fmt, **kwargs)
        try:
            # Python 3
            sys.stdout.buffer.write(content.encode("utf-8"))
        except AttributeError:
            sys.stdout.write(content.encode("utf-8"))
        return

    if not hasattr(fp, "write"):
        # Treat fp as a file name
        fp = str(fp)
        _, ext = os.path.splitext(fp)
        fmt = copy(fmt or {})
        fmt = long_form_one_format(fmt, update={"extension": ext})
        create_prefix_dir(fp, fmt)

        with io.open(fp, "w", encoding="utf-8") as stream:
            write(nb, stream, version=version, fmt=fmt, **kwargs)
            return
    else:
        assert (
            fmt is not None
        ), "'fmt' argument in jupytext.write is mandatory unless fp is a file name"

    content = writes(nb, version=version, fmt=fmt, **kwargs)
    if isinstance(content, bytes):
        content = content.decode("utf8")
    fp.write(content)
    if not content.endswith(u"\n"):
        fp.write(u"\n")