Py.Cafe

maxi.schulz/

vizro-parameter-interaction

Interactive Data Visualizations with Vizro

DocsPricing
  • assets/
  • app.py
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""Dev app to try things out."""
import pandas as pd
from dash import callback, Input, Output, no_update

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

iris = px.data.iris()

ag_grid_fake_data = pd.DataFrame(
    {
        "name": ["John", "Jane", "Joe", "Jake"],
        "age": [25, 30, 35, 40],
        "graph_type": ["scatter", "box", "scatter", "box"],
        "city": ["New York", "Los Angeles", "Chicago", "Houston"],
    }
)


@capture("graph")
def custom_graph(data_frame, graph_type, **kwargs):
    if graph_type == "scatter":
        return px.scatter(data_frame, **kwargs)
    elif graph_type == "box":
        return px.box(data_frame, **kwargs)


page = vm.Page(
    title="My Page Title",
    components=[
        vm.AgGrid(
            id="ag_grid_id",
            title="My Grid",
            header="⤵ Click on a \"Graph_type\" column cell to select the chart type below.",
            figure=dash_ag_grid(
                id="underlying_ag_grid_id",
                data_frame=ag_grid_fake_data
            ),
        ),
        vm.Graph(
            id="graph_id",
            title="My Graph",
            figure=custom_graph(
                data_frame=iris,
                graph_type="scatter",
                x="sepal_width",
                y="sepal_length",
                color="species"
            )
        ),
    ],
    controls=[
        vm.Filter(column="species"),
        vm.Parameter(
            targets=["graph_id.graph_type"],
            selector=vm.RadioItems(
                id="graph_type_parameter_id",
                options=["scatter", "box"],
                value="scatter",
            )
        )
    ]
)


@callback(
    Output("graph_type_parameter_id", "value"),
    Input("underlying_ag_grid_id", "cellClicked"),
)
def update_parameter(ag_grid_cell_clicked):
    if not ag_grid_cell_clicked:
        return no_update
    return ag_grid_cell_clicked["value"]


dashboard = vm.Dashboard(pages=[page])


Vizro().build(dashboard).run(debug=False)