Last active
April 29, 2022 11:15
-
-
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 contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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