File size: 2,925 Bytes
f5e4ebb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import pandas as pd
import time


# ── Demo data source ──────────────────────────────────────────────────────────
# Replace this with your real data source (DB query, API call, model output…)
SAMPLE_ROWS = [
    {"Name": "Alpha",   "Value": 10,  "Status": "βœ… Done"},
    {"Name": "Beta",    "Value": 25,  "Status": "βœ… Done"},
    {"Name": "Gamma",   "Value": 15,  "Status": "βœ… Done"},
    {"Name": "Delta",   "Value": 42,  "Status": "βœ… Done"},
    {"Name": "Epsilon", "Value": 8,   "Status": "βœ… Done"},
    {"Name": "Zeta",    "Value": 33,  "Status": "βœ… Done"},
    {"Name": "Eta",     "Value": 19,  "Status": "βœ… Done"},
    {"Name": "Theta",   "Value": 56,  "Status": "βœ… Done"},
]


# ── Generator: yields growing DataFrame on each step ─────────────────────────
def stream_dataframe(query: str, delay: float):
    """
    Streams rows into a gr.Dataframe one-by-one.
    Yields the *full accumulated* DataFrame each iteration so Gradio
    replaces the table contents live.
    """
    cols = ["Name", "Value", "Status"]
    df = pd.DataFrame(columns=cols)
    yield df, "⏳ Starting…"

    source = [r for r in SAMPLE_ROWS if query.lower() in r["Name"].lower()] \
             if query.strip() else SAMPLE_ROWS

    if not source:
        yield df, "⚠️ No results found."
        return

    for i, row in enumerate(source, 1):
        time.sleep(delay)
        df = pd.concat([df, pd.DataFrame([row])], ignore_index=True)
        yield df, f"⏳ Loaded {i} / {len(source)} rows…"

    yield df, f"βœ… Complete β€” {len(df)} rows loaded."


# ── Gradio UI ─────────────────────────────────────────────────────────────────
with gr.Blocks(title="Streaming DataFrame Demo") as demo:
    gr.Markdown("# πŸ“Š Streaming DataFrame\nRows populate live as data arrives.")

    with gr.Row():
        query_box = gr.Textbox(
            label="Filter by name (leave blank for all)",
            placeholder="e.g. Alpha",
            scale=4,
        )
        delay_slider = gr.Slider(
            minimum=0.1, maximum=2.0, value=0.6, step=0.1,
            label="Row delay (s)",
            scale=1,
        )

    run_btn = gr.Button("β–Ά Stream Data", variant="primary")

    status = gr.Textbox(label="Status", interactive=False)
    table = gr.Dataframe(
        headers=["Name", "Value", "Status"],
        datatype=["str", "number", "str"],
        interactive=False,
        wrap=True,
    )

    run_btn.click(
        fn=stream_dataframe,
        inputs=[query_box, delay_slider],
        outputs=[table, status],
    )

if __name__ == "__main__":
    demo.launch()