Safe read/write and validation for PHYS130B Jupyter notebooks under notes_src. Use when editing, fixing, or restructuring any .ipynb.
Programmatic editing of .ipynb only—never NotebookEdit or Edit/Write on notebooks in Cowork.
.claude/README.md (feedback → progress → work).notes_src/**/*.ipynb (content, structure, or metadata).feedback.md / progress.md that require JSON cell edits.science-reviewer or lecture-content when content changes../build.shjupyter-book buildrules/validation.mdrules/troubleshooting-ipynb.md, re-validate, then continue.NotebookEdit on .ipynb.Edit/Write on .ipynb in Cowork—use Bash + json.load / json.dump.json.dump(nb, f, indent=1, ensure_ascii=False) always.text_to_source for markdown strings—canonical implementation: skills/notebook-writer/scripts/safe_edit.py (same logic as rules/notebook-editing.md).import json
def text_to_source(text):
lines = text.split('\n')
source = []
for i, line in enumerate(lines):
if i < len(lines) - 1:
source.append(line + '\n')
else:
if line:
source.append(line)
return source
with open(path) as f:
nb = json.load(f)
# inspect len(nb['cells']), then:
nb['cells'][CELL_INDEX]['source'] = text_to_source(new_content)
with open(path, 'w') as f:
json.dump(nb, f, indent=1, ensure_ascii=False)
python3 .claude/skills/notebook-writer/scripts/safe_edit.py validate path/to.ipynb
For broader checks (structure, MyST, notation heuristics):
python3 .claude/scripts/validate_project.py --scope <chapter-or-file-stem>
Full list of checks: see former validate playbook—corruption, cell counts/titles, $$ blank lines, banned patterns (---, nested :::, plt.show(), etc.), notation hints in math (script docstring).
Report results in a small table if many files: | Notebook | Corruption | Structure | Other |.
json.load the notebook; locate cell/line from the report.\n / collapsed line / content violation of rules/ (architecture, MyST, style).json.dump; validate; update progress.md or annotate feedback.md when resolved.CLAUDE.md, .claude/README.md, rules/notebook-editing.md, rules/tooling-security.md, rules/validation.md, rules/troubleshooting-ipynb.md_refs/ (project root): Professor's original lecture notes. Read the corresponding _refs/ file before writing or rewriting any derivation or physics content. See CLAUDE.md § "Reference materials" for the chapter-to-file mapping.