Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import time
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
# ββ Demo data source ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 7 |
+
# Replace this with your real data source (DB query, API call, model outputβ¦)
|
| 8 |
+
SAMPLE_ROWS = [
|
| 9 |
+
{"Name": "Alpha", "Value": 10, "Status": "β
Done"},
|
| 10 |
+
{"Name": "Beta", "Value": 25, "Status": "β
Done"},
|
| 11 |
+
{"Name": "Gamma", "Value": 15, "Status": "β
Done"},
|
| 12 |
+
{"Name": "Delta", "Value": 42, "Status": "β
Done"},
|
| 13 |
+
{"Name": "Epsilon", "Value": 8, "Status": "β
Done"},
|
| 14 |
+
{"Name": "Zeta", "Value": 33, "Status": "β
Done"},
|
| 15 |
+
{"Name": "Eta", "Value": 19, "Status": "β
Done"},
|
| 16 |
+
{"Name": "Theta", "Value": 56, "Status": "β
Done"},
|
| 17 |
+
]
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# ββ Generator: yields growing DataFrame on each step βββββββββββββββββββββββββ
|
| 21 |
+
def stream_dataframe(query: str, delay: float):
|
| 22 |
+
"""
|
| 23 |
+
Streams rows into a gr.Dataframe one-by-one.
|
| 24 |
+
Yields the *full accumulated* DataFrame each iteration so Gradio
|
| 25 |
+
replaces the table contents live.
|
| 26 |
+
"""
|
| 27 |
+
cols = ["Name", "Value", "Status"]
|
| 28 |
+
df = pd.DataFrame(columns=cols)
|
| 29 |
+
yield df, "β³ Startingβ¦"
|
| 30 |
+
|
| 31 |
+
source = [r for r in SAMPLE_ROWS if query.lower() in r["Name"].lower()] \
|
| 32 |
+
if query.strip() else SAMPLE_ROWS
|
| 33 |
+
|
| 34 |
+
if not source:
|
| 35 |
+
yield df, "β οΈ No results found."
|
| 36 |
+
return
|
| 37 |
+
|
| 38 |
+
for i, row in enumerate(source, 1):
|
| 39 |
+
time.sleep(delay)
|
| 40 |
+
df = pd.concat([df, pd.DataFrame([row])], ignore_index=True)
|
| 41 |
+
yield df, f"β³ Loaded {i} / {len(source)} rowsβ¦"
|
| 42 |
+
|
| 43 |
+
yield df, f"β
Complete β {len(df)} rows loaded."
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# ββ Gradio UI βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 47 |
+
with gr.Blocks(title="Streaming DataFrame Demo") as demo:
|
| 48 |
+
gr.Markdown("# π Streaming DataFrame\nRows populate live as data arrives.")
|
| 49 |
+
|
| 50 |
+
with gr.Row():
|
| 51 |
+
query_box = gr.Textbox(
|
| 52 |
+
label="Filter by name (leave blank for all)",
|
| 53 |
+
placeholder="e.g. Alpha",
|
| 54 |
+
scale=4,
|
| 55 |
+
)
|
| 56 |
+
delay_slider = gr.Slider(
|
| 57 |
+
minimum=0.1, maximum=2.0, value=0.6, step=0.1,
|
| 58 |
+
label="Row delay (s)",
|
| 59 |
+
scale=1,
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
run_btn = gr.Button("βΆ Stream Data", variant="primary")
|
| 63 |
+
|
| 64 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 65 |
+
table = gr.Dataframe(
|
| 66 |
+
headers=["Name", "Value", "Status"],
|
| 67 |
+
datatype=["str", "number", "str"],
|
| 68 |
+
interactive=False,
|
| 69 |
+
wrap=True,
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
run_btn.click(
|
| 73 |
+
fn=stream_dataframe,
|
| 74 |
+
inputs=[query_box, delay_slider],
|
| 75 |
+
outputs=[table, status],
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
demo.launch()
|