-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgenerate_notebooks.py
More file actions
122 lines (99 loc) · 3.82 KB
/
generate_notebooks.py
File metadata and controls
122 lines (99 loc) · 3.82 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python3
"""
Generate .ipynb files from MyST Markdown notebook files.
This script converts all chapter, lab, exercise, and appendix markdown files
to Jupyter notebook format and creates a downloadable zip archive.
"""
import os
import shutil
import zipfile
from pathlib import Path
import jupytext
def get_notebook_files():
"""Get list of all markdown files that should be converted to notebooks."""
notebook_files = []
# Chapter files
for i in range(1, 22):
file = f"chapter_{i:02d}.md"
if os.path.exists(file):
notebook_files.append(file)
# Lab files
for lab_file in ["chapter_04_lab.md", "chapter_05_lab.md"]:
if os.path.exists(lab_file):
notebook_files.append(lab_file)
# Exercise files
for exercise_file in ["chapter_04_exercises_a.md", "chapter_04_exercises_b.md"]:
if os.path.exists(exercise_file):
notebook_files.append(exercise_file)
# Appendix files
for letter in ['a', 'b', 'c', 'd', 'e']:
file = f"appendix_{letter}.md"
if os.path.exists(file):
notebook_files.append(file)
return notebook_files
def convert_md_to_ipynb(md_file, output_dir):
"""Convert a single markdown file to ipynb format."""
try:
# Read the markdown file
notebook = jupytext.read(md_file)
# Generate output filename
output_file = output_dir / f"{Path(md_file).stem}.ipynb"
# Write the ipynb file
jupytext.write(notebook, output_file, fmt='ipynb')
print(f"✓ Converted {md_file} -> {output_file}")
return output_file
except Exception as e:
print(f"✗ Error converting {md_file}: {e}")
return None
def create_zip_archive(notebook_dir, zip_path):
"""Create a zip archive of all notebook files."""
try:
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
for file in sorted(notebook_dir.glob("*.ipynb")):
zipf.write(file, file.name)
print(f" Added {file.name} to archive")
print(f"✓ Created archive: {zip_path}")
return True
except Exception as e:
print(f"✗ Error creating archive: {e}")
return False
def main():
"""Main function to generate all notebooks and archive."""
import sys
print("=" * 60)
print("Generating Jupyter Notebooks from MyST Markdown")
print("=" * 60)
# Allow specifying output directory (for pre-build vs post-build)
if len(sys.argv) > 1 and sys.argv[1] == '--post-build':
# Generate to _build/html for final output
output_dir = Path("_build/html/notebooks")
zip_path = Path("_build/html/learn_probability_notebooks.zip")
else:
# Generate to project root for MyST validation
output_dir = Path("notebooks")
zip_path = Path("learn_probability_notebooks.zip")
output_dir.mkdir(parents=True, exist_ok=True)
# Get all notebook files
notebook_files = get_notebook_files()
print(f"\nFound {len(notebook_files)} notebook files to convert\n")
# Convert each file
converted_files = []
for md_file in notebook_files:
result = convert_md_to_ipynb(md_file, output_dir)
if result:
converted_files.append(result)
print(f"\n✓ Successfully converted {len(converted_files)} notebooks")
# Create zip archive
if converted_files:
print("\nCreating zip archive...")
create_zip_archive(output_dir, zip_path)
# Calculate total size
total_size = sum(f.stat().st_size for f in converted_files)
zip_size = zip_path.stat().st_size
print(f"\nTotal notebooks size: {total_size / 1024:.1f} KB")
print(f"Archive size: {zip_size / 1024:.1f} KB")
print("\n" + "=" * 60)
print("Done!")
print("=" * 60)
if __name__ == "__main__":
main()