"""
Part 2: 运行完整的 notebook cells 1-35
解决中文路径编码问题
"""
import warnings, time, os, sys, json, traceback
warnings.filterwarnings('ignore')
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as _p
_p.show = lambda *a, **kw: None

nb = r'D:\Code\doing_exercises\programs\外教作业外快\强化学习个人课程作业报告\notebooks\insurance_premium_risk.ipynb'
cells = json.load(open(nb, encoding='utf-8'))['cells']
code_cells = [c for c in cells if c['cell_type'] == 'code']
print(f"Total code cells: {len(code_cells)}")

main_ns = globals().copy()
main_ns['RANDOM_STATE'] = 42

for i, cell in enumerate(code_cells, start=1):
    src = ''.join(cell['source'])
    print(f"\n{'='*60}")
    print(f"Running cell {i}/{len(code_cells)}...")
    try:
        exec(compile(src, f'cell_{i}', 'exec'), main_ns)
    except Exception as e:
        print(f"ERROR cell {i}: {e}")
        traceback.print_exc()
        print("Stopping.")
        break

print("\n\nAll cells executed!")
