Spaces:
Running
Running
| # Install Playwright Chromium into the demo-local cache used by Live Run. | |
| set -euo pipefail | |
| ROOT="$(cd "$(dirname "$0")/.." && pwd)" | |
| CACHE="${PLAYWRIGHT_BROWSERS_PATH:-$ROOT/.playwright}" | |
| PY="${ROOT}/.venv/bin/python" | |
| if [[ ! -x "$PY" ]]; then | |
| PY="$(command -v python3)" | |
| fi | |
| mkdir -p "$CACHE" | |
| export PLAYWRIGHT_BROWSERS_PATH="$CACHE" | |
| echo "Installing Playwright Chromium into: $CACHE" | |
| "$PY" -m playwright install chromium | |
| # Ensure .env points at the local cache (override Cursor sandbox temp paths). | |
| ENV_FILE="$ROOT/.env" | |
| touch "$ENV_FILE" | |
| if grep -q '^PLAYWRIGHT_BROWSERS_PATH=' "$ENV_FILE"; then | |
| # portable in-place replace | |
| tmp="$(mktemp)" | |
| awk -v p="$CACHE" ' | |
| BEGIN { done=0 } | |
| /^PLAYWRIGHT_BROWSERS_PATH=/ { print "PLAYWRIGHT_BROWSERS_PATH=" p; done=1; next } | |
| { print } | |
| END { if (!done) print "PLAYWRIGHT_BROWSERS_PATH=" p } | |
| ' "$ENV_FILE" > "$tmp" | |
| mv "$tmp" "$ENV_FILE" | |
| else | |
| printf '\nPLAYWRIGHT_BROWSERS_PATH=%s\n' "$CACHE" >> "$ENV_FILE" | |
| fi | |
| echo "Done. Restart Streamlit so the new PLAYWRIGHT_BROWSERS_PATH is picked up." | |
| echo "Quick check:" | |
| PLAYWRIGHT_BROWSERS_PATH="$CACHE" "$PY" - <<'PY' | |
| import os | |
| from playwright.sync_api import sync_playwright | |
| os.environ.setdefault( | |
| "PLAYWRIGHT_BROWSERS_PATH", | |
| os.environ.get("PLAYWRIGHT_BROWSERS_PATH", ""), | |
| ) | |
| with sync_playwright() as p: | |
| try: | |
| browser = p.chromium.launch(channel="chrome", headless=True) | |
| except Exception: | |
| browser = p.chromium.launch(headless=True) | |
| page = browser.new_page() | |
| page.goto("https://example.com", timeout=20000) | |
| print("ok:", page.title()) | |
| browser.close() | |
| PY | |