Skip to content

Instantly share code, notes, and snippets.

@Andreal2000
Created December 13, 2023 16:58
Show Gist options
  • Save Andreal2000/ae9399f61a5a4776576963e89caedf39 to your computer and use it in GitHub Desktop.
Save Andreal2000/ae9399f61a5a4776576963e89caedf39 to your computer and use it in GitHub Desktop.
Jupyter Notebook output cleaner Git Hooks
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
tmp_extension = "tmp"
current_dir = os.getcwd()
list_of_files = []
for (dirpath, dirnames, filenames) in os.walk(current_dir):
list_of_files += [os.path.join(dirpath, file) for file in filenames]
list_of_ipynb_files = [file for file in list_of_files if file.endswith(f".ipynb.{tmp_extension}")]
for file in list_of_ipynb_files:
print(f"Restored notebook: {file}")
os.replace(file, file.replace(f".{tmp_extension}", ""))
print("All notebooks have been restored to their original names.")
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import json
tmp_extension = "tmp"
remove_metadata_fields = ["collapsed", "scrolled"]
current_dir = os.getcwd()
list_of_files = []
for (dirpath, dirnames, filenames) in os.walk(current_dir):
list_of_files += [os.path.join(dirpath, file) for file in filenames]
list_of_ipynb_files = [file for file in list_of_files if file.endswith(".ipynb")]
for file in list_of_ipynb_files:
ipynb_file = json.loads(open(file, "r").read())
for cell in ipynb_file["cells"]:
if cell["cell_type"] == "code":
cell["outputs"] = []
cell["execution_count"] = None
# Remove metadata associated with output
if "metadata" in cell:
for field in remove_metadata_fields:
cell["metadata"].pop(field, None)
print(f"Cleared notebook: {file}")
os.rename(file, f"{file}.{tmp_extension}")
json.dump(ipynb_file, open(file, "w"))
print("All notebooks have been cleared of their outputs.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment