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()