Files
concrete/script/nbmake_utils/notebook_sanitize.py
Benoit Chevallier-Mames fe03521dc6 fix: python_format
2021-09-16 12:04:32 +02:00

34 lines
967 B
Python

import argparse
import json
from pathlib import Path
def main():
parser = argparse.ArgumentParser(description="Sanitizer for Jupyter Notebooks")
parser.add_argument("base", type=str, help="directory which contains the notebooks")
parser.add_argument("--check", action="store_true", help="flag to enable just checking mode")
args = parser.parse_args()
base = Path(args.base)
notebooks = base.glob("*.ipynb")
for notebook in notebooks:
with open(notebook, "r") as f:
content = json.load(f)
if args.check:
if len(content["metadata"]) != 0:
print("Notebooks are not sanitized. Please run `make conformance`.")
exit(1)
else:
content["metadata"] = {}
with open(notebook, "w", newline="\n") as f:
json.dump(content, f, indent=1, ensure_ascii=False)
f.write("\n")
if __name__ == "__main__":
main()