1+ # ...existing code...
2+ import os
3+ import json
4+ import uuid
5+
6+ def regenerate_cell_ids (directory ):
7+ # os.walk는 지정된 디렉토리부터 시작해서 모든 하위 폴더를 재귀적으로 순회합니다.
8+ print (f"📂 Searching for notebooks recursively in: { directory } " )
9+
10+ processed_files = 0
11+
12+ for root , dirs , files in os .walk (directory ):
13+ # 불필요한 폴더 건너뛰기 (_build, git, venv 등)
14+ if any (x in root for x in ["_build" , ".ipynb_checkpoints" , ".git" , ".venv" , "env" ]):
15+ continue
16+
17+ for file in files :
18+ if file .endswith (".ipynb" ):
19+ file_path = os .path .join (root , file )
20+ try :
21+ with open (file_path , 'r' , encoding = 'utf-8' ) as f :
22+ nb = json .load (f )
23+
24+ if 'cells' not in nb :
25+ continue
26+
27+ # 모든 셀에 새로운 고유 ID 할당 및 metadata 정리
28+ for cell in nb ['cells' ]:
29+ # 1. 표준 'id' 필드 갱신
30+ cell ['id' ] = str (uuid .uuid4 ())
31+
32+ # 2. metadata 내의 'id' 필드 제거 (중복의 원인)
33+ if 'metadata' in cell and 'id' in cell ['metadata' ]:
34+ del cell ['metadata' ]['id' ]
35+
36+ with open (file_path , 'w' , encoding = 'utf-8' ) as f :
37+ json .dump (nb , f , indent = 1 , ensure_ascii = False )
38+
39+ print (f" ✓ Fixed: { file_path } " )
40+ processed_files += 1
41+
42+ except Exception as e :
43+ print (f" ✗ Error: { file_path } - { str (e )} " )
44+
45+ print (f"\n ✨ 완료! 총 { processed_files } 개의 노트북 파일이 수정되었습니다." )
46+
47+ if __name__ == "__main__" :
48+ # 현재 폴더(.)를 시작점으로 지정하면 모든 하위 폴더를 탐색합니다.
49+ regenerate_cell_ids ("." )
0 commit comments