Beispiel #1
0
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"),
    )

    exporter = nbconvert.exporters.ScriptExporter(
        log=LoggerAdapterWrapper(js.logger))
    with warnings.catch_warnings():
        # See https://github.com/jupyter/nbconvert/issues/1388
        warnings.simplefilter('ignore', DeprecationWarning)
        contents, resources = exporter.from_notebook_node(notebook)

    notebook_file = notebook_name + resources['output_extension']
    output_dir = Path(output_dir)
    # utf-8 is the de-facto standard encoding for notebooks.
    (output_dir / notebook_file).write_text(contents, encoding="utf8")
Beispiel #2
0
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)

    notebook_file = notebook_name + ext
    output_dir = Path(output_dir)
    (output_dir / notebook_file).write_text(contents, encoding = "utf8")
Beispiel #3
0
def write_notebook_output(notebook, output_dir, notebook_name):
    """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.language_info.file_extension
    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)
Beispiel #4
0
def remove_solutions(nb, nb_name):
    """Convert solution cells to markdown; embed images from Python output."""

    # -- Extract image data from the cell outputs
    c = Config()
    template = (f"../static/{nb_name}"
                "_Solution_{cell_index}_{index}{extension}")
    c.ExtractOutputPreprocessor.output_filename_template = template

    # Note: using the RST exporter means we need to install pandoc as a dep
    # in the github workflow, which adds a little bit of latency, and we don't
    # actually care about the RST output. It's just a convenient way to get the
    # image resources the way we want them.
    exporter = RSTExporter()
    extractor = ExtractOutputPreprocessor(config=c)
    exporter.register_preprocessor(extractor, True)
    _, resources = exporter.from_notebook_node(nb)

    # -- Convert solution cells to markdown with embedded image
    nb_cells = nb.get("cells", [])
    outputs = resources["outputs"]
    solution_resources = {}

    for i, cell in enumerate(nb_cells):
        cell_text = cell["source"].replace(" ", "").lower()
        if cell_text.startswith("#@titlesolution"):

            # Just remove solution cells that generate no outputs
            if not cell["outputs"]:
                nb_cells.remove(cell)
                continue

            # Filter the resources for solution images
            image_paths = [k for k in outputs if f"Solution_{i}" in k]
            solution_resources.update({k: outputs[k] for k in image_paths})

            # Conver the solution cell to markdown, strip the source,
            # and embed the image as a link to static resource
            new_source = "**Example output:**\n\n" + "\n\n".join(
                [f"<img src='{f}' align='left'>" for f in image_paths])
            cell["source"] = new_source
            cell["cell_type"] = "markdown"
            del cell["outputs"]
            del cell["execution_count"]

    return nb, solution_resources
Beispiel #5
0
def write_notebook_output(notebook, output_dir, notebook_name):
    """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 Python script too.
    contents = '\n\n'.join(cell.source for cell in notebook.cells)
    with open(os.path.join(output_dir, notebook_name + '.py'), 'w') as f:
        f.write(contents)
Beispiel #6
0
def convert_to_rst(nb, basedir, fpath, fstem):
    # Convert to .rst formats
    exp = RSTExporter()
    c = Config()
    c.TagRemovePreprocessor.remove_cell_tags = {"hide"}
    c.TagRemovePreprocessor.remove_input_tags = {"hide-input"}
    c.TagRemovePreprocessor.remove_all_outputs_tags = {"hide-output"}
    c.ExtractOutputPreprocessor.output_filename_template = (
        f"{fstem}_files/{fstem}_" + "{cell_index}_{index}{extension}"
    )
    exp.register_preprocessor(TagRemovePreprocessor(config=c), True)
    exp.register_preprocessor(ExtractOutputPreprocessor(config=c), True)
    body, resources = exp.from_notebook_node(nb)

    # Clean the output on the notebook and save a .ipynb back to disk
    print(f"Writing clean {fpath} ... ", end=" ", flush=True)
    nb = strip_output(nb)
    with open(fpath, "wt") as f:
        nbformat.write(nb, f)

    # Write the .rst file
    rst_path = os.path.join(basedir, f"{fstem}.rst")
    print(f"Writing {rst_path}")
    with open(rst_path, "w") as f:
        f.write(body)

    print(resources["outputs"])

    # Write the individual image outputs
    imdir = os.path.join(basedir, f"{fstem}_files")
    if not os.path.exists(imdir):
        os.mkdir(imdir)
    for imname, imdata in resources["outputs"].items():
        if imname.startswith(fstem):
            impath = os.path.join(basedir, f"{imname}")
            with open(impath, "wb") as f:
                f.write(imdata)
                f.write(imdata)
Beispiel #7
0
                        field["data"].pop(key)
                if not field["data"]:
                    fields.remove(field)

    # Convert to .rst formats
    exp = RSTExporter()

    c = Config()
    c.TagRemovePreprocessor.remove_cell_tags = {"hide"}
    c.TagRemovePreprocessor.remove_input_tags = {"hide-input"}
    c.TagRemovePreprocessor.remove_all_outputs_tags = {"hide-output"}
    c.ExtractOutputPreprocessor.output_filename_template = \
        f"{fstem}_files/{fstem}_" + "{cell_index}_{index}{extension}"

    exp.register_preprocessor(TagRemovePreprocessor(config=c), True)
    exp.register_preprocessor(ExtractOutputPreprocessor(config=c), True)

    body, resources = exp.from_notebook_node(nb)

    # Clean the output on the notebook and save a .ipynb back to disk
    print(f"Writing clean {fpath} ... ", end=" ", flush=True)
    nb = strip_output(nb)
    with open(fpath, "wt") as f:
        nbformat.write(nb, f)

    # Write the .rst file
    rst_path = os.path.join(basedir, f"{fstem}.rst")
    print(f"Writing {rst_path}")
    with open(rst_path, "w") as f:
        f.write(body)
Beispiel #8
0
    def extract_resources(self, nb, resources):

        output_filename_template = "image_{cell_index}_{index}{extension}"

        return ExtractOutputPreprocessor(output_filename_template=output_filename_template) \
            .preprocess(nb, resources)