๐Ÿ“ฆ lichon / streamlit-demo

๐Ÿ“„ streamlit_wakeup.py ยท 91 lines
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91from playwright.async_api import async_playwright
import subprocess
import atexit
import asyncio
import sys
import os

# Streamlit app URL from environment variable (or default)
STREAMLIT_URL = os.environ.get("STREAMLIT_APP_URL", "https://lichon.streamlit.app/")


async def click_wake_up_button(page):
    # Try to find the wake-up button
    button = await page.query_selector("//button[contains(text(),'Yes, get this app back up')]")
    if button:
        print("Wake-up button found. Clicking...")
        await button.click()
        # Wait for the button to disappear
        try:
            await page.wait_for_selector(
                "//button[contains(text(),'Yes, get this app back up')]",
                state="detached",
                timeout=15000
            )
            print("Button clicked and disappeared โœ… (app should be waking up)")
        except Exception:
            print("Button was clicked but did NOT disappear โŒ (possible failure)")
    else:
        print("No wake-up button found. Assuming app is already awake โœ…")


async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=True, args=[
            '--no-sandbox',
            '--disable-dev-shm-usage',
            '--disable-gpu',
            '--single-process',
            '--window-size=1920,1080'
        ])
        page = await browser.new_page()
        try:
            while True:
                await page.goto(STREAMLIT_URL, timeout=30000)
                print(f"Playwright Opened {STREAMLIT_URL}")

                await click_wake_up_button(page)
                await asyncio.sleep(36000)  # Keep the script running
        except Exception as e:
            print(f"Playwright Unexpected error: {e}")
            exit(1)
        finally:
            await browser.close()
            print("Playwright Script finished.")


class KeepAlive:
    def __init__(self):
        self.proc: subprocess.Popen | None = None

    def __call__(
        self,
        secrets: dict = None,
    ) -> None:
        if not self.is_alive():
            self.start(secrets)
        atexit.register(self.proc.terminate)

    def terminate(self) -> None:
        if self.proc:
            self.proc.terminate()
        atexit.unregister(self.proc.terminate)

    def is_alive(self) -> bool:
        return self.proc and self.proc.poll() is None

    def start(self, secrets: dict) -> None:
        print("Starting Playwright...")
        self.proc = subprocess.Popen(
            [sys.executable, "streamlit_wakeup.py"],
            shell=False
        )


keepAlive = KeepAlive()


if __name__ == "__main__":
    os.system("playwright install chromium")
    asyncio.run(main())