Skip to content

Instantly share code, notes, and snippets.

@matthewfeickert
Last active March 24, 2021 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewfeickert/20815c415adc891c015053cbd4bcc3e5 to your computer and use it in GitHub Desktop.
Save matthewfeickert/20815c415adc891c015053cbd4bcc3e5 to your computer and use it in GitHub Desktop.
Example of problems with versioning and cell ID in Jupyter notebooks
.ipynb_checkpoints/

Known Issue

Since creating this Gist I've found out that this is a known issue and being worked on in JupyterLab Issue #9729.

A temporary work around until the next release of JupyterLab would be to downgrade nbformat to v5.0.8 (at the time the last non-yanked release before v5.1.2) which sidesteps the problem by not supporting cell IDs (and removing them from the notebook).

Versioning problem example

With the addition of cell IDs to Jupyter notebooks (c.f. JEP 62) the ability to cleanly version them, even with all output cleaned from them, has become even harder given that subsequent runs of the notebooks change the cell ID and create a non-empty diff.

Note that this only seems to be a problem in JupyterLab. :?

Also reproducible in Binder

Environment setup

$ pyenv virtualenv 3.8.7 example
$ pyenv activate example
(example) $ pip install --upgrade pip setuptools wheel
(example) $ pip install -r requirements.txt
(example) $ jupyter lab

Example of non-issue in Jupyter Notebook

  1. Run jupyter notebook
  2. Open example.ipynb
  3. Kernel -> Restart & Run All
  4. Kernel -> Restart & Clear Output
  5. Save example.ipynb
  6. Shutdown the running instance of Jupyter
  7. git diff

Reproduction steps of problem in JupyterLab

This only seems to be a problem in JupyterLab, interestingly enough.

  1. Run jupyter lab
  2. Open example.ipynb
  3. Kernel -> Restart Kernel and Run All Cells
  4. Kernel -> Restart Kernel and Clear All Outputs
  5. Save example.ipynb
  6. Shutdown the running instance of Jupyter
  7. git diff

An example diff is included in clean_run.diff.

diff --git a/example.ipynb b/example.ipynb
index 66713cf..89cc325 100644
--- a/example.ipynb
+++ b/example.ipynb
@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
- "id": "expanded-samoa",
+ "id": "satisfied-forth",
"metadata": {},
"source": [
"# Example of problems with cell names and version control"
@@ -11,7 +11,7 @@
{
"cell_type": "code",
"execution_count": null,
- "id": "powerful-reverse",
+ "id": "royal-lottery",
"metadata": {},
"outputs": [],
"source": [
@@ -21,7 +21,7 @@
{
"cell_type": "code",
"execution_count": null,
- "id": "tropical-woman",
+ "id": "naked-quarter",
"metadata": {},
"outputs": [],
"source": [
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
numpy
jupyter
jupyterlab==3.0.12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment