DEV Community

Isaac Land
Isaac Land

Posted on Originally published at enter.scholay.com

Keeping Jupyter Notebook Edits Readable in Git

Keeping Jupyter Notebook Edits Readable in Git

A colleague changes the last observation in a notebook from six to nine. The question for a reviewer is simple: why did that value change? A diff of an ipynb file can bury the one-line change under execution counts, image data and cell metadata. Jupytext lets the team keep a second representation of the notebook. The author can execute cells in Jupyter, while the reviewer sees cell inputs as ordinary text in Git.[1][3]

Pair a notebook with a text file

Install Jupytext in the environment that starts the Jupyter server, for example with pip install jupytext or conda install -c conda-forge jupytext. A kernel can live in a different environment. Installing only there may leave a working conversion command but no pairing command in the interface. Restart the server, open the command palette in JupyterLab, search for Jupytext, choose a pairing for an existing notebook and save. Look for the companion file beside the original. The project uses the MIT license.[1][2]

For a code-heavy analysis choose py:percent. Python code remains readable in an ordinary editor, cell boundaries appear as # %%, and Markdown cells carry a distinct marker. A Markdown pairing can be easier to review for a notebook that is mostly prose. Both formats preserve inputs. Large printed tables and images belong to the ipynb file. Picking one text format for the project gives each change one obvious place for review.[1][3]

Follow one changed observation

Run jupytext --set-formats ipynb,py:percent mean.ipynb in the project directory. It records the pairing and creates the companion text file. Our example notebook takes the mean of two, four and six. Its stored output is four. In the text file, replace six with nine, save, then run jupytext --sync mean.ipynb. Reading the resulting notebook shows the new input and the old output of four. Calculating the mean of the revised observations gives five. Copying the number next to the updated input into a report would copy the wrong result.[3][4]

The edited input, old output and recomputed mean

Figure: Mean after synchronization and execution.

Opening a pair loads inputs from its more recent member and any saved outputs from the ipynb file. Saving updates both members and can recreate a missing one. If Git tracks only the text file, a collaborator can open and save it in Jupyter with Jupytext installed to create a local notebook. The text has no historical plots to recover. Reproducing the last plot needs the input data, the execution environment and a fresh run.[3]

Hand the analysis to a collaborator

For a project with several notebooks, set formats = "ipynb,py:percent" in a project-level jupytext.toml and share the configuration with the text files. Start review at the changed cell, then identify results that depend on it. A particular figure cited in a paper can be saved as a separate output so that the team knows which artifact to regenerate.[4][5]

In a merge request, separate the reason for the edit from the check on its effect. The last observation might be a corrected entry. Record both the old and new mean and the cell responsible. Someone with only the text file can check three numbers by hand before rerunning the whole analysis. This helps distinguish an input error, a calculation error and a stale display.

Save work in Jupyter before switching to an external editor. If both stay open, the documentation recommends disabling autosave and reloading when returning to Jupyter. Where both copies have unsaved contributions, preserve a text copy and reconcile the meaningful lines. Finish by checking the value the report actually cites: after the change from six to nine, the fresh run must produce five.[3]

References

  1. [1] Jupytext project repository

  2. [2] Jupytext installation

  3. [3] Paired notebooks

  4. [4] Jupytext CLI

  5. [5] Jupytext configuration


Originally published at EnterScholar.

Scholay ยท EnterScholar

Top comments (0)