Skip to content

Instantly share code, notes, and snippets.

@binaryfunt
Last active April 29, 2022 11:15
Show Gist options
  • Save binaryfunt/f31a7ecc8d698dd0edbec1489f2ab055 to your computer and use it in GitHub Desktop.
Save binaryfunt/f31a7ecc8d698dd0edbec1489f2ab055 to your computer and use it in GitHub Desktop.
Jupyter notebooks - clear output pre-save hook (for cleaner git diffs)
# This file has to be saved in your Jupyter config directory (found by running
# jupyter --config-dir
# but usually C:\Users\<username>\.jupyter\jupyter_notebook_config.py on Windows
def scrub_output_pre_save(model, **kwargs):
"""scrub output before saving notebooks"""
# only run on notebooks
if model['type'] != 'notebook':
return
# only run on nbformat v4
if model['content']['nbformat'] != 4:
return
for cell in model['content']['cells']:
if cell['cell_type'] != 'code':
continue
cell['outputs'] = []
cell['execution_count'] = None
if 'collapsed' in cell['metadata']:
cell['metadata'].pop('collapsed', 0)
c.FileContentsManager.pre_save_hook = scrub_output_pre_save
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment