How to call nbdime's diff options in python

39 views
Skip to first unread message

Adam Rule

unread,
Jul 10, 2017, 1:52:26 PM7/10/17
to Project Jupyter
I noticed there are console commands for controlling which part of the notebook to diff using nbdime (source, output, metadata, attachments). How do I set these options when calling nbdime.diff() from python rather than the command line? I've looked through the documentation and source code and it seems to be related to the `differs` parameter in the diff function, but I have not (yet) figured out how to set it properly.

Vidar Tonaas Fauske

unread,
Jul 11, 2017, 6:06:49 AM7/11/17
to jup...@googlegroups.com
The function you want is
`nbdime.diffing.notebooks.set_notebook_diff_targets`. It is not very
well exposed because its current implementation is a bit hackish: It
currently sets what to diff in a global (i.e. for the running
session). As such, it is a good candidate for a refactor, but for now
you should probably wrap it in a context manager to ensure it is reset
after calling it (snippet included below for convenience).


from contextlib import contextmanager
from nbdime.diffing.notebooks import set_notebook_diff_targets
@contextmanager
def diff_targets(sources=True, outputs=True, metadata=True):
set_notebook_diff_targets(sources=sources, outputs=outputs,
metadata=metadata)
try:
yield
finally:
set_notebook_diff_targets() # default options will reset

def only_diff_targets(sources=False, outputs=False, metadata=False):
return diff_targets(sources, outputs, metadata)

# Usage:

import nbdime
import nbformat
afn = 'local file.ipynb'
bfn = 'remote file.ipynb'
a = nbformat.read(afn, as_version=4)
b = nbformat.read(bfn, as_version=4)
with only_diff_targets(outputs=True):
diff = nbdime.diff_notebooks(a, b)
nbdime.prettyprint.pretty_print_notebook_diff(afn, bfn, a, diff)
> --
> You received this message because you are subscribed to the Google Groups
> "Project Jupyter" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to jupyter+u...@googlegroups.com.
> To post to this group, send email to jup...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jupyter/b7366123-7db7-4635-81c2-f27c19df0bca%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Adam Rule

unread,
Jul 11, 2017, 11:28:19 AM7/11/17
to Project Jupyter
Perfect! 

I had found `nbdime.diffing.notebooks.set_notebook_diff_targets` and noticed the global setting, but missed that I could use a context manager. 
Reply all
Reply to author
Forward
0 new messages