{title}
Migrate from proprietary APIs to open-source alternatives
#!/usr/bin/env python3 """ Script to convert Jupyter notebooks to recipe pages using nbgradio. This script processes notebooks in the notebooks/ directory and generates recipe pages in the recipes/ directory with proper styling integration. """ import os import subprocess import sys from pathlib import Path import shutil import json def install_nbgradio(): """Install nbgradio if not already installed.""" try: import nbgradio print("ā nbgradio is already installed") except ImportError: print("š¦ Installing nbgradio...") subprocess.run([sys.executable, "-m", "pip", "install", "nbgradio"], check=True) print("ā nbgradio installed successfully") def get_notebook_files(): """Get all notebook files from the notebooks directory.""" notebooks_dir = Path("notebooks") if not notebooks_dir.exists(): print("ā notebooks/ directory not found") return [] notebook_files = list(notebooks_dir.glob("*.ipynb")) print(f"š Found {len(notebook_file)} notebook(s): {[f.name for f in notebook_files]}") return notebook_files def get_existing_recipes(): """Get list of existing recipe files.""" recipes_dir = Path("recipes") if not recipes_dir.exists(): return [] recipe_files = list(recipes_dir.glob("*.html")) return [f.stem for f in recipe_files] def convert_notebook_to_recipe(notebook_path): """Convert a single notebook to a recipe page.""" notebook_name = notebook_path.stem print(f"\nš Converting {notebook_name}...") # Create temporary directory for nbgradio output temp_dir = Path(f"temp_{notebook_name}") temp_dir.mkdir(exist_ok=True) try: # Run nbgradio with --spaces and --fragment cmd = [ "nbgradio", "build", str(notebook_path), "--spaces", "--fragment", "--output-dir", str(temp_dir) ] print(f"š Running: {' '.join(cmd)}") result = subprocess.run(cmd, capture_output=True, text=True) if result.returncode != 0: print(f"ā Error converting {notebook_name}:") print(result.stderr) return False print(f"ā Successfully converted {notebook_name}") # Process the generated fragment fragment_file = temp_dir / "fragments" / f"{notebook_name}.html" if fragment_file.exists(): create_recipe_page(notebook_name, fragment_file) return True else: print(f"ā Fragment file not found: {fragment_file}") return False except Exception as e: print(f"ā Error processing {notebook_name}: {e}") return False finally: # Clean up temporary directory if temp_dir.exists(): shutil.rmtree(temp_dir) def create_recipe_page(notebook_name, fragment_file): """Create a complete recipe page from the fragment.""" recipes_dir = Path("recipes") recipes_dir.mkdir(exist_ok=True) # Read the fragment content with open(fragment_file, 'r', encoding='utf-8') as f: fragment_content = f.read() # Extract title from notebook name (convert snake_case to Title Case) title = notebook_name.replace('_', ' ').replace('-', ' ').title() # Create the complete recipe page recipe_html = f"""
Migrate from proprietary APIs to open-source alternatives