|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Script to update all Jupyter notebook kernels to use 'geosci-labs' |
| 4 | +This script will: |
| 5 | +1. Find all .ipynb files in the current directory and subdirectories |
| 6 | +2. Update their kernel metadata to use the 'geosci-labs' kernel |
| 7 | +3. Preserve all other metadata and content |
| 8 | +""" |
| 9 | + |
| 10 | +import os |
| 11 | +import json |
| 12 | +import glob |
| 13 | + |
| 14 | +def update_notebook_kernel(notebook_path, target_kernel_name="geosci-labs"): |
| 15 | + """ |
| 16 | + Update a single notebook's kernel metadata |
| 17 | + |
| 18 | + Args: |
| 19 | + notebook_path (str): Path to the notebook file |
| 20 | + target_kernel_name (str): Name of the target kernel |
| 21 | + |
| 22 | + Returns: |
| 23 | + bool: True if update was successful, False otherwise |
| 24 | + """ |
| 25 | + try: |
| 26 | + # Read the notebook |
| 27 | + with open(notebook_path, 'r', encoding='utf-8') as f: |
| 28 | + notebook_data = json.load(f) |
| 29 | + |
| 30 | + # Get current kernel info for logging |
| 31 | + current_kernel = "unknown" |
| 32 | + if 'metadata' in notebook_data and 'kernelspec' in notebook_data['metadata']: |
| 33 | + current_kernel = notebook_data['metadata']['kernelspec'].get('name', 'unknown') |
| 34 | + |
| 35 | + # Skip if already using target kernel |
| 36 | + if current_kernel == target_kernel_name: |
| 37 | + print(f"✓ {notebook_path} - Already using {target_kernel_name}") |
| 38 | + return True |
| 39 | + |
| 40 | + # Update kernel metadata |
| 41 | + if 'metadata' not in notebook_data: |
| 42 | + notebook_data['metadata'] = {} |
| 43 | + |
| 44 | + notebook_data['metadata']['kernelspec'] = { |
| 45 | + "display_name": target_kernel_name, |
| 46 | + "language": "python", |
| 47 | + "name": target_kernel_name |
| 48 | + } |
| 49 | + |
| 50 | + # Also update language_info if it exists to be consistent |
| 51 | + if 'language_info' in notebook_data['metadata']: |
| 52 | + notebook_data['metadata']['language_info']['name'] = 'python' |
| 53 | + |
| 54 | + # Write back the updated notebook |
| 55 | + with open(notebook_path, 'w', encoding='utf-8') as f: |
| 56 | + json.dump(notebook_data, f, indent=1, ensure_ascii=False) |
| 57 | + |
| 58 | + print(f"✓ {notebook_path} - Updated from '{current_kernel}' to '{target_kernel_name}'") |
| 59 | + return True |
| 60 | + |
| 61 | + except Exception as e: |
| 62 | + print(f"✗ {notebook_path} - Error: {e}") |
| 63 | + return False |
| 64 | + |
| 65 | +def find_all_notebooks(root_dir="."): |
| 66 | + """ |
| 67 | + Find all .ipynb files recursively in the given directory |
| 68 | + |
| 69 | + Args: |
| 70 | + root_dir (str): Root directory to search from |
| 71 | + |
| 72 | + Returns: |
| 73 | + list: List of notebook file paths |
| 74 | + """ |
| 75 | + notebook_pattern = os.path.join(root_dir, "**", "*.ipynb") |
| 76 | + notebooks = glob.glob(notebook_pattern, recursive=True) |
| 77 | + |
| 78 | + # Filter out checkpoint files |
| 79 | + notebooks = [nb for nb in notebooks if '.ipynb_checkpoints' not in nb] |
| 80 | + |
| 81 | + return sorted(notebooks) |
| 82 | + |
| 83 | +def main(): |
| 84 | + """Main function to update all notebooks""" |
| 85 | + print("🔍 Searching for Jupyter notebooks...") |
| 86 | + |
| 87 | + # Get the current working directory |
| 88 | + root_dir = os.getcwd() |
| 89 | + print(f"📁 Root directory: {root_dir}") |
| 90 | + |
| 91 | + # Find all notebooks |
| 92 | + notebooks = find_all_notebooks(root_dir) |
| 93 | + |
| 94 | + if not notebooks: |
| 95 | + print("❌ No notebooks found!") |
| 96 | + return |
| 97 | + |
| 98 | + print(f"📓 Found {len(notebooks)} notebooks") |
| 99 | + print("\n" + "="*60) |
| 100 | + |
| 101 | + # Update each notebook |
| 102 | + success_count = 0 |
| 103 | + error_count = 0 |
| 104 | + |
| 105 | + for notebook_path in notebooks: |
| 106 | + # Make path relative for cleaner output |
| 107 | + rel_path = os.path.relpath(notebook_path, root_dir) |
| 108 | + |
| 109 | + if update_notebook_kernel(rel_path): |
| 110 | + success_count += 1 |
| 111 | + else: |
| 112 | + error_count += 1 |
| 113 | + |
| 114 | + # Summary |
| 115 | + print("\n" + "="*60) |
| 116 | + print(f"✅ Successfully updated: {success_count}") |
| 117 | + print(f"❌ Failed to update: {error_count}") |
| 118 | + print(f"📊 Total processed: {len(notebooks)}") |
| 119 | + |
| 120 | + if error_count == 0: |
| 121 | + print("\n🎉 All notebooks have been successfully updated to use 'geosci-labs' kernel!") |
| 122 | + else: |
| 123 | + print(f"\n⚠️ {error_count} notebooks had errors. Please check the output above.") |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + main() |
0 commit comments