Stream-table / app.py
abdurrahman-data-scientist's picture
Create app.py
f5e4ebb verified
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()