-
Notifications
You must be signed in to change notification settings - Fork 747
Expand file tree
/
Copy pathremove_notebook_headers.py
More file actions
31 lines (25 loc) · 1.09 KB
/
remove_notebook_headers.py
File metadata and controls
31 lines (25 loc) · 1.09 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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.
import json
import sys
def remove_kernelspec_from_ipynb_files(file_path: str):
modified_files = []
if file_path.endswith(".ipynb"):
# Iterate through all .ipynb files in the specified file
with open(file_path, encoding="utf-8") as f:
content = json.load(f)
# Remove the "kernelspec" metadata section if it exists
if "metadata" in content and "kernelspec" in content["metadata"]:
modified_files.append(file_path)
del content["metadata"]["kernelspec"]
# Save the modified content back to the .ipynb file
with open(file_path, "w", encoding="utf-8") as f:
json.dump(content, f, indent=1)
if modified_files:
print("Modified files:", modified_files)
sys.exit(1)
if __name__ == "__main__":
files = sys.argv[1:]
# # Remove the "kernelspec" section from all .ipynb files in the specified file
for file in files:
remove_kernelspec_from_ipynb_files(file)