Py.Cafe

Dash Color Selector

DocsPricing
  • app.py
  • bank_notes.py
  • banknotesData.csv
  • 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import dash
from dash import dcc, html, Input, Output, State
import dash_bootstrap_components as dbc
import pandas as pd
import plotly.express as px
import plotly.graph_objs as go
import numpy as np
from dash.exceptions import PreventUpdate

# Load the data globally
df = pd.read_csv("banknotesData.csv")

# Preprocessing
df['gender'] = df['gender'].map({'F': 'Female', 'M': 'Male'})
df['firstAppearanceDate'] = pd.to_numeric(df['firstAppearanceDate'], errors='coerce')

# Convert numpy types to native Python types
min_year = int(df['firstAppearanceDate'].min())
max_year = int(df['firstAppearanceDate'].max())

# Color palette
color_palette = {
    'Male': '#3498db',   # light blue
    'Female': '#e74c3c',  # Soft red
    'Grid': 'rgba(0,0,0,0.05)',
    'Background': '#f4f6f7', # gray
    'Header': '#34495e'   # Dark blue-gray
}

# Initialize the Dash app
app = dash.Dash(__name__, 
                external_stylesheets=[dbc.themes.FLATLY],
                suppress_callback_exceptions=True,
                meta_tags=[{'name': 'viewport', 'content': 'width=device-width, initial-scale=1'}])
app.title = "Banknote Characters Explorer"

# App Layout
app.layout = dbc.Container([
    # Modal for character details
    dbc.Modal(
        [
            dbc.ModalHeader(
                dbc.ModalTitle("Character Details", className='text-white fw-bold'), 
                close_button=True,
                style={'backgroundColor': color_palette['Header']}
            ),
            dbc.ModalBody(id='character-details-modal-body'),
        ],
        id="character-details-modal",
        size="xl",
        scrollable=True,
        style={'backgroundColor': 'rgba(244, 246, 247,0.95)'}
    ),
    
    # Header
    dbc.Row([
        dbc.Col(html.H1("BillNoteTimeline: Banknote Character Explorer", 
                        className='text-center my-4 text-primary'), 
                width=12)
    ]),
    
    # Main content row
    dbc.Row([
        # Gender Distribution Column
        dbc.Col([
            html.H4("Gender Distribution", className='text-center'),
            html.Hr(style={'backgroundColor': color_palette['Background'], 'height': '10px' }),
            dcc.Graph(id='gender-pie-chart', config={'displayModeBar': False})
        ], width=3),
        
        # Timeline Column
        dbc.Col([
            html.H4("Historical Timeline", className='text-center'),
            dcc.RangeSlider(
                id='year-range-slider',
                min=min_year,
                max=max_year,
                step=20,
                marks={
                    str(min_year): str(min_year),
                    str(max_year): str(max_year),
                    **{str(year): str(year) for year in range(min_year + 20, max_year, 20)}
                },
                value=[min_year, max_year],
                tooltip={'placement': 'bottom', 'always_visible': False}
            ),
            dbc.Button("Reset", id="reset-button", className="mt-2"),
            dcc.Graph(id='timeline-visualization')
        ], width=9)
    ]),
    dbc.Row([
        dbc.Col([
            html.P('πŸ” Click on the circle/square to learn more about the characters from that year', 
                   className='text-center text-muted fw-bold'
    )
])
], className='mt-3'),
    
    # Additional Information
    dbc.Row([
        dbc.Col(html.Div(id='stats-summary', className='mt-4 text-center'), width=12)
    ])
], fluid=True, style={'backgroundColor': color_palette['Background']})

# Gender Pie Chart Callback
@app.callback(
    Output('gender-pie-chart', 'figure'),
    [Input('year-range-slider', 'value')]
)
def update_gender_pie_chart(year_range):
    filtered_df = df[
        (df['firstAppearanceDate'] >= year_range[0]) & 
        (df['firstAppearanceDate'] <= year_range[1])
    ]
    
    gender_counts = filtered_df['gender'].value_counts()
    
    fig = px.pie(
        values=gender_counts.values, 
        names=gender_counts.index,
        hole=0.6,
        title=f'Years ({year_range[0]}-{year_range[1]})',
        color_discrete_map={'Male': color_palette['Male'], 'Female': color_palette['Female']},
        template='simple_white'
    )
    fig.update_layout(showlegend=False,paper_bgcolor='rgb(244, 246, 247)', plot_bgcolor='rgb(244, 246, 247)')
    
    fig.update_traces(
        textposition='inside', 
        textinfo='percent+label',
        marker=dict(line=dict(color='#ffffff', width=1.5))
    )
    
    return fig

