|
| 1 | +# Notebooks and version control |
| 2 | + |
| 3 | +:::{objectives} |
| 4 | +- Demonstrate two tools which make version control of notebooks easier. |
| 5 | +::: |
| 6 | + |
| 7 | +[this episode is adapted after <https://coderefinery.github.io/jupyter/version-control/>] |
| 8 | + |
| 9 | +Jupyter Notebooks are stored in [JSON](https://en.wikipedia.org/wiki/JSON) format. |
| 10 | +With this format it can be a bit difficult to compare and merge changes which are introduced |
| 11 | +through the notebook interface. |
| 12 | + |
| 13 | + |
| 14 | +## Packages and JupyterLab extensions to simplify version control |
| 15 | + |
| 16 | +Several packages and JupyterLab extensions have been developed |
| 17 | +to make it easier to interact with Git and GitHub: |
| 18 | + |
| 19 | +- [nbdime](http://nbdime.readthedocs.io/) (notebook "diff" and "merge") provides |
| 20 | + "content-aware" diffing and merging. |
| 21 | + - Adds a Git button to the notebook interface. |
| 22 | + - `git diff` and `git merge` shell commands can use nbdime's diff |
| 23 | + and merge for notebook files, but leave Git's behavior unchanged |
| 24 | + for non-notebook files. |
| 25 | +- [jupyterlab-git](https://github.com/jupyterlab/jupyterlab-git) |
| 26 | + is a JupyterLab extension for version control using Git. |
| 27 | + - Adds a Git tab to the left-side menu bar for version control inside JupyterLab. |
| 28 | +- [JupyterLab GitHub](https://www.npmjs.com/package/@jupyterlab/github) |
| 29 | + is a JupyterLab extension for accessing GitHub repositories. |
| 30 | + - Adds a GitHub tab to the left-side menu bar where you can browse |
| 31 | + and open notebooks from your GitHub repositories. |
| 32 | + |
| 33 | +All three extensions can be used from within the JupyterLab interface and our |
| 34 | +[Conda |
| 35 | +environment](https://coderefinery.github.io/installation/conda-environment/) |
| 36 | +provides [jupyterlab-git](https://github.com/jupyterlab/jupyterlab-git) and |
| 37 | +[nbdime](http://nbdime.readthedocs.io/). To install additional extensions, |
| 38 | +please consult the [official |
| 39 | +documentation](https://jupyterlab.readthedocs.io/en/stable/user/extensions.html) |
| 40 | +about installing and managing JupyterLab extensions. |
| 41 | + |
| 42 | +--- |
| 43 | + |
| 44 | +## Comparing Jupyter Notebooks on GitHub |
| 45 | + |
| 46 | +For this you really want to enable [Rich Jupyter Notebook |
| 47 | +Diffs](https://github.blog/changelog/2023-03-01-feature-preview-rich-jupyter-notebook-diffs/) |
| 48 | +on GitHub: |
| 49 | +- On GitHub click on your avatar/image (top right). |
| 50 | +- Click on "Feature preview". |
| 51 | +- Enable "Rich Jupyter Notebook Diffs". |
| 52 | + |
| 53 | +:::{exercise} Demonstration |
| 54 | +- We can demonstrate this with a notebook that contains a Matplotlib plot |
| 55 | + (unfortunately the demonstration is less convincing with an Altair plot since the latter |
| 56 | + is generated on the fly and not stored as an image). |
| 57 | + |
| 58 | +- We place the notebook in a GitHub repository and make a small change to it. |
| 59 | + |
| 60 | +- We use https://github.com/**USER**/**REPO**/compare/**VERSION1**..**VERSION2** to |
| 61 | + compare the two versions of the notebook, once with "Rich Jupyter Notebook Diffs" enabled, |
| 62 | + and once without. |
| 63 | +::: |
0 commit comments