-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathclean_notebooks.py
More file actions
41 lines (37 loc) · 1.27 KB
/
Copy pathclean_notebooks.py
File metadata and controls
41 lines (37 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
python3 << 'PYEOF'
script = """#!/usr/bin/env python3
import json, subprocess, sys
def get_staged_notebooks():
result = subprocess.run(["git", "diff", "--cached", "--name-only", "--diff-filter=ACM"], capture_output=True, text=True)
return [f for f in result.stdout.splitlines() if f.endswith(".ipynb")]
def clean_notebook(path):
try:
with open(path, "r", encoding="utf-8") as f:
nb = json.load(f)
except json.JSONDecodeError as e:
print(f" WARNING JSON invalido, se omite: {path} ({e})")
return
if "widgets" in nb.get("metadata", {}):
del nb["metadata"]["widgets"]
with open(path, "w", encoding="utf-8") as f:
json.dump(nb, f, indent=1, ensure_ascii=False)
f.write("\\n")
subprocess.run(["git", "add", path])
print(f" OK metadata.widgets eliminado: {path}")
else:
print(f" OK Sin cambios necesarios: {path}")
def main():
notebooks = get_staged_notebooks()
if not notebooks:
sys.exit(0)
print("Limpiando notebooks staged...")
for nb_path in notebooks:
clean_notebook(nb_path)
print("Listo.")
if __name__ == "__main__":
main()
"""
with open("clean_notebooks.py", "w") as f:
f.write(script)
print("Archivo actualizado.")
PYEOF