Py.Cafe

XLlobet/

figure-friday-steak-risk-survey-analysis

Figure Friday Steak Risk Survey Analysis

DocsPricing
  • figures/
  • 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from dash import Dash, Input, Output, dcc, html
import dash_customizable_app_style as dcas
import dash_bootstrap_components as dbc
import plotly.express as px
from plotly.graph_objects import Figure
import pandas as pd
import copy
from figures import plots


df:             pd.DataFrame    = pd.read_csv("https://raw.githubusercontent.com/plotly/Figure-Friday/refs/heads/main/2025/week-23/steak-risk-survey.csv")
options:        list            = list (df.columns)[2:]
colors:         list            = ["#00FF98", "#00E1FF", "#4600CF", "#C8FF00", "#0021FF", "#E600FF", "#FF002E", "#FFF300", "#37FF00", "#00FFFA"]

grid                            = plots.create_grid(df)
choropleth_regions_fig          = plots.create_choropleth(df, "Census Division")
choropleth_counts_fig           = plots.create_choropleth(df, "Count", "Cividis")

app = Dash ("Figure Friday week 23", external_stylesheets=[dbc.themes.BOOTSTRAP])

# App layout
app.layout = html.Div(
    id       = "main_container",
    style    = {"minHeight": "100vh"},
    children = [

        dcas.customize_app_selectors(),
        html.H1("Steak Risk Survey", className="p-4 bg-light text-primary mb-0"),
        grid,

        html.Div(
            id          = "main_div",
            className   = "m-0 p-2 w-100",
            children    = [
                html.Div(
                    className   = "row m-0 p-0",
                    children    = [
                        html.Div(
                            className   = "col-6 m-0 p-0",
                            children    = [
                                dcc.Loading(
                                    type        = 'cube',
                                    children    = [
                                        dcc.Graph(id = "choropleth_1", figure = choropleth_regions_fig),
                                    ]
                                )
                            ]
                        ),
                        html.Div(
                            className   = "col-6 m-0 p-0",
                            children    = [
                                dcc.Loading(
                                    type        = 'cube',
                                    children    = [
                                        dcc.Graph(id = "choropleth_2", figure = choropleth_counts_fig),
                                    ]
                                )
                            ]
                        ),
                    ]
                ),
                html.Div(
                    className   = "col-12 m-0 p-0",
                    children    = [
                        dcc.Dropdown(
                            className   = "text-dark",
                            id          = "variable_1",
                            options     = options[0:8],
                            value       = options[0],
                        ),
                        dcc.Dropdown(
                            className   = "text-dark",
                            id          = "variable_2",
                            options     = options[7:],
                            value       = options[8],
                        ),
                        dcc.Loading(
                            type        = 'cube',
                            children    = [
                                dcc.Graph(id = "figure_1")
                            ]
                        )
                    ]
                )
            ])

    ])

@app.callback(
    Output("figure_1",      'figure'),
    Output("choropleth_1",  "figure"),
    Output("choropleth_2",  "figure"),
    Input('variable_1',     'value'),
    Input('variable_2',     'value'),
    Input("bg_color",       "value"),
    Input("text_color",     "value"),
    Input("font_type",      "value"),
)
def two_variable_corssfiltering(variable_1, 
                                variable_2, 
                                bg_color, 
                                text_color, 
                                font_type):

    filtered_df:    pd.DataFrame    = df[[variable_1, variable_2]].dropna(how="any")

    options_lst:    list            = [variable_1, variable_2]
    cat_orders:     dict            = {cat:sorted(set(list(filtered_df[cat]))) for cat in options_lst}

    figure:         Figure          = px.histogram(
                                            data_frame                = filtered_df, 
                                            x                         = variable_2, 
                                            facet_row                 = variable_1, 
                                            color                     = variable_2, 
                                            color_discrete_sequence   = colors,
                                            category_orders           = cat_orders,
                                            title                     = variable_1)
    
    figure.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]) if "=" in a.text else a.update(text=a.text))

    figure.update_layout(
        paper_bgcolor   = bg_color,
        plot_bgcolor    = bg_color,
        font_family     = font_type,
        font_color      = text_color,
        height          = 600,
        bargap          = 0.01
    )

    figure.update_xaxes(showline            = True,
                        showgrid            = False,
                        linewidth           = 0.5,
                        linecolor           = "#E7E7E7",
                        mirror              = True,
                        zeroline            = False)
    figure.update_yaxes(showline            = False,
                        showgrid            = False,
                        linewidth           = 0.5,
                        linecolor           = "#E7E7E7",
                        mirror              = False,
                        zeroline            = False)
    
    choro_1 = copy.deepcopy(choropleth_regions_fig)
    choro_2 = copy.deepcopy(choropleth_counts_fig)

    choro_1.update_layout(  paper_bgcolor   = bg_color,
                            font_family     = font_type,
                            font_color      = text_color)
    choro_1.update_geos(    bgcolor         = bg_color)

    choro_2.update_layout(  paper_bgcolor   = bg_color,
                            font_family     = font_type,
                            font_color      = text_color)
    choro_2.update_geos(    bgcolor         = bg_color)

    return figure, choro_1, choro_2

if __name__ == "__main__":
    app.run(debug=True)