Spaces:
Running
Running
File size: 2,387 Bytes
e9d126c |
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 |
#!/usr/bin/env python3
import os
import subprocess
import argparse
import webbrowser
import time
import sys
from pathlib import Path
def main():
parser = argparse.ArgumentParser(description="Build and preview marimo notebooks site")
parser.add_argument(
"--port", default=8000, type=int, help="Port to run the server on"
)
parser.add_argument(
"--no-build", action="store_true", help="Skip building the site (just serve existing files)"
)
parser.add_argument(
"--output-dir", default="_site", help="Output directory for built files"
)
args = parser.parse_args()
# Store the current directory
original_dir = os.getcwd()
try:
# Build the site if not skipped
if not args.no_build:
print("Building site...")
build_script = Path("scripts/build.py")
if not build_script.exists():
print(f"Error: Build script not found at {build_script}")
return 1
result = subprocess.run(
[sys.executable, str(build_script), "--output-dir", args.output_dir],
check=False
)
if result.returncode != 0:
print("Warning: Build process completed with errors.")
# Check if the output directory exists
output_dir = Path(args.output_dir)
if not output_dir.exists():
print(f"Error: Output directory '{args.output_dir}' does not exist.")
return 1
# Change to the output directory
os.chdir(args.output_dir)
# Open the browser
url = f"http://localhost:{args.port}"
print(f"Opening {url} in your browser...")
webbrowser.open(url)
# Start the server
print(f"Starting server on port {args.port}...")
print("Press Ctrl+C to stop the server")
# Use the appropriate Python executable
subprocess.run([sys.executable, "-m", "http.server", str(args.port)])
return 0
except KeyboardInterrupt:
print("\nServer stopped.")
return 0
except Exception as e:
print(f"Error: {e}")
return 1
finally:
# Always return to the original directory
os.chdir(original_dir)
if __name__ == "__main__":
sys.exit(main())
|