Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
import gradio as gr
|
| 6 |
+
import datetime
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def get_time():
|
| 11 |
+
return datetime.datetime.now()
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
plot_end = 2 * math.pi
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def get_plot(period=1):
|
| 18 |
+
global plot_end
|
| 19 |
+
x = np.arange(plot_end - 2 * math.pi, plot_end, 0.02)
|
| 20 |
+
y = np.sin(2 * math.pi * period * x)
|
| 21 |
+
update = gr.LinePlot.update(
|
| 22 |
+
value=pd.DataFrame({"x": x, "y": y}),
|
| 23 |
+
x="x",
|
| 24 |
+
y="y",
|
| 25 |
+
title="Plot (updates every second)",
|
| 26 |
+
width=600,
|
| 27 |
+
height=350,
|
| 28 |
+
)
|
| 29 |
+
plot_end += 2 * math.pi
|
| 30 |
+
if plot_end > 1000:
|
| 31 |
+
plot_end = 2 * math.pi
|
| 32 |
+
return update
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
with gr.Blocks() as demo:
|
| 36 |
+
with gr.Row():
|
| 37 |
+
with gr.Column():
|
| 38 |
+
c_time2 = gr.Textbox(label="Current Time refreshed every second")
|
| 39 |
+
gr.Textbox(
|
| 40 |
+
"Change the value of the slider to automatically update the plot",
|
| 41 |
+
label="",
|
| 42 |
+
)
|
| 43 |
+
period = gr.Slider(
|
| 44 |
+
label="Period of plot", value=1, minimum=0, maximum=10, step=1
|
| 45 |
+
)
|
| 46 |
+
plot = gr.LinePlot(show_label=False)
|
| 47 |
+
with gr.Column():
|
| 48 |
+
name = gr.Textbox(label="Enter your name")
|
| 49 |
+
greeting = gr.Textbox(label="Greeting")
|
| 50 |
+
button = gr.Button(value="Greet")
|
| 51 |
+
button.click(lambda s: f"Hello {s}", name, greeting)
|
| 52 |
|
| 53 |
+
demo.load(lambda: datetime.datetime.now(), None, c_time2, every=1)
|
| 54 |
+
dep = demo.load(get_plot, None, plot, every=1)
|
| 55 |
+
period.change(get_plot, period, plot, every=1, cancels=[dep])
|
| 56 |
|
| 57 |
+
if __name__ == "__main__":
|
| 58 |
+
demo.queue().launch()
|