import json
from dash import Output, Input, callback
from dash.exceptions import PreventUpdate
import vizro.models as vm
import vizro.plotly.express as px
from vizro import Vizro
from vizro.models.types import capture
from vizro.tables import dash_ag_grid
df = px.data.iris()
graph = vm.Graph(id="graph", figure=px.box(df, x="petal_width", color="species"))
@capture("action")
def f(data):
code_markdown = f"```\n{json.dumps(data, indent=2)}\n```"
return graph(x=data["colId"]), code_markdown
@callback(Output("radio", "value"), Output("card", "children"), Input("grid", "cellClicked"))
def g(data):
if data is None:
raise PreventUpdate
code_markdown = f"```\n{json.dumps(data, indent=2)}\n```"
return data["colId"], code_markdown
page = vm.Page(
title="Hi",
components=[
vm.AgGrid(
figure=dash_ag_grid(id="grid", data_frame=df),
# actions=[vm.Action(function=f(), inputs=["grid.cellClicked"], outputs=["graph.figure", "card.children"])],
# actions=[
# vm.Action(function=g(), inputs=["grid.cellClicked"], outputs=["radio.value", "card.children"]),
# ],
),
graph,
vm.Card(id="card", text="Click on a cell on the grid and the box plot will show that column"),
],
controls=[
vm.Parameter(targets=["graph.x"], selector=vm.RadioItems(id="radio", options=list(df.columns[:4]))),
vm.Filter(column="species"),
],
)
dashboard = vm.Dashboard(pages=[page])
Vizro().build(dashboard).run()