nbdime - version control for Jupyter notebooks#

Documentation: https://nbdime.readthedocs.io

Accessing nbdime on the hub#

The nbdiff-web command should be executed with these parameters. You can pick the port number at will, but you need to pick a number so you can set the same value both in the -p and --base-url flags; using 9999 for illustration:

nbdiff-web --ip 0.0.0.0 -p 9999 --base-url ${JUPYTERHUB_SERVICE_PREFIX}proxy/absolute/9999

in the folder where the notebooks under git version control that you want to diff reside.

Warning

The above command has no / between the ${JUPYTERHUB_SERVICE_PREFIX} environment variable and proxy! That variable already ends in /, and if there’s a double / in the command, the proxying will fail.

And then the following URL should be accessed:

https://stat159.datahub.berkeley.edu/user-redirect/proxy/absolute/9999/difftool

When instead accessing a manual diff of two files, that on a local machine would be done with nbdiff-web nb1.ipynb nb2.ipynb, you should similarly execute

nbdiff-web --ip 0.0.0.0 -p 9999 --base-url ${JUPYTERHUB_SERVICE_PREFIX}proxy/absolute/9999 nb1.ipynb nb2.ipynb

but then you need to access the URL:

https://stat159.datahub.berkeley.edu/user-redirect/proxy/absolute/9999/diff?base=nb1.ipynb&remote=nb2.ipynb

(or use instead this if working in the staging hub):

https://stat159-staging.datahub.berkeley.edu/user-redirect/proxy/absolute/9999/diff?base=nb1.ipynb&remote=nb2.ipynb

Code to semi-automate the above#

Let’s first define some little utilities that we’ll use to display cleaner code later:

from IPython.display import Markdown

def mdpre(s):
    return Markdown(f"`{s}`")

def mdcode(code, lang=''):
    tpl = f"""\
```{lang}
{code}
```"""
    return Markdown(tpl)

def mdsh(code):
    return mdcode(code, 'bash')

Now, define the command to run:

port = 9999
nbdiff_web = f"nbdiff-web --ip 0.0.0.0 -p {port} --base-url ${{JUPYTERHUB_SERVICE_PREFIX}}proxy/absolute/{port}"
nbdiff_web
'nbdiff-web --ip 0.0.0.0 -p 9999 --base-url ${JUPYTERHUB_SERVICE_PREFIX}proxy/absolute/9999'

And our base url:

# Production hub
baseurl = f"https://stat159.datahub.berkeley.edu/user-redirect/proxy/absolute/{port}"

# Staging hub
#baseurl = f"https://stat159-staging.datahub.berkeley.edu/user-redirect/proxy/absolute/{port}"

baseurl
'https://stat159.datahub.berkeley.edu/user-redirect/proxy/absolute/9999'

Next, define the location of the file/directory we want to diff:

folder = "$HOME/159/site/lectures/drafts/"

Now, go to the folder with your project with:

and, once there, run the command:

and then visit ().

If using nbdime to diff two local files (instead of the git state), there’s a small change:

file1 = "nb1.ipynb"
file2 = "nb2.ipynb"

In this case, with two explicit files to compare, we can again go to the folder with this content:

and, once there, run the command:

and and then visit .

x = 1

First, , and then !

First, , and then !

x+y
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[7], line 1
----> 1 x+y

NameError: name 'y' is not defined
y = 2
f"{x+y=}"
'x+y=3'