Git Tracking for Notebooks

Hi Everyone -

I woke up yesterday annoyed with the lack of git in my python-notebook-life so I built this…

I’d love it if anyone wanted to play with and give feed back.

The readme probably explains it better than I can here. Let me know if you have questions.

Its pretty simple and not 100% complete but it does everything you’d need it too. The only missing feature is a converter from the python files back to notebooks, but you don’t need that really and it is to come.

I should say… I know others have built things, probably pretty similar but I liked the simplicity of this setup.

Cheers!
.brookie

Thanks! Can you explain what happens after the .py files are added to git? Are they automatically processed back into .ipynb files somehow?

Hi Jeremy!

Thats a great question, and in fact was the “one missing feature” I mentioned above. But its no longer missing :slight_smile:

SHORT ANSWER:

# using default destination path
nb_git tonb -s soure_file.nbpy.py

# specifiy destination path
nb_git tonb -s soure_file.nbpy.py -d output_file.nbpy.ipynb

There’s a longer answer that has more to do with my suggested workflow, but I need to run. I’ll update this thread and the readme once I’ve thought about it some more.

SIDE NOTE: I’ve added the above package to pip and in the process moved from nbgit to nb_git.

Thanks.
Brookie

1 Like

Hi Everyone - In thinking through the above promised “longer answer” for Jeremy I decided to rework the whole thing. I am really excited about how it worked out.

There is a configuration in which a simple commit

gitnb commit -am "a commit message"

will automatically track every notebook in your repo. This is similar to my original setup (but works better).

However the default is much more “git” like, where you choose what notebooks to track and can review and diff files before committing your changes.

An example workflow might be:

# add a notebook to be tracked
gitnb add SomeNotebook.ipynb

# check the changes before commiting
gitnb diff SomeNotebook.ipynb

# commit your changes
gitnb commit -am "I fixed a bug"

The README has a fairly complete set of docs with examples. Please let me know if you have any questions, if you hate it , if you love it, if you want to contribute, etc…

1 Like

Awesome work.
How hard would it be to have something like git gutter (https://github.com/jisaacks/GitGutter) inside a jupyter notebook now that you have done this?

Thats a cool little plugin. I hadn’t seen it before.

Sadly I don’t think gitnb would help you get that into a python-notebook. For gitnb diff I first generate a temporary “nbpy.py” version of the notebook, then diff that ( using the difflib module ) with the saved ( ie gitnb update-ed ) version of the file.

I could imagine a similar process that then injected the diff markers into the ipynb file, but (a) thats probably a terrible idea, and (b) to do something like GitGutter you would probably really want to inject into the html. With that in mind would probably need to be something done deep inside of the core of jupyter’s platform.

So yeah - a command line gitnb diff is probably your best bet for now, but of course I could be missing something.