您的仪表板可能不仅仅包含图表。让我们来看看仪表板中其他一些常见的组件。
使用任何标准的Gradio表单组件来过滤你的数据。你可以通过事件监听器或函数作为值的语法来实现这一点。让我们首先看看事件监听器的方法:
import gradio as gr
from data import df
with gr.Blocks() as demo:
with gr.Row():
origin = gr.Dropdown(["All", "DFW", "DAL", "HOU"], value="All", label="Origin")
destination = gr.Dropdown(["All", "JFK", "LGA", "EWR"], value="All", label="Destination")
max_price = gr.Slider(0, 1000, value=1000, label="Max Price")
plt = gr.ScatterPlot(df, x="time", y="price", inputs=[origin, destination, max_price])
@gr.on(inputs=[origin, destination, max_price], outputs=plt)
def filtered_data(origin, destination, max_price):
_df = df[df["price"] <= max_price]
if origin != "All":
_df = _df[_df["origin"] == origin]
if destination != "All":
_df = _df[_df["destination"] == destination]
return _df
demo.launch()这将是相同演示的函数作为值的方法。
import gradio as gr
from data import df
with gr.Blocks() as demo:
with gr.Row():
origin = gr.Dropdown(["All", "DFW", "DAL", "HOU"], value="All", label="Origin")
destination = gr.Dropdown(["All", "JFK", "LGA", "EWR"], value="All", label="Destination")
max_price = gr.Slider(0, 1000, value=1000, label="Max Price")
def filtered_data(origin, destination, max_price):
_df = df[df["price"] <= max_price]
if origin != "All":
_df = _df[_df["origin"] == origin]
if destination != "All":
_df = _df[_df["destination"] == destination]
return _df
gr.ScatterPlot(filtered_data, x="time", y="price", inputs=[origin, destination, max_price])
demo.launch()在你的仪表板上添加 gr.DataFrame 和 gr.Label 以获取一些具体的数据。
import gradio as gr
from data import df
with gr.Blocks() as demo:
with gr.Row():
gr.Label(len(df), label="Flight Count")
gr.Label(f"${df['price'].min()}", label="Cheapest Flight")
gr.DataFrame(df)
demo.launch()