Remove Jupyter Notebook Output from Terminal and when using Git06 Nov 2021
Often times you want to delete the output of a jupyter notebook before commiting it to a repository, but in most cases you want to still have the notebook output for yourself. In this short guide you will seeh how to delete the notebook output automatically when committing notebooks to a repository while keeping the outputs local.
Removing the Notebook Output in the Command-line
The first tool to do this job is the nbconvert command-line tool to work with jupyter notebooks. First, check your installed version of
nbconvert by typing:
jupyter nbconvert --version
In order to delete the output, you can type the following command:
jupyter nbconvert \ --clear-output \ --to notebook \ --output=new_notebook \ notebook.ipynb
To remove the output inplace, you can type:
jupyter nbconvert \ --clear-output \ --inplace \ notebook.ipynb
If you have
nbconvert below version
6.0, change the command to:
jupyter nbconvert \ --ClearOutputPreprocessor.enabled=True \ --to notebook \ --output=new_notebook \ notebook.ipynb
Removing the Notebook Output automatically when Committing
Register a new filter by appending the following lines to the
.git/config in your chosen repository:
[filter "remove-notebook-output"] clean = "jupyter nbconvert --clear-output --to=notebook --stdin --stdout --log-level=ERROR"
If you want the filter to be available globally, append it to
~/.gitconfig instead. Also, remember to check the
nbconvert version and change the command as shown previously.
Now, append the following lines to the .gitattributes file:
If you want to apply those filters only to a specific folder you can instead append:
That’s all! Now you should be able to commit jupyter notebooks to git repositories without output if you followed all the steps.
For more resources, have a look at: