Py.Cafe

ramnathv/

dash-tip-analysis-insights

Tip Analysis Insights

DocsPricing
  • app.py
  • myui.py
  • requirements.txt
  • tip_data_analyzer.py
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
from dash import Dash, dcc, Input, Output, callback, html, no_update
import dash_mantine_components as dmc
import dash_bootstrap_components as dbc

from tip_data_analyzer import TipsDataAnalyzer
import myui
import pandas as pd

analyzer = TipsDataAnalyzer()
total_bill_range = analyzer.compute_total_bill_range()
times = analyzer.tips.time.unique().tolist()

app = Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])

app.layout = dbc.Container(
    [
        html.H1("Dashboard"),
        dcc.Store(id="tips-filtered"),
        myui.ui_input_total_bill_range(total_bill_range),
        myui.ui_input_times(times),
        dbc.Card([ 
            dbc.CardHeader('Total Tippers'),
            dbc.CardBody([
              html.Div(id="output-total-tippers")
            ])
        ]),
        dbc.Card([ 
            dbc.CardHeader('Average Tip'),
            dbc.CardBody([
              html.Div(id="output-average-tip-pct")
            ])
        ]),
    ],
    className="p-4",
)


@callback(
    Output("tips-filtered", "data"),
    [
        Input("total-bill-range", "value"),
        Input("times", "value")
    ]
)
def filter_tips(total_bill_range, times):
    return (
        analyzer
          .filter_data(total_bill_range, times)
          .to_dict(orient='records')
    )

@callback(
    Output("output-total-tippers", "children"),
    Input("tips-filtered", "data")
)
def update_total_tippers(tips_filtered):
    tips_filtered_df = pd.DataFrame(tips_filtered)
    val = analyzer.compute_total_tippers(tips_filtered_df)
    return val

@callback(
    Output("output-average-tip-pct", "children"),
    Input("tips-filtered", "data")
)
def update_average_tip_pct(tips_filtered):
    tips_filtered_df = pd.DataFrame(tips_filtered)
    val = analyzer.compute_average_tip_pct(tips_filtered_df)
    return f"{val:.1%}" if val is not None else "N/A"