|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Generate .ipynb files from MyST Markdown notebook files. |
| 4 | +This script converts all chapter, lab, exercise, and appendix markdown files |
| 5 | +to Jupyter notebook format and creates a downloadable zip archive. |
| 6 | +""" |
| 7 | + |
| 8 | +import os |
| 9 | +import shutil |
| 10 | +import zipfile |
| 11 | +from pathlib import Path |
| 12 | +import jupytext |
| 13 | + |
| 14 | +def get_notebook_files(): |
| 15 | + """Get list of all markdown files that should be converted to notebooks.""" |
| 16 | + notebook_files = [] |
| 17 | + |
| 18 | + # Chapter files |
| 19 | + for i in range(1, 22): |
| 20 | + file = f"chapter_{i:02d}.md" |
| 21 | + if os.path.exists(file): |
| 22 | + notebook_files.append(file) |
| 23 | + |
| 24 | + # Lab files |
| 25 | + for lab_file in ["chapter_04_lab.md", "chapter_05_lab.md"]: |
| 26 | + if os.path.exists(lab_file): |
| 27 | + notebook_files.append(lab_file) |
| 28 | + |
| 29 | + # Exercise files |
| 30 | + for exercise_file in ["chapter_04_exercises_a.md", "chapter_04_exercises_b.md"]: |
| 31 | + if os.path.exists(exercise_file): |
| 32 | + notebook_files.append(exercise_file) |
| 33 | + |
| 34 | + # Appendix files |
| 35 | + for letter in ['a', 'b', 'c', 'd', 'e']: |
| 36 | + file = f"appendix_{letter}.md" |
| 37 | + if os.path.exists(file): |
| 38 | + notebook_files.append(file) |
| 39 | + |
| 40 | + return notebook_files |
| 41 | + |
| 42 | +def convert_md_to_ipynb(md_file, output_dir): |
| 43 | + """Convert a single markdown file to ipynb format.""" |
| 44 | + try: |
| 45 | + # Read the markdown file |
| 46 | + notebook = jupytext.read(md_file) |
| 47 | + |
| 48 | + # Generate output filename |
| 49 | + output_file = output_dir / f"{Path(md_file).stem}.ipynb" |
| 50 | + |
| 51 | + # Write the ipynb file |
| 52 | + jupytext.write(notebook, output_file, fmt='ipynb') |
| 53 | + |
| 54 | + print(f"✓ Converted {md_file} -> {output_file}") |
| 55 | + return output_file |
| 56 | + except Exception as e: |
| 57 | + print(f"✗ Error converting {md_file}: {e}") |
| 58 | + return None |
| 59 | + |
| 60 | +def create_zip_archive(notebook_dir, zip_path): |
| 61 | + """Create a zip archive of all notebook files.""" |
| 62 | + try: |
| 63 | + with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf: |
| 64 | + for file in sorted(notebook_dir.glob("*.ipynb")): |
| 65 | + zipf.write(file, file.name) |
| 66 | + print(f" Added {file.name} to archive") |
| 67 | + print(f"✓ Created archive: {zip_path}") |
| 68 | + return True |
| 69 | + except Exception as e: |
| 70 | + print(f"✗ Error creating archive: {e}") |
| 71 | + return False |
| 72 | + |
| 73 | +def main(): |
| 74 | + """Main function to generate all notebooks and archive.""" |
| 75 | + print("=" * 60) |
| 76 | + print("Generating Jupyter Notebooks from MyST Markdown") |
| 77 | + print("=" * 60) |
| 78 | + |
| 79 | + # Create output directory |
| 80 | + output_dir = Path("_build/html/notebooks") |
| 81 | + output_dir.mkdir(parents=True, exist_ok=True) |
| 82 | + |
| 83 | + # Get all notebook files |
| 84 | + notebook_files = get_notebook_files() |
| 85 | + print(f"\nFound {len(notebook_files)} notebook files to convert\n") |
| 86 | + |
| 87 | + # Convert each file |
| 88 | + converted_files = [] |
| 89 | + for md_file in notebook_files: |
| 90 | + result = convert_md_to_ipynb(md_file, output_dir) |
| 91 | + if result: |
| 92 | + converted_files.append(result) |
| 93 | + |
| 94 | + print(f"\n✓ Successfully converted {len(converted_files)} notebooks") |
| 95 | + |
| 96 | + # Create zip archive |
| 97 | + if converted_files: |
| 98 | + print("\nCreating zip archive...") |
| 99 | + zip_path = Path("_build/html/learn_probability_notebooks.zip") |
| 100 | + create_zip_archive(output_dir, zip_path) |
| 101 | + |
| 102 | + # Calculate total size |
| 103 | + total_size = sum(f.stat().st_size for f in converted_files) |
| 104 | + zip_size = zip_path.stat().st_size |
| 105 | + print(f"\nTotal notebooks size: {total_size / 1024:.1f} KB") |
| 106 | + print(f"Archive size: {zip_size / 1024:.1f} KB") |
| 107 | + |
| 108 | + print("\n" + "=" * 60) |
| 109 | + print("Done!") |
| 110 | + print("=" * 60) |
| 111 | + |
| 112 | +if __name__ == "__main__": |
| 113 | + main() |
0 commit comments