Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Percent format with outputs - Sponsors wanted! #951

Open
mwouts opened this issue Apr 21, 2022 · 4 comments
Open

Percent format with outputs - Sponsors wanted! #951

mwouts opened this issue Apr 21, 2022 · 4 comments

Comments

@mwouts
Copy link
Owner

mwouts commented Apr 21, 2022

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
Copy link
Owner Author

mwouts commented Apr 21, 2022

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

@bruttif
Copy link

bruttif commented Apr 6, 2023

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

@maikol-solis
Copy link

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

@psychemedia
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants