Py.Cafe

petar-qb/

vizro-filters-value-reset

Gapminder Data Analysis with Vizro

DocsPricing
  • 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
"""Example to show the reset of the filter values on a button click."""
from dash import callback, Input, Output

import vizro.models as vm
import vizro.plotly.express as px
from vizro import Vizro
from vizro.models.types import capture


@callback(
    Output("continent_filter", "value", allow_duplicate=True),
    Output("country_filter", "value", allow_duplicate=True),
    Output("life_exp_filter", "value", allow_duplicate=True),
    Input("reset_button", "n_clicks"),
    prevent_initial_call=True
)
def reset_filters(n_clicks):
    return ["ALL"], ["ALL"], [23, 83]

# TODO: (Remove) This is an example how it would work if the AV2-Stage-1 were implemented
# @capture("action")
# def reset_filters():
#     return ["ALL"], ["ALL"], [23, 83]


vm.Page.add_type("controls", vm.Button)

page_one = vm.Page(
    title="Graph / AG Grid",
    components=[
        vm.Graph(
            figure=px.scatter(
                data_frame=px.data.gapminder(),
                x="continent",
                y="lifeExp",
                color="continent",
                title="Graph",
            )
        )
    ],
    controls=[
        vm.Filter(
            column="continent",
            selector=vm.Dropdown(id="continent_filter")
        ),
        vm.Filter(
            column="country",
            selector=vm.Dropdown(id="country_filter")
        ),
        vm.Filter(
            column="lifeExp",
            selector=vm.RangeSlider(id="life_exp_filter", min=23, max=83, step=1, marks=None)
        ),
        vm.Button(
            id="reset_button",
            text="Reset filters",
            # TODO: (Remove) This is an example how it would work if the AV2-Stage-1 were merged
            # actions=[
            #     vm.Action(
            #         function=reset_filters(),
            #         outputs=[
            #             "continent_filter.value",
            #             "country_filter.value",
            #             "life_exp_filter.value",
            #         ]
            #     )
            # ]
        )
    ]
)

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

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