2022-10-29 05:53:06 +08:00
|
|
|
import math
|
2022-12-21 09:27:21 +08:00
|
|
|
|
|
|
|
import pandas as pd
|
|
|
|
|
2022-10-29 05:53:06 +08:00
|
|
|
import gradio as gr
|
|
|
|
import datetime
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
def get_time():
|
|
|
|
return datetime.datetime.now()
|
|
|
|
|
|
|
|
plot_end = 2 * math.pi
|
|
|
|
|
|
|
|
def get_plot(period=1):
|
|
|
|
global plot_end
|
|
|
|
x = np.arange(plot_end - 2 * math.pi, plot_end, 0.02)
|
2022-11-03 00:48:26 +08:00
|
|
|
y = np.sin(2 * math.pi * period * x)
|
2023-09-19 12:17:06 +08:00
|
|
|
update = gr.LinePlot(
|
2022-12-21 09:27:21 +08:00
|
|
|
value=pd.DataFrame({"x": x, "y": y}),
|
|
|
|
x="x",
|
|
|
|
y="y",
|
|
|
|
title="Plot (updates every second)",
|
|
|
|
width=600,
|
|
|
|
height=350,
|
|
|
|
)
|
2022-10-29 05:53:06 +08:00
|
|
|
plot_end += 2 * math.pi
|
2022-12-21 09:27:21 +08:00
|
|
|
if plot_end > 1000:
|
|
|
|
plot_end = 2 * math.pi
|
|
|
|
return update
|
2022-10-29 05:53:06 +08:00
|
|
|
|
|
|
|
with gr.Blocks() as demo:
|
|
|
|
with gr.Row():
|
|
|
|
with gr.Column():
|
|
|
|
c_time2 = gr.Textbox(label="Current Time refreshed every second")
|
2022-11-03 00:48:26 +08:00
|
|
|
gr.Textbox(
|
|
|
|
"Change the value of the slider to automatically update the plot",
|
|
|
|
label="",
|
|
|
|
)
|
|
|
|
period = gr.Slider(
|
|
|
|
label="Period of plot", value=1, minimum=0, maximum=10, step=1
|
|
|
|
)
|
2022-12-21 09:27:21 +08:00
|
|
|
plot = gr.LinePlot(show_label=False)
|
2022-10-29 05:53:06 +08:00
|
|
|
with gr.Column():
|
|
|
|
name = gr.Textbox(label="Enter your name")
|
|
|
|
greeting = gr.Textbox(label="Greeting")
|
|
|
|
button = gr.Button(value="Greet")
|
|
|
|
button.click(lambda s: f"Hello {s}", name, greeting)
|
|
|
|
|
|
|
|
demo.load(lambda: datetime.datetime.now(), None, c_time2, every=1)
|
2022-11-03 00:48:26 +08:00
|
|
|
dep = demo.load(get_plot, None, plot, every=1)
|
|
|
|
period.change(get_plot, period, plot, every=1, cancels=[dep])
|
2022-10-29 05:53:06 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-07-30 13:08:51 +08:00
|
|
|
demo.launch()
|