Skip to content

Commit 51d43cd

Browse files
committed
Fix download paths and add frontmatter downloads configuration
Fixes 404 errors for notebook downloads by: - Correcting download paths in downloads.md to use relative paths (./notebooks/, ./learn_probability_notebooks.zip) - Adding downloads configuration to frontmatter of all 30 notebook files - Adding missing frontmatter to exercise and appendix files that didn't have it This enables MyST's built-in download buttons to work correctly alongside the custom downloads page. Each chapter now has a download button in the UI that points to the correct .ipynb file location.
1 parent 93c7d68 commit 51d43cd

32 files changed

Lines changed: 217 additions & 30 deletions

add_download_frontmatter.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Add downloads configuration to frontmatter of all notebook markdown files.
4+
"""
5+
6+
import re
7+
from pathlib import Path
8+
9+
def add_downloads_to_frontmatter(file_path):
10+
"""Add downloads configuration to a markdown file's frontmatter."""
11+
with open(file_path, 'r') as f:
12+
content = f.read()
13+
14+
# Extract the frontmatter
15+
frontmatter_match = re.match(r'^---\n(.*?)\n---\n(.*)$', content, re.DOTALL)
16+
if not frontmatter_match:
17+
print(f"⚠️ No frontmatter found in {file_path}")
18+
return False
19+
20+
frontmatter = frontmatter_match.group(1)
21+
body = frontmatter_match.group(2)
22+
23+
# Check if downloads already exists
24+
if 'downloads:' in frontmatter:
25+
print(f"ℹ️ Downloads already configured in {file_path}")
26+
return False
27+
28+
# Generate the ipynb filename
29+
stem = file_path.stem
30+
ipynb_file = f"notebooks/{stem}.ipynb"
31+
32+
# Add downloads configuration
33+
downloads_config = f"\ndownloads:\n - file: {ipynb_file}\n"
34+
new_frontmatter = frontmatter + downloads_config
35+
36+
# Write back
37+
new_content = f"---\n{new_frontmatter}---\n{body}"
38+
with open(file_path, 'w') as f:
39+
f.write(new_content)
40+
41+
print(f"✓ Added downloads to {file_path}")
42+
return True
43+
44+
def main():
45+
"""Add downloads configuration to all notebook files."""
46+
print("=" * 60)
47+
print("Adding Downloads Configuration to Frontmatter")
48+
print("=" * 60)
49+
50+
# Get all markdown notebook files
51+
files = []
52+
53+
# Chapter files
54+
for i in range(1, 22):
55+
file = Path(f"chapter_{i:02d}.md")
56+
if file.exists():
57+
files.append(file)
58+
59+
# Lab files
60+
for lab_file in ["chapter_04_lab.md", "chapter_05_lab.md"]:
61+
file = Path(lab_file)
62+
if file.exists():
63+
files.append(file)
64+
65+
# Exercise files
66+
for exercise_file in ["chapter_04_exercises_a.md", "chapter_04_exercises_b.md"]:
67+
file = Path(exercise_file)
68+
if file.exists():
69+
files.append(file)
70+
71+
# Appendix files
72+
for letter in ['a', 'b', 'c', 'd', 'e']:
73+
file = Path(f"appendix_{letter}.md")
74+
if file.exists():
75+
files.append(file)
76+
77+
print(f"\nProcessing {len(files)} files...\n")
78+
79+
updated = 0
80+
for file in files:
81+
if add_downloads_to_frontmatter(file):
82+
updated += 1
83+
84+
print(f"\n✓ Updated {updated} files")
85+
print("=" * 60)
86+
87+
if __name__ == "__main__":
88+
main()

appendix_a.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ kernelspec:
99
display_name: Python 3 (ipykernel)
1010
language: python
1111
name: python3
12+
downloads:
13+
- file: notebooks/appendix_a.ipynb
1214
---
1315

1416
# Appendix A: Python/Jupyter Setup Deep Dive

appendix_b.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ kernelspec:
99
display_name: Python 3 (ipykernel)
1010
language: python
1111
name: python3
12+
downloads:
13+
- file: notebooks/appendix_b.ipynb
1214
---
1315

1416
# Appendix B: Essential Library Reference

appendix_c.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ kernelspec:
99
display_name: Python 3 (ipykernel)
1010
language: python
1111
name: python3
12+
downloads:
13+
- file: notebooks/appendix_c.ipynb
1214
---
1315

1416
# Appendix C: Mathematical Notation Summary

appendix_d.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ kernelspec:
99
display_name: Python 3 (ipykernel)
1010
language: python
1111
name: python3
12+
downloads:
13+
- file: notebooks/appendix_d.ipynb
1214
---
1315

1416
# Appendix D: Further Reading and Resources

appendix_e.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
---
2+
jupytext:
3+
text_representation:
4+
extension: .md
5+
format_name: myst
6+
format_version: 0.13
7+
jupytext_version: 1.18.1
8+
kernelspec:
9+
display_name: Python 3 (ipykernel)
10+
language: python
11+
name: python3
12+
downloads:
13+
- file: notebooks/appendix_e.ipynb
14+
---
15+
116
# Appendix E: Summary of Formulas
217

318
This appendix provides a summary of the key formulas introduced in Chapters 1–8.

chapter_01.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ kernelspec:
99
display_name: Python 3 (ipykernel)
1010
language: python
1111
name: python3
12+
downloads:
13+
- file: notebooks/chapter_01.ipynb
1214
---
1315

1416
# Chapter 1: Introduction to Probability and Python Setup

chapter_02.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ kernelspec:
99
display_name: Python 3 (ipykernel)
1010
language: python
1111
name: python3
12+
downloads:
13+
- file: notebooks/chapter_02.ipynb
1214
---
1315

1416
# Chapter 2: The Language of Probability: Sets, Sample Spaces, and Events

chapter_03.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ kernelspec:
99
display_name: Python 3 (ipykernel)
1010
language: python
1111
name: python3
12+
downloads:
13+
- file: notebooks/chapter_03.ipynb
1214
---
1315

1416
# Chapter 3: Counting Techniques: Permutations and Combinations

chapter_04.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ kernelspec:
99
display_name: Python 3 (ipykernel)
1010
language: python
1111
name: python3
12+
downloads:
13+
- file: notebooks/chapter_04.ipynb
1214
---
1315

1416
# Chapter 4: Conditional Probability

0 commit comments

Comments
 (0)