File size: 1,632 Bytes
f329675
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env bash
# 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