Py.Cafe

stichbury/

vizro-global-electricity-access-2

Global Access to Electricity Visualization

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
############ Imports ##############
import vizro.models as vm
from vizro.models.types import capture
from vizro import Vizro
import pandas as pd
from vizro.managers import data_manager
from vizro.models.types import capture



####### Function definitions ######
@capture("graph")
def electricity_access_chart(data_frame):
    import plotly.graph_objects as go

    # Get unique countries in the filtered data
    countries = data_frame["Entity"].unique()

    # Create figure
    fig = go.Figure()

    # Add a line for each country with text labels
    for country in countries:
        country_data = data_frame[data_frame["Entity"] == country].sort_values("Year")

        fig.add_trace(
            go.Scatter(
                x=country_data["Year"],
                y=country_data["Access to electricity (% of population)"],
                mode="lines+markers",
                name=country,
                hovertemplate="<b>%{fullData.name}</b><br>Year: %{x}<br>Access: %{y:.1f}%<extra></extra>",
                line=dict(width=2.5),
            )
        )

    # Update layout
    fig.update_layout(
        xaxis_title="Year",
        yaxis_title="Access to electricity (% of population)",
        yaxis=dict(range=[0, 105]),
        hovermode="closest",
        legend=dict(
            orientation="v",
            yanchor="middle",
            y=0.5,
            xanchor="left",
            x=1.02,
            bgcolor="rgba(0,0,0,0)",
        ),
        margin=dict(r=150),
    )

    return fig


####### Data Manager Settings #####
data_manager["electricity_data"] = pd.read_csv(
    "share-of-the-population-with-access-to-electricity.csv"
)

########### Model code ############
model = vm.Dashboard(
    pages=[
        vm.Page(
            components=[
                vm.Graph(
                    id="electricity_line_chart",
                    type="graph",
                    figure=electricity_access_chart(data_frame="electricity_data"),
                    title="Access to Electricity by Country Over Time",
                )
            ],
            title="Global Electricity Access",
            layout=vm.Flex(type="flex", direction="column"),
            controls=[
                vm.Filter(
                    type="filter",
                    column="Entity",
                    targets=["electricity_line_chart"],
                    selector=vm.Checklist(
                        type="checklist",
                        value=[
                            "United States",
                            "China",
                            "Brazil",
                            "India",
                            "Afghanistan",
                            "Rwanda",
                            "Haiti",
                        ],
                        title="Select Countries",
                    ),
                )
            ],
        )
    ],
    theme="vizro_dark",
    title="Electricity Access Dashboard",
)

Vizro().build(model).run()