4

I'm using nbdime to diff and merge Jupyter notebooks, and aside from being a tad slow for large notebooks, I love it. However, when I try to do version control from within PyCharm, my git settings for this seem to be ignored and it does a textual diff of the .ipynb files.

My .gitconfig has the following, which I believe were set up when I installed nbdime:

[diff "jupyternotebook"]
        command = git-nbdiffdriver diff
[merge "jupyternotebook"]
        driver = git-nbmergedriver merge %O %A %B %L %P
        name = jupyter notebook merge driver
[difftool "nbdime"]
        cmd = git-nbdifftool diff \"$LOCAL\" \"$REMOTE\"
[difftool]
        prompt = false
[mergetool "nbdime"]
        cmd = git-nbmergetool merge \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\"
[mergetool]
        prompt = false

From the command line, this works great. (Well, I've never had to do a merge, but the diff works great.)

Also, I see that you can specify an external diff tool in PyCharm, but that appears to be a global setting.

Has anyone gotten this to work?

jtlz2
  • 7,700
  • 9
  • 64
  • 114
sfjac
  • 7,119
  • 5
  • 45
  • 69

2 Answers2

7

Updated 2021-11-29

Since this is pretty setup dependant, you may have to adapt the following.

I'm using:

  • macos 12.01
  • miniconda as my base user python (3.9.7)
  • per project i have a mix of virtualenv and conda

To use nbdime to diff my notebooks...

  1. First figure out where nbdime is installed for you. On Mac/Linux: which nbdiff-web
  2. In PyCharm or IDEA, Navigate to Preferences -> Tools -> Diff & Merge -> External Diff Tools
  3. Make sure "Use external diff tool:" is checked
  4. Path to executable: fully qualified path from step 1
    • Mine is /Users/dork/miniconda/bin/nbdiff-web
  5. Parameters: --ignore-details --ignore-metadata --ignore-outputs %1 %2
    • the flags assume you don't want to diff execution count, metadata, and outputs
  6. Click Apply

To use it:

  1. Open a diff of a notebook
  2. Click on icon second to last button on the top (before the ?). Looks like a hammer and wrench. It may be hidden by an expander.
debugme
  • 1,041
  • 1
  • 10
  • 22
  • 1
    Thank you so so much - absolutely amazing - a miracle it works (for DataSpell too of course)! Did you manage to get a merge working too? – jtlz2 Feb 24 '22 at 07:58
  • 1
    Haha. I can't figure out how to launch an external merge tool from within PyCharm/IDEA/DataSpell even though the I've clicked on the "Enable External Merge Tool". FWIW, my path to executable goes to `nbdiff-merge` and my parameters are `--ignore-outputs --ignore-metadata --ignore-id --out %4 %3 %1 %2` Let me know if you figure something out! – debugme Feb 25 '22 at 20:52
  • Ugh, Now this has stopped working for me completely - even the diff - nbdiff-web gives a 422.... – jtlz2 Mar 07 '22 at 07:38
  • @debugme - Try `nbmerge-web` instead of `nbdiff-merge` – Alaa M. Apr 18 '22 at 10:05
3

@debugme answer didn't work for me (under Windows, using Anaconda Python + PyCharm 2018.2). There were three issues:

  • nbdime needs Python, but under Anaconda the Python interpreter isn't available globally (i.e. in the PATH), only when activating the relevant conda environment.
  • Not sure if it was a change in PyCharm or nbdime - but my PyCharm only passes two parameters when diffing, and git-nbdifftool expects 3.
  • The answer explains how to set up nbdime for diff, but I wanted it for merges as well.

So, this worked for me:

  1. Create a batch file pycharm_nb_diff.bat with:

    call %USERPROFILE%\AppData\Local\Continuum\anaconda3\Scripts\activate.bat
    nbdiff-web --ignore-details %*
    exit /b %errorlevel%
    
    • Note I'm using nbdiff-web instead of git-nbdifftool.
    • The --ignore-details switch ignores some differences when comparing cells (mainly the execution_count, which is the small number in square brackets next to the cell). You can remove the switch for a more comprehensive diff.
    • Calling activate.bat runs the tool within the conda env (the location I'm using is the default under Windows).
    • BTW, conda 4.6.0 re-introduced conda-run, which is preferred over activate.bat, but it's currently only supported under Linux.
  2. Now set PyCharm to use pycharm_nb_diff.bat with the params %1 %2.

  3. In the same manner, create pycharm_nb_merge.bat with nbmerge-web instead of nbdiff-web, and set PyCharm to use it as a merge tool. Note that the required params for merge are different: --out %4 %3 %1 %2

    • thanks to the exit /b %errorlevel% line in the script, you can check the box Trust process exit code in PyCharm and merges work as expected.
OmerB
  • 4,134
  • 3
  • 20
  • 33