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

36 lines
1.0 KiB
Python

"""Sanitizer for Jupyter notebooks."""
import argparse
import json
from pathlib import Path
def main():
"""Sanitize"""
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", encoding="utf-8") as f:
content = json.load(f)
if args.check:
if len(content["metadata"]) != 0:
print("Notebooks are not sanitized. Please run `make conformance`.")
raise ValueError
else:
content["metadata"] = {}
with open(notebook, "w", newline="\n", encoding="utf-8") as f:
json.dump(content, f, indent=1, ensure_ascii=False)
f.write("\n")
if __name__ == "__main__":
main()