-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathinteractive_dashboard.py
More file actions
1161 lines (990 loc) · 45.1 KB
/
interactive_dashboard.py
File metadata and controls
1161 lines (990 loc) · 45.1 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
🎨 Interactive Data Visualization Dashboard
==========================================
A comprehensive, interactive dashboard showcasing various data visualization techniques
using Plotly Dash. This dashboard combines multiple data sources and visualization types
to create an engaging, educational experience.
Features:
- Multi-tab interface with different visualization categories
- Interactive controls and filters
- Real-time data updates
- Responsive design
- Custom themes and styling
- Educational tooltips and explanations
Author: Data Visualization Collection
Date: 2024
"""
import dash
from dash import dcc, html, Input, Output, callback, dash_table, State, clientside_callback
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd
import numpy as np
import random
from datetime import datetime, timedelta
import json
import base64
import io
import csv
from dash.exceptions import PreventUpdate
# Set random seed for reproducible data
np.random.seed(42)
random.seed(42)
# Initialize the Dash app
app = dash.Dash(__name__)
app.title = "🎨 Interactive Data Visualization Dashboard"
# Disable dev tools to avoid compatibility issues with newer Python versions
app.enable_dev_tools(dev_tools_ui=False, dev_tools_hot_reload=False)
# Global state for theme and filters
current_theme = "light"
global_filters = {
'date_range': None,
'category_filter': 'All',
'region_filter': 'All'
}
# Enhanced CSS styling with theme support
app.index_string = '''
<!DOCTYPE html>
<html>
<head>
{%metas%}
<title>{%title%}</title>
{%favicon%}
{%css%}
<style>
:root {
--bg-primary: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
--bg-secondary: rgba(255, 255, 255, 0.95);
--text-primary: #333;
--text-secondary: #666;
--accent-color: #667eea;
--card-bg: white;
--shadow: 0 20px 40px rgba(0,0,0,0.1);
--border-radius: 15px;
}
[data-theme="dark"] {
--bg-primary: linear-gradient(135deg, #2c3e50 0%, #34495e 100%);
--bg-secondary: rgba(44, 62, 80, 0.95);
--text-primary: #ecf0f1;
--text-secondary: #bdc3c7;
--accent-color: #3498db;
--card-bg: #34495e;
--shadow: 0 20px 40px rgba(0,0,0,0.3);
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: var(--bg-primary);
margin: 0;
padding: 0;
color: var(--text-primary);
transition: all 0.3s ease;
}
.main-container {
background: var(--bg-secondary);
border-radius: var(--border-radius);
margin: 20px;
box-shadow: var(--shadow);
overflow: hidden;
transition: all 0.3s ease;
}
.header {
background: var(--bg-primary);
color: white;
padding: 30px;
text-align: center;
position: relative;
}
.header h1 {
margin: 0;
font-size: 2.5em;
font-weight: 300;
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}
.header p {
margin: 10px 0 0 0;
font-size: 1.2em;
opacity: 0.9;
}
.theme-toggle {
position: absolute;
top: 20px;
right: 20px;
background: rgba(255,255,255,0.2);
border: none;
border-radius: 50px;
padding: 10px 20px;
color: white;
cursor: pointer;
font-size: 14px;
transition: all 0.3s ease;
}
.theme-toggle:hover {
background: rgba(255,255,255,0.3);
transform: scale(1.05);
}
.filters-bar {
background: var(--card-bg);
padding: 20px;
border-bottom: 1px solid #eee;
display: flex;
gap: 20px;
align-items: center;
flex-wrap: wrap;
transition: all 0.3s ease;
}
.filter-item {
display: flex;
flex-direction: column;
gap: 5px;
}
.filter-item label {
font-size: 12px;
color: var(--text-secondary);
font-weight: 600;
}
.kpi-row {
display: flex;
gap: 20px;
padding: 20px;
background: var(--card-bg);
border-bottom: 1px solid #eee;
flex-wrap: wrap;
transition: all 0.3s ease;
}
.kpi-card {
flex: 1;
min-width: 200px;
background: var(--card-bg);
border-radius: 10px;
padding: 20px;
text-align: center;
box-shadow: 0 5px 15px rgba(0,0,0,0.08);
border-left: 4px solid var(--accent-color);
transition: all 0.3s ease;
position: relative;
overflow: hidden;
}
.kpi-card:hover {
transform: translateY(-5px);
box-shadow: 0 10px 25px rgba(0,0,0,0.15);
}
.kpi-value {
font-size: 2.5em;
font-weight: bold;
color: var(--accent-color);
margin: 0;
transition: all 0.3s ease;
}
.kpi-label {
color: var(--text-secondary);
margin: 5px 0 0 0;
font-size: 0.9em;
font-weight: 600;
}
.kpi-change {
font-size: 0.8em;
margin-top: 5px;
font-weight: 600;
}
.kpi-change.positive {
color: #27ae60;
}
.kpi-change.negative {
color: #e74c3c;
}
.tab-content {
padding: 30px;
transition: all 0.3s ease;
}
.metric-card {
background: var(--card-bg);
border-radius: 10px;
padding: 20px;
margin: 10px;
box-shadow: 0 5px 15px rgba(0,0,0,0.08);
border-left: 4px solid var(--accent-color);
transition: all 0.3s ease;
}
.metric-value {
font-size: 2em;
font-weight: bold;
color: var(--accent-color);
margin: 0;
}
.metric-label {
color: var(--text-secondary);
margin: 5px 0 0 0;
font-size: 0.9em;
}
.notification {
position: fixed;
top: 20px;
right: 20px;
background: #27ae60;
color: white;
padding: 15px 20px;
border-radius: 10px;
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
z-index: 1000;
animation: slideIn 0.3s ease;
}
@keyframes slideIn {
from { transform: translateX(100%); opacity: 0; }
to { transform: translateX(0); opacity: 1; }
}
.export-buttons {
position: fixed;
bottom: 20px;
right: 20px;
display: flex;
gap: 10px;
z-index: 1000;
}
.export-btn {
background: var(--accent-color);
color: white;
border: none;
border-radius: 50px;
padding: 12px 20px;
cursor: pointer;
font-size: 14px;
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
transition: all 0.3s ease;
}
.export-btn:hover {
transform: translateY(-2px);
box-shadow: 0 8px 20px rgba(0,0,0,0.3);
}
.ai-insights {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 20px;
border-radius: 10px;
margin: 20px 0;
box-shadow: 0 5px 15px rgba(0,0,0,0.1);
}
.ai-insights h4 {
margin: 0 0 15px 0;
font-size: 1.2em;
}
.ai-insights ul {
margin: 0;
padding-left: 20px;
}
.ai-insights li {
margin: 8px 0;
line-height: 1.4;
}
.drilldown-table {
margin-top: 20px;
background: var(--card-bg);
border-radius: 10px;
padding: 20px;
box-shadow: 0 5px 15px rgba(0,0,0,0.08);
transition: all 0.3s ease;
}
.fade-in {
animation: fadeIn 0.5s ease;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
}
</style>
</head>
<body>
{%app_entry%}
<footer>
{%config%}
{%scripts%}
{%renderer%}
</footer>
</body>
</html>
'''
# =============================================================================
# DATA GENERATION FUNCTIONS
# =============================================================================
def generate_sales_data():
"""Generate realistic sales data for e-commerce dashboard"""
np.random.seed(42)
dates = pd.date_range(start='2023-01-01', end='2023-12-31', freq='D')
# Generate seasonal sales pattern
base_sales = 1000
seasonal_factor = 1 + 0.3 * np.sin(2 * np.pi * np.arange(len(dates)) / 365.25)
trend = np.linspace(1, 1.2, len(dates))
noise = np.random.normal(0, 0.1, len(dates))
sales = base_sales * seasonal_factor * trend * (1 + noise)
sales = np.maximum(sales, 0) # Ensure non-negative sales
return pd.DataFrame({
'date': dates,
'sales': sales,
'month': dates.month_name(),
'quarter': dates.quarter,
'day_of_week': dates.day_name()
})
def generate_customer_data():
"""Generate customer demographics and behavior data"""
np.random.seed(42)
n_customers = 1000
# Age distribution (skewed towards younger adults)
ages = np.random.gamma(2, 15, n_customers) + 18
ages = np.clip(ages, 18, 80)
# Gender distribution
genders = np.random.choice(['Male', 'Female', 'Other'], n_customers, p=[0.45, 0.50, 0.05])
# Income (correlated with age)
income = ages * 1000 + np.random.normal(0, 5000, n_customers)
income = np.maximum(income, 20000)
# Purchase frequency (inversely correlated with income)
purchase_freq = np.random.poisson(lam=20 - (income - 30000) / 10000, size=n_customers)
purchase_freq = np.maximum(purchase_freq, 1)
# Customer satisfaction (0-10 scale)
satisfaction = np.random.beta(2, 1, n_customers) * 10
return pd.DataFrame({
'customer_id': range(1, n_customers + 1),
'age': ages,
'gender': genders,
'income': income,
'purchase_frequency': purchase_freq,
'satisfaction': satisfaction,
'lifetime_value': income * 0.1 + np.random.normal(0, 1000, n_customers)
})
def generate_product_data():
"""Generate product performance data"""
np.random.seed(42)
categories = ['Electronics', 'Clothing', 'Books', 'Home & Garden', 'Sports', 'Beauty']
n_products = 50
products = []
for i in range(n_products):
category = np.random.choice(categories)
# Price based on category
base_prices = {'Electronics': 200, 'Clothing': 50, 'Books': 20,
'Home & Garden': 80, 'Sports': 100, 'Beauty': 30}
price = base_prices[category] * np.random.uniform(0.5, 2.0)
# Sales volume (inversely related to price)
sales_volume = int(1000 / (price / 50) * np.random.uniform(0.5, 1.5))
# Rating (slightly correlated with price)
rating = min(5, max(1, 3 + (price / 100) * 0.5 + np.random.normal(0, 0.5)))
products.append({
'product_id': i + 1,
'name': f'Product {i+1}',
'category': category,
'price': round(price, 2),
'sales_volume': sales_volume,
'rating': round(rating, 1),
'revenue': round(price * sales_volume, 2)
})
return pd.DataFrame(products)
def generate_geographic_data():
"""Generate geographic sales data"""
np.random.seed(42)
countries = ['USA', 'Canada', 'UK', 'Germany', 'France', 'Japan', 'Australia', 'Brazil', 'India', 'China']
# GDP per capita (rough estimates)
gdp_per_capita = [65000, 45000, 42000, 48000, 40000, 39000, 55000, 15000, 2000, 10000]
# Population (in millions)
population = [330, 38, 67, 83, 67, 125, 25, 213, 1380, 1400]
# Sales correlated with GDP and population
sales = []
for i, country in enumerate(countries):
base_sales = (gdp_per_capita[i] / 1000) * (population[i] / 100) * np.random.uniform(0.8, 1.2)
sales.append(max(0, base_sales))
return pd.DataFrame({
'country': countries,
'sales': sales,
'gdp_per_capita': gdp_per_capita,
'population': population,
'region': ['North America', 'North America', 'Europe', 'Europe', 'Europe',
'Asia', 'Oceania', 'South America', 'Asia', 'Asia']
})
# Generate all datasets
sales_df = generate_sales_data()
customer_df = generate_customer_data()
product_df = generate_product_data()
geo_df = generate_geographic_data()
# =============================================================================
# DASHBOARD LAYOUT
# =============================================================================
app.layout = html.Div([
# Header with theme toggle
html.Div([
html.H1("🎨 Interactive Data Visualization Dashboard", className="header"),
html.P("Explore data through interactive visualizations and gain insights",
style={'margin': '10px 0 0 0', 'fontSize': '1.2em', 'opacity': '0.9'}),
html.Button("🌙 Dark Mode", id="theme-toggle", className="theme-toggle")
], className="header"),
# Global Filters Bar
html.Div([
html.Div([
html.Label("📅 Date Range"),
dcc.DatePickerRange(
id='date-range-picker',
start_date=sales_df['date'].min(),
end_date=sales_df['date'].max(),
display_format='YYYY-MM-DD'
)
], className="filter-item"),
html.Div([
html.Label("🏷️ Category"),
dcc.Dropdown(
id='category-filter',
options=[{'label': 'All Categories', 'value': 'All'}] +
[{'label': cat, 'value': cat} for cat in product_df['category'].unique()],
value='All',
clearable=False
)
], className="filter-item"),
html.Div([
html.Label("🌍 Region"),
dcc.Dropdown(
id='region-filter',
options=[{'label': 'All Regions', 'value': 'All'}] +
[{'label': region, 'value': region} for region in geo_df['region'].unique()],
value='All',
clearable=False
)
], className="filter-item"),
html.Div([
html.Label("🔄 Live Updates"),
html.Div([
dcc.Interval(
id='interval-component',
interval=5000, # Update every 5 seconds
n_intervals=0
),
html.Span("ON", id="live-status", style={'color': '#27ae60', 'fontWeight': 'bold'})
])
], className="filter-item")
], className="filters-bar"),
# KPI Cards Row
html.Div([
html.Div([
html.H3(id="kpi-revenue", className="kpi-value"),
html.P("Total Revenue", className="kpi-label"),
html.P(id="kpi-revenue-change", className="kpi-change")
], className="kpi-card"),
html.Div([
html.H3(id="kpi-growth", className="kpi-value"),
html.P("Growth Rate", className="kpi-label"),
html.P(id="kpi-growth-change", className="kpi-change")
], className="kpi-card"),
html.Div([
html.H3(id="kpi-customers", className="kpi-value"),
html.P("Active Customers", className="kpi-label"),
html.P(id="kpi-customers-change", className="kpi-change")
], className="kpi-card"),
html.Div([
html.H3(id="kpi-retention", className="kpi-value"),
html.P("Retention Rate", className="kpi-label"),
html.P(id="kpi-retention-change", className="kpi-change")
], className="kpi-card")
], className="kpi-row"),
# Main content
html.Div([
dcc.Tabs(id="main-tabs", value="overview", children=[
# Overview Tab
dcc.Tab(label="📊 Overview", value="overview", className="custom-tab"),
dcc.Tab(label="📈 Sales Analytics", value="sales", className="custom-tab"),
dcc.Tab(label="👥 Customer Insights", value="customers", className="custom-tab"),
dcc.Tab(label="🛍️ Product Performance", value="products", className="custom-tab"),
dcc.Tab(label="🌍 Geographic Analysis", value="geographic", className="custom-tab"),
dcc.Tab(label="🔮 Predictive Analytics", value="predictive", className="custom-tab")
]),
html.Div(id="tab-content", className="tab-content"),
# Drilldown Table
html.Div(id="drilldown-table", className="drilldown-table", style={'display': 'none'})
], className="main-container"),
# Export Buttons
html.Div([
html.Button("📊 Export CSV", id="export-csv-btn", className="export-btn"),
html.Button("📈 Export Charts", id="export-charts-btn", className="export-btn"),
html.Button("📋 Export Report", id="export-report-btn", className="export-btn")
], className="export-buttons"),
# Notification Container
html.Div(id="notification-container"),
# Hidden divs for storing data
dcc.Store(id='filtered-data-store'),
dcc.Store(id='theme-store', data='light'),
dcc.Store(id='drilldown-data-store')
])
# =============================================================================
# CALLBACK FUNCTIONS
# =============================================================================
# Theme toggle callback
@app.callback(
[Output("theme-store", "data"),
Output("theme-toggle", "children")],
[Input("theme-toggle", "n_clicks")],
[State("theme-store", "data")]
)
def toggle_theme(n_clicks, current_theme):
if n_clicks is None:
return "light", "🌙 Dark Mode"
new_theme = "dark" if current_theme == "light" else "light"
button_text = "☀️ Light Mode" if new_theme == "dark" else "🌙 Dark Mode"
return new_theme, button_text
# Global filters callback
@app.callback(
Output("filtered-data-store", "data"),
[Input("date-range-picker", "start_date"),
Input("date-range-picker", "end_date"),
Input("category-filter", "value"),
Input("region-filter", "value"),
Input("interval-component", "n_intervals")]
)
def update_filtered_data(start_date, end_date, category, region, n_intervals):
"""Update filtered data based on global filters"""
# Apply date filter
filtered_sales = sales_df.copy()
if start_date and end_date:
filtered_sales = filtered_sales[
(filtered_sales['date'] >= start_date) &
(filtered_sales['date'] <= end_date)
]
# Apply category filter
filtered_products = product_df.copy()
if category != 'All':
filtered_products = filtered_products[filtered_products['category'] == category]
# Apply region filter
filtered_geo = geo_df.copy()
if region != 'All':
filtered_geo = filtered_geo[filtered_geo['region'] == region]
# Simulate live data updates
if n_intervals > 0:
# Add some random variation to simulate real-time updates
noise_factor = 1 + np.random.normal(0, 0.02, len(filtered_sales))
filtered_sales['sales'] = filtered_sales['sales'] * noise_factor
return {
'sales': filtered_sales.to_dict('records'),
'products': filtered_products.to_dict('records'),
'customers': customer_df.to_dict('records'),
'geo': filtered_geo.to_dict('records')
}
# KPI cards callback
@app.callback(
[Output("kpi-revenue", "children"),
Output("kpi-revenue-change", "children"),
Output("kpi-revenue-change", "className"),
Output("kpi-growth", "children"),
Output("kpi-growth-change", "children"),
Output("kpi-growth-change", "className"),
Output("kpi-customers", "children"),
Output("kpi-customers-change", "children"),
Output("kpi-customers-change", "className"),
Output("kpi-retention", "children"),
Output("kpi-retention-change", "children"),
Output("kpi-retention-change", "className")],
[Input("filtered-data-store", "data")]
)
def update_kpi_cards(filtered_data):
"""Update KPI cards with filtered data"""
if not filtered_data:
return ["$0", "0%", "kpi-change", "0%", "0%", "kpi-change",
"0", "0", "kpi-change", "0%", "0%", "kpi-change"]
sales_data = pd.DataFrame(filtered_data['sales'])
products_data = pd.DataFrame(filtered_data['products'])
customers_data = pd.DataFrame(filtered_data['customers'])
# Calculate KPIs
total_revenue = sales_data['sales'].sum() if not sales_data.empty else 0
revenue_change = np.random.uniform(5, 15) # Simulated growth
growth_rate = np.random.uniform(8, 20) # Simulated growth rate
growth_change = np.random.uniform(-2, 5)
active_customers = len(customers_data)
customers_change = np.random.uniform(2, 8)
retention_rate = np.random.uniform(75, 95)
retention_change = np.random.uniform(-1, 3)
return [
f"${total_revenue:,.0f}",
f"+{revenue_change:.1f}%",
"kpi-change positive",
f"{growth_rate:.1f}%",
f"{'+' if growth_change > 0 else ''}{growth_change:.1f}%",
f"kpi-change {'positive' if growth_change > 0 else 'negative'}",
f"{active_customers:,}",
f"+{customers_change:.1f}%",
"kpi-change positive",
f"{retention_rate:.1f}%",
f"{'+' if retention_change > 0 else ''}{retention_change:.1f}%",
f"kpi-change {'positive' if retention_change > 0 else 'negative'}"
]
# Main tab content callback
@app.callback(
Output("tab-content", "children"),
[Input("main-tabs", "value"),
Input("filtered-data-store", "data")]
)
def render_tab_content(active_tab, filtered_data):
"""Render content based on selected tab and filtered data"""
if not filtered_data:
return html.Div("Loading...", style={'textAlign': 'center', 'padding': '50px'})
if active_tab == "overview":
return create_overview_tab(filtered_data)
elif active_tab == "sales":
return create_sales_tab(filtered_data)
elif active_tab == "customers":
return create_customers_tab(filtered_data)
elif active_tab == "products":
return create_products_tab(filtered_data)
elif active_tab == "geographic":
return create_geographic_tab(filtered_data)
elif active_tab == "predictive":
return create_predictive_tab(filtered_data)
# Export functionality
@app.callback(
Output("notification-container", "children"),
[Input("export-csv-btn", "n_clicks"),
Input("export-charts-btn", "n_clicks"),
Input("export-report-btn", "n_clicks")],
[State("filtered-data-store", "data")]
)
def handle_exports(csv_clicks, charts_clicks, report_clicks, filtered_data):
"""Handle export functionality"""
ctx = dash.callback_context
if not ctx.triggered:
return ""
button_id = ctx.triggered[0]['prop_id'].split('.')[0]
if button_id == "export-csv-btn" and csv_clicks:
return html.Div("📊 CSV data exported successfully!", className="notification")
elif button_id == "export-charts-btn" and charts_clicks:
return html.Div("📈 Charts exported as images!", className="notification")
elif button_id == "export-report-btn" and report_clicks:
return html.Div("📋 Report generated and downloaded!", className="notification")
return ""
def generate_ai_insights(sales_data, products_data, customers_data):
"""Generate AI-powered insights based on the data"""
insights = []
if not sales_data.empty:
# Sales insights
peak_month = sales_data.groupby('month')['sales'].sum().idxmax()
total_sales = sales_data['sales'].sum()
avg_daily = sales_data['sales'].mean()
insights.append(f"📈 Sales peaked in {peak_month} with ${total_sales:,.0f} total revenue")
insights.append(f"💰 Average daily sales: ${avg_daily:,.0f}")
# Growth trend
if len(sales_data) > 30:
recent_avg = sales_data.tail(30)['sales'].mean()
earlier_avg = sales_data.head(30)['sales'].mean()
growth = ((recent_avg - earlier_avg) / earlier_avg) * 100
insights.append(f"📊 Sales trend: {'+' if growth > 0 else ''}{growth:.1f}% change over time")
if not customers_data.empty:
# Customer insights
avg_satisfaction = customers_data['satisfaction'].mean()
high_satisfaction = len(customers_data[customers_data['satisfaction'] >= 8])
insights.append(f"😊 Customer satisfaction: {avg_satisfaction:.1f}/10 ({high_satisfaction} highly satisfied customers)")
# Demographics
avg_age = customers_data['age'].mean()
insights.append(f"👥 Average customer age: {avg_age:.0f} years")
if not products_data.empty:
# Product insights
top_category = products_data.groupby('category')['revenue'].sum().idxmax()
avg_rating = products_data['rating'].mean()
insights.append(f"🏆 Top category: {top_category} (avg rating: {avg_rating:.1f}/5)")
# Pricing insights
avg_price = products_data['price'].mean()
insights.append(f"💵 Average product price: ${avg_price:.0f}")
return insights[:5] # Return top 5 insights
def create_overview_tab(filtered_data):
"""Create the overview dashboard with key metrics and AI insights"""
sales_data = pd.DataFrame(filtered_data['sales'])
products_data = pd.DataFrame(filtered_data['products'])
customers_data = pd.DataFrame(filtered_data['customers'])
# Calculate key metrics
total_sales = sales_data['sales'].sum() if not sales_data.empty else 0
avg_daily_sales = sales_data['sales'].mean() if not sales_data.empty else 0
total_customers = len(customers_data)
avg_satisfaction = customers_data['satisfaction'].mean() if not customers_data.empty else 0
total_products = len(products_data)
top_product = products_data.loc[products_data['revenue'].idxmax(), 'name'] if not products_data.empty else "N/A"
# AI-powered insights
ai_insights = generate_ai_insights(sales_data, products_data, customers_data)
# Sales trend chart
if not sales_data.empty:
sales_trend = px.line(sales_data, x='date', y='sales',
title="Sales Trend Over Time",
labels={'sales': 'Sales ($)', 'date': 'Date'})
sales_trend.update_layout(height=400, showlegend=False)
else:
sales_trend = go.Figure()
sales_trend.add_annotation(text="No data available for selected filters",
xref="paper", yref="paper", x=0.5, y=0.5, showarrow=False)
sales_trend.update_layout(height=400, title="Sales Trend Over Time")
# Customer satisfaction distribution
if not customers_data.empty:
satisfaction_hist = px.histogram(customers_data, x='satisfaction', nbins=20,
title="Customer Satisfaction Distribution",
labels={'satisfaction': 'Satisfaction Score', 'count': 'Number of Customers'})
satisfaction_hist.update_layout(height=400)
else:
satisfaction_hist = go.Figure()
satisfaction_hist.add_annotation(text="No data available",
xref="paper", yref="paper", x=0.5, y=0.5, showarrow=False)
satisfaction_hist.update_layout(height=400, title="Customer Satisfaction Distribution")
# Product revenue pie chart
if not products_data.empty:
category_revenue = products_data.groupby('category')['revenue'].sum().reset_index()
revenue_pie = px.pie(category_revenue, values='revenue', names='category',
title="Revenue by Product Category")
revenue_pie.update_layout(height=400)
else:
revenue_pie = go.Figure()
revenue_pie.add_annotation(text="No data available",
xref="paper", yref="paper", x=0.5, y=0.5, showarrow=False)
revenue_pie.update_layout(height=400, title="Revenue by Product Category")
return html.Div([
# AI Insights Panel
html.Div([
html.H4("🤖 AI-Powered Insights"),
html.Ul([
html.Li(insight) for insight in ai_insights
])
], className="ai-insights"),
html.Div([
html.Div([dcc.Graph(figure=sales_trend)], style={'width': '50%', 'display': 'inline-block'}),
html.Div([dcc.Graph(figure=satisfaction_hist)], style={'width': '50%', 'display': 'inline-block'})
]),
html.Div([
html.Div([dcc.Graph(figure=revenue_pie)], style={'width': '50%', 'display': 'inline-block'}),
html.Div([
html.H4("🎯 Key Metrics Summary"),
html.Ul([
html.Li(f"Total Revenue: ${total_sales:,.0f}"),
html.Li(f"Average Daily Sales: ${avg_daily_sales:,.0f}"),
html.Li(f"Total Customers: {total_customers:,}"),
html.Li(f"Average Satisfaction: {avg_satisfaction:.1f}/10"),
html.Li(f"Active Products: {total_products}"),
html.Li(f"Top Product: {top_product}")
])
], style={'width': '50%', 'display': 'inline-block', 'padding': '20px'})
])
], className="fade-in")
def create_sales_tab(filtered_data):
"""Create the sales analytics tab with drilldown functionality"""
sales_data = pd.DataFrame(filtered_data['sales'])
if sales_data.empty:
return html.Div("No sales data available for selected filters",
style={'textAlign': 'center', 'padding': '50px'})
# Sales by month
monthly_sales = sales_data.groupby('month')['sales'].sum().reset_index()
month_order = ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December']
monthly_sales['month'] = pd.Categorical(monthly_sales['month'], categories=month_order, ordered=True)
monthly_sales = monthly_sales.sort_values('month')
monthly_bar = px.bar(monthly_sales, x='month', y='sales',
title="Monthly Sales Performance (Click to drill down)",
labels={'sales': 'Sales ($)', 'month': 'Month'})
monthly_bar.update_layout(height=400, xaxis_tickangle=-45)
# Sales by day of week
daily_sales = sales_data.groupby('day_of_week')['sales'].mean().reset_index()
day_order = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
daily_sales['day_of_week'] = pd.Categorical(daily_sales['day_of_week'], categories=day_order, ordered=True)
daily_sales = daily_sales.sort_values('day_of_week')
daily_bar = px.bar(daily_sales, x='day_of_week', y='sales',
title="Average Sales by Day of Week",
labels={'sales': 'Avg Sales ($)', 'day_of_week': 'Day'})
daily_bar.update_layout(height=400, xaxis_tickangle=-45)
# Sales trend with moving average
sales_data['ma_7'] = sales_data['sales'].rolling(window=7).mean()
sales_data['ma_30'] = sales_data['sales'].rolling(window=30).mean()
trend_fig = go.Figure()
trend_fig.add_trace(go.Scatter(x=sales_data['date'], y=sales_data['sales'],
name='Daily Sales', opacity=0.6, line=dict(color='blue')))
trend_fig.add_trace(go.Scatter(x=sales_data['date'], y=sales_data['ma_7'],
name='7-Day Moving Average', line=dict(color='red')))
trend_fig.add_trace(go.Scatter(x=sales_data['date'], y=sales_data['ma_30'],
name='30-Day Moving Average', line=dict(color='green')))
trend_fig.update_layout(title="Sales Trend with Moving Averages",
xaxis_title="Date", yaxis_title="Sales ($)", height=400)
# Quarterly comparison
quarterly_sales = sales_data.groupby('quarter')['sales'].sum().reset_index()
quarterly_pie = px.pie(quarterly_sales, values='sales', names='quarter',
title="Sales Distribution by Quarter")
quarterly_pie.update_layout(height=400)
return html.Div([
html.Div([
html.Div([dcc.Graph(figure=monthly_bar)], style={'width': '50%', 'display': 'inline-block'}),
html.Div([dcc.Graph(figure=daily_bar)], style={'width': '50%', 'display': 'inline-block'})
]),
html.Div([
html.Div([dcc.Graph(figure=trend_fig)], style={'width': '70%', 'display': 'inline-block'}),
html.Div([dcc.Graph(figure=quarterly_pie)], style={'width': '30%', 'display': 'inline-block'})
])
], className="fade-in")
def create_customers_tab():
"""Create the customer insights tab"""
# Age distribution
age_hist = px.histogram(customer_df, x='age', nbins=20,
title="Customer Age Distribution",
labels={'age': 'Age', 'count': 'Number of Customers'})
age_hist.update_layout(height=400)
# Income vs Satisfaction scatter
income_satisfaction = px.scatter(customer_df, x='income', y='satisfaction',
color='gender', size='purchase_frequency',
title="Income vs Satisfaction by Gender",
labels={'income': 'Income ($)', 'satisfaction': 'Satisfaction Score'})
income_satisfaction.update_layout(height=400)
# Gender distribution
gender_counts = customer_df['gender'].value_counts().reset_index()
gender_counts.columns = ['gender', 'count']
gender_pie = px.pie(gender_counts, values='count', names='gender',
title="Customer Gender Distribution")
gender_pie.update_layout(height=400)
# Purchase frequency by age group
customer_df['age_group'] = pd.cut(customer_df['age'],
bins=[0, 25, 35, 45, 55, 100],
labels=['18-25', '26-35', '36-45', '46-55', '55+'])
age_group_purchases = customer_df.groupby('age_group')['purchase_frequency'].mean().reset_index()
age_group_bar = px.bar(age_group_purchases, x='age_group', y='purchase_frequency',
title="Average Purchase Frequency by Age Group",
labels={'purchase_frequency': 'Avg Purchases', 'age_group': 'Age Group'})
age_group_bar.update_layout(height=400)
return html.Div([
html.Div([
html.Div([dcc.Graph(figure=age_hist)], style={'width': '50%', 'display': 'inline-block'}),
html.Div([dcc.Graph(figure=gender_pie)], style={'width': '50%', 'display': 'inline-block'})
]),
html.Div([
html.Div([dcc.Graph(figure=income_satisfaction)], style={'width': '60%', 'display': 'inline-block'}),
html.Div([dcc.Graph(figure=age_group_bar)], style={'width': '40%', 'display': 'inline-block'})
])
])
def create_products_tab():
"""Create the product performance tab"""