File size: 1,446 Bytes
2f7596f
 
 
 
2a0d681
 
 
 
 
 
2f7596f
 
2a0d681
 
2f7596f
2a0d681
 
 
 
 
2f7596f
 
2a0d681
 
 
2f7596f
 
2a0d681
 
 
 
 
2f7596f
 
2a0d681
 
 
841d632
2f7596f
 
2a0d681
 
 
 
 
1d45997
 
 
2f7596f
 
 
2a0d681
 
2f7596f
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
import streamlit as st
import asyncio
from playwright.async_api import async_playwright

if "logs" not in st.session_state:
    st.session_state.logs = []

if "running" not in st.session_state:
    st.session_state.running = False


async def run_bot(url):
    st.session_state.logs.append("Starting bot...")

    async with async_playwright() as p:
        browser = await p.chromium.launch(
            headless=True,
            args=["--no-sandbox", "--disable-dev-shm-usage"]
        )

        page = await browser.new_page()

        page.on("console", lambda msg: st.session_state.logs.append(f"LOG: {msg.text}"))
        page.on("request", lambda req: st.session_state.logs.append(f"REQ: {req.url}"))
        page.on("response", lambda res: st.session_state.logs.append(f"RES: {res.url} {res.status}"))

        await page.goto(url)

        # keep session alive
        for _ in range(15):
            await asyncio.sleep(1)

        await browser.close()

    st.session_state.logs.append("Bot finished.")
    st.session_state.running = False


st.title("DevTools Bot Dashboard")

url = st.text_input("Enter URL", "https://example.com")

if st.button("Run Bot") and not st.session_state.running:
    st.session_state.logs.clear()
    st.session_state.running = True
    # Run in background
    loop = asyncio.get_event_loop()
    loop.create_task(run_bot(url))


st.subheader("Logs")

for log in st.session_state.logs:
    st.text(log)