jupytext icon indicating copy to clipboard operation
jupytext copied to clipboard

Percent format with outputs - Sponsors wanted!

Open mwouts opened this issue 3 years ago • 4 comments

I'd like to be add support for outputs to the py:percent format.

This seems easier to do that adding outputs to Markdown (#220), as we have less constraints on how outputs should be included in the text document.

The project is described at https://github.com/mwouts/nbpercent/#readme - essentially the idea is to export all outputs (except simple text outputs) to PNG, HTML, Markdown files.

Such a text format for notebook would improve the experience of version control for notebooks with outputs, and would also eliminate the need to keep paired text and ipynb files in sync.

I'd like to be working on this in the next two months, but I am looking for a sponsor - please contact me through email (on my GitHub account).

mwouts avatar Apr 21 '22 09:04 mwouts

This project has the potential to make diffs on notebooks easy to read without requiring any additional tool - see how a change in the code of a plot is rendered on our proof of concept: sample_diff

mwouts avatar Apr 21 '22 13:04 mwouts

Any update on this issue? I would love to include the output of code cells in the percent py files

bruttif avatar Apr 06 '23 20:04 bruttif

Your idea looks pretty neat! I pray for this will be integrated into Jupytext soon. Best.

maikol-solis avatar Sep 13 '23 23:09 maikol-solis

I have been looking for a simple way to render a notebook to myst and preserve code generated image outputs in particular. As as a really simple, minimal hack, it looks like we can modify the myst.py notebook_to_myst() function to grab any code cell image output and append it as an image in a specially tagged markdown cell:

# https://github.com/mwouts/jupytext/blob/4b5651237fabce3ad49f5c397cbd2e6b34c1a943/src/jupytext/myst.py#L412
            added_md_cell = False
            for output in cell.outputs:
                if "data" in output and "image/png" in output["data"]:
                    metadata = {"tags": ["jp-previous-cell-output"]}
                    string += f"\n+++ {json.dumps(metadata)}\n"
                    string += (
                        f'![](data:image/png;base64,{output["data"]["image/png"]})\n'
                    )
                    added_md_cell = True
            last_cell_md = added_md_cell

We can round trip on this by ignoring any jp-previous-cell-output tagged markdown cell in myst_to_notebook():

# https://github.com/mwouts/jupytext/blob/4b5651237fabce3ad49f5c397cbd2e6b34c1a943/src/jupytext/myst.py#L343
            if "tags" in md_metadata and "jp-previous-cell-output" in md_metadata["tags"]:
                pass
            else:
                _flush_markdown(md_start_line, token, md_metadata)
            md_metadata = read_cell_metadata(token, len(notebook.cells))
            md_start_line = token.map[1]

I imagine things would get more complex as more output types are handled, but as a simple way of exporting notebooks with generated images to myst, it seems to work.

psychemedia avatar Nov 21 '23 17:11 psychemedia