Py.Cafe

huong-li-nguyen/

vizro-interactive-data-transfer

Interactive Data Transfer App

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
# Vizro is an open-source toolkit for creating modular data visualization applications.
# check out https://github.com/mckinsey/vizro for more info about Vizro
# and checkout https://vizro.readthedocs.io/en/stable/ for documentation.

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


@capture("action")
def send_data_to_store(n_clicks):
    """Send data to global store"""
    return f"Button clicked {n_clicks} times"


@capture("action")
def read_data_from_store(store_data):
    """Read data from global store"""
    if store_data is None:
        return "No data received from Page One"
    return f"Received: {store_data}"


df = px.data.iris()

page_one = vm.Page(
    title="Page One",
    components=[
        vm.Button(
            id="send_button",
            text="Send to Global Store",
            actions=[
                vm.Action(
                    function=send_data_to_store(),
                    inputs=["send_button.n_clicks"],
                    outputs=["user_session_storage_id.data"],
                )
            ],
        ),
    ],
)

page_two = vm.Page(
    title="Page Two", 
    components=[
        vm.Button(
            id="display_button",
            text="Loading data...",
            actions=[
                vm.Action(
                    function=read_data_from_store(),
                    inputs=["user_session_storage_id.data"],
                    outputs=["display_button.children"],
                )
            ],
        ),
    ],
)

dashboard = vm.Dashboard(pages=[page_one, page_two])
app = Vizro().build(dashboard)

# Add global store after building dashboard
app.dash.layout.children.append(
    dcc.Store(id="user_session_storage_id", storage_type="session", data=None)
)

app.run()