import vizro.actions as va
import vizro.models as vm
import vizro.plotly.express as px
from vizro import Vizro
from vizro.tables import dash_ag_grid
from vizro.models.types import capture
tips = px.data.tips()
# This function written by ChatGPT and could probably be better...
@capture("graph")
def highlighted_box(data_frame, highlight=None):
# Dim all traces first
fig = px.box(data_frame, x="tip", y="sex")
for trace in fig.data:
trace.opacity = 0.2
if highlight is not None:
# Overlay highlighted category
df_highlight = data_frame[data_frame["sex"] == highlight]
fig_highlight = px.box(
df_highlight, x="tip", y="sex", color="sex"
)
for trace in fig_highlight.data:
trace.opacity = 1.0 # make highlight fully visible
fig.add_trace(trace)
return fig
page = vm.Page(
title="Cross-filter from graph to table",
components=[
vm.Graph(
id="box",
title="Click on a box to use that box's sex to filter table",
figure=highlighted_box(tips),
actions=[
va.set_control(control="sex_filter", value="y"),
va.set_control(control="highlight", value="y")
]
),
vm.AgGrid(id="tips_table", figure=dash_ag_grid(tips)),
],
controls=[
vm.Filter(id="sex_filter", column="sex", targets=["tips_table"]),
# This parameter looks pretty silly, definitely you'd want to make it hidden in reality
vm.Parameter(id="highlight", selector=vm.RadioItems(options=["NONE", "Male", "Female"]), targets=["box.highlight"])
],
)
dashboard = vm.Dashboard(pages=[page])
Vizro().build(dashboard).run()