# Timeline Visualization Callback
@app.callback(
    Output('timeline-visualization', 'figure'),
    [Input('year-range-slider', 'value')]
)
def update_timeline(year_range):
    filtered_df = df[
        (df['firstAppearanceDate'] >= year_range[0]) & 
        (df['firstAppearanceDate'] <= year_range[1])
    ]
    
    year_counts = filtered_df.groupby(['firstAppearanceDate', 'gender']).size().reset_index(name='count')
    
    fig = px.scatter(
        year_counts, 
        x='firstAppearanceDate', 
        y='count', 
        color='gender',
        symbol='gender',
        symbol_sequence=['circle', 'square'],
        size='count',
        template='ygridoff',
        color_discrete_map={'Male': color_palette['Male'], 'Female': color_palette['Female']},
        title=f'Timeline of Characters ({year_range[0]}-{year_range[1]})',
        labels={'firstAppearanceDate': '', 'count': 'Character Count', 'gender': 'Gender'}
    )
    
    fig.update_layout(
        xaxis=dict(gridcolor=color_palette['Grid']),
        yaxis=dict(title='Character Count', gridcolor=color_palette['Grid']),
        legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="center", x=0.5),
        hovermode='closest',
        paper_bgcolor='rgb(244, 246, 247)', plot_bgcolor='rgb(244, 246, 247)'
    )
    
    return fig

# Reset Slider Callback
@app.callback(
    Output('year-range-slider', 'value'),
    Input('reset-button', 'n_clicks'),
    prevent_initial_call=True
)
def reset_slider(n_clicks):
    if n_clicks:
        return [min_year, max_year]
    else:
        raise PreventUpdate

@app.callback(
    [Output('character-details-modal', 'is_open'),
     Output('character-details-modal-body', 'children')],
    [Input('timeline-visualization', 'clickData')],
    [State('year-range-slider', 'value'),
     State('character-details-modal', 'is_open')]
)
def toggle_modal(clickData, year_range, is_open):
    if not clickData:
        return False, []
    
    # Get the clicked year and gender
    year = clickData['points'][0]['x']
    gender = clickData['points'][0]['curveNumber']  # 0 for Male, 1 for Female
    gender_map = {0: 'Male', 1: 'Female'}
    selected_gender = gender_map[gender]
    
    # Filter characters by year range and gender
    year_characters = df[
        (df['firstAppearanceDate'] == year) & 
        (df['gender'] == selected_gender) &
        (df['firstAppearanceDate'] >= year_range[0]) & 
        (df['firstAppearanceDate'] <= year_range[1])
    ]
    
    character_cards = [
        dbc.Card([
            dbc.CardHeader(
                html.H5(char['name'], className='text-white m-0'),
                className='bg-primary text-center'
            ),
            dbc.CardBody([
                html.P(f"🌍 Country: {char['country']}", className='card-text'),
                html.P(f"πŸ’Ό Profession: {char.get('profession', 'N/A')}", className='card-text'),
                html.P(f"πŸ’΅ Note: {char.get('currencyName', 'N/A')}", className='card-text'),
                html.P(f"πŸ’° Current Value: {char.get('currentBillValue', 'N/A')}", className='card-text'),
                html.P(f"πŸ’¬ Comments: {char.get('comments', 'N/A')}", className='card-text')
            ])
        ], className='mb-3 shadow-sm') 
        for _, char in year_characters.iterrows()
    ]
    
    modal_content = [
        html.H4(f"{selected_gender} Characters in {year}", className='text-center mb-4'),
        dbc.Row([dbc.Col(card, width=4) for card in character_cards])
    ]
    
    return not is_open, modal_content

# Stats Summary Callback
@app.callback(
    Output('stats-summary', 'children'),
    [Input('year-range-slider', 'value')]
)
def update_stats_summary(year_range):
    filtered_df = df[
        (df['firstAppearanceDate'] >= year_range[0]) & 
        (df['firstAppearanceDate'] <= year_range[1])
    ]
    
    total_characters = len(filtered_df)
    countries = filtered_df['country'].nunique()
    most_common_country = filtered_df['country'].mode().values[0]
    
    summary = dbc.Card(
        dbc.CardBody([
            html.H5("Dashboard Statistics", className='card-title text-primary'),
            html.P(f"Total Characters: {total_characters}", className='card-text'),
            html.P(f"Countries Represented: {countries}", className='card-text'),
            html.P(f"Most Common Country: {most_common_country}", className='card-text')
        ])
    )
    
    return summary

# Boilerplate for cloud deployment
server = app.server