r/IPython • u/gstoel • Oct 02 '15
How to use git with iPython notebooks
I love the interactive and flexible environments of iPython. But I hate it I seem unable to get effective version control in using git. executing a cell already results in a diff :(
Anyone using git with iPython notebooks and if so, how do you use it and is it efficient?
3
u/my_name_is_jody Oct 02 '15
There are several stack overflow answers on the topic. I've also found some good info in other places. I'll try to dig them up later when I'm not on mobile, bit the gist is you can setup a git filter to fix parts of the metadata that shouldn't change from commit to commit. You can also set it up to strip out the run count and output. Finally, for code review sanity, you can get ipython/Jupyter to export the notebook as a script as well. This way, when you look at the different, you can read the python script version, but you can also still access the notebook. I'll see if I can find the links later tonight.
3
u/my_name_is_jody Oct 03 '15
The first two answers here illustrate what I was saying pretty well. http://stackoverflow.com/questions/18734739/using-ipython-notebooks-under-version-control
It should be noted the --script option was removed. You can simulate that same function using the examples here: http://jupyter-notebook.readthedocs.org/en/latest/extending/savehooks.html
Here is what seems to be a fairly recently maintained github project to do this. https://github.com/kynan/nbstripout
If you're on an older version (<3.0) version of ipython/jupyter, this script worked for me as well - https://gist.github.com/pbugnion/ea2797393033b54674af
Good luck. Let us know how it goes.
1
u/parnmatt Oct 03 '15
I clear output on all cells and I think restart the kernel before a commit. That should remove the output and the execution number. Only really useful if you are developing short running scripts.
1
u/bluecheese33 Oct 03 '15
Add a prepush or precommit hook that runs something like this: https://gist.github.com/damianavila/5305869
2
u/gstoel Oct 04 '15
Yeah these are the kind of solutions I read about before.... But it is such a hassle.. And I am a lazy guy so the barrier to adopt version control in ipython notebooks this way is pretty high.... Would be great if the jupyter notebook integrated git version control and did all this clean up in the background... (I know lazy man's thought)