2022-12-21 00:13:51 +08:00
|
|
|
import gradio as gr
|
2024-07-23 00:52:48 +08:00
|
|
|
from data import temp_sensor_data, food_rating_data
|
2022-12-21 00:13:51 +08:00
|
|
|
|
2024-07-23 00:52:48 +08:00
|
|
|
with gr.Blocks() as line_plots:
|
|
|
|
with gr.Row():
|
|
|
|
start = gr.DateTime("2021-01-01 00:00:00", label="Start")
|
|
|
|
end = gr.DateTime("2021-01-05 00:00:00", label="End")
|
|
|
|
apply_btn = gr.Button("Apply", scale=0)
|
|
|
|
with gr.Row():
|
|
|
|
group_by = gr.Radio(["None", "30m", "1h", "4h", "1d"], value="None", label="Group by")
|
|
|
|
aggregate = gr.Radio(["sum", "mean", "median", "min", "max"], value="sum", label="Aggregation")
|
2022-12-21 00:13:51 +08:00
|
|
|
|
2024-07-23 00:52:48 +08:00
|
|
|
temp_by_time = gr.LinePlot(
|
|
|
|
temp_sensor_data,
|
|
|
|
x="time",
|
|
|
|
y="temperature",
|
|
|
|
)
|
|
|
|
temp_by_time_location = gr.LinePlot(
|
|
|
|
temp_sensor_data,
|
|
|
|
x="time",
|
|
|
|
y="temperature",
|
|
|
|
color="location",
|
|
|
|
)
|
|
|
|
|
|
|
|
time_graphs = [temp_by_time, temp_by_time_location]
|
|
|
|
group_by.change(
|
2024-07-30 13:08:51 +08:00
|
|
|
lambda group: [gr.LinePlot(x_bin=None if group == "None" else group)] * len(time_graphs),
|
|
|
|
group_by,
|
2024-07-23 00:52:48 +08:00
|
|
|
time_graphs
|
|
|
|
)
|
|
|
|
aggregate.change(
|
2024-07-30 13:08:51 +08:00
|
|
|
lambda aggregate: [gr.LinePlot(y_aggregate=aggregate)] * len(time_graphs),
|
|
|
|
aggregate,
|
2024-07-23 00:52:48 +08:00
|
|
|
time_graphs
|
|
|
|
)
|
2022-12-21 00:13:51 +08:00
|
|
|
|
2024-07-23 00:52:48 +08:00
|
|
|
def rescale(select: gr.SelectData):
|
|
|
|
return select.index
|
|
|
|
rescale_evt = gr.on([plot.select for plot in time_graphs], rescale, None, [start, end])
|
|
|
|
|
|
|
|
for trigger in [apply_btn.click, rescale_evt.then]:
|
|
|
|
trigger(
|
|
|
|
lambda start, end: [gr.LinePlot(x_lim=[start, end])] * len(time_graphs), [start, end], time_graphs
|
|
|
|
)
|
2022-12-21 00:13:51 +08:00
|
|
|
|
2024-07-23 00:52:48 +08:00
|
|
|
price_by_cuisine = gr.LinePlot(
|
|
|
|
food_rating_data,
|
|
|
|
x="cuisine",
|
|
|
|
y="price",
|
2024-06-21 07:09:04 +08:00
|
|
|
)
|
2024-07-23 00:52:48 +08:00
|
|
|
with gr.Row():
|
|
|
|
price_by_rating = gr.LinePlot(
|
|
|
|
food_rating_data,
|
|
|
|
x="rating",
|
|
|
|
y="price",
|
|
|
|
)
|
|
|
|
price_by_rating_color = gr.LinePlot(
|
|
|
|
food_rating_data,
|
|
|
|
x="rating",
|
|
|
|
y="price",
|
|
|
|
color="cuisine",
|
|
|
|
color_map={"Italian": "red", "Mexican": "green", "Chinese": "blue"},
|
|
|
|
)
|
2022-12-21 00:13:51 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-07-23 00:52:48 +08:00
|
|
|
line_plots.launch()
|