Py.Cafe

acabrera.citizens/

himalay_explorer

DocsPricing
  • assets/
  • app.py
  • himalaya_challenge_last_version.py
  • mountains_with_10_plus_visits.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
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
import dash
from dash import dcc, html, Input, Output, State, callback
import dash_bootstrap_components as dbc
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline

# Load the dataset
def load_expedition_data():
    # Load the complete dataset here
    return pd.read_csv('mountains_with_10_plus_visits.csv', low_memory=False)

df = load_expedition_data()

# Train the machine learning model
def train_prediction_model(dataframe):
    # Select relevant features for predicting success
    features = ['year', 'season', 'heightm', 'camps', 'totmembers',
                'o2used', 'tothired', 'rope']

    # Prepare X and y
    X = dataframe[features]
    y = dataframe['success1']  # Predict success on route 1

    # Split into train and test sets
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)

    # Preprocessing
    numeric_features = ['year', 'heightm', 'camps', 'totmembers', 'tothired', 'rope']
    categorical_features = ['season', 'o2used']

    numeric_transformer = Pipeline(steps=[
        ('scaler', StandardScaler())
    ])

    categorical_transformer = Pipeline(steps=[
        ('onehot', OneHotEncoder(handle_unknown='ignore'))
    ])

    preprocessor = ColumnTransformer(
        transformers=[
            ('num', numeric_transformer, numeric_features),
            ('cat', categorical_transformer, categorical_features)
        ])

    # Create and train the complete pipeline
    model = Pipeline(steps=[
        ('preprocessor', preprocessor),
        ('classifier', RandomForestClassifier(n_estimators=100, random_state=42))
    ])

    model.fit(X_train, y_train)

    return model

# Train the model
prediction_model = train_prediction_model(df)

# Enhanced nature-inspired theme colors with gradients
COLORS = {
    'primary': '#1e3a8a',        # Deep Mountain Blue
    'secondary': '#059669',      # Evergreen
    'accent': '#f59e0b',         # Golden Sunrise
    'dark': '#0f172a',           # Night Sky
    'light': '#f8fafc',          # Snow White
    'danger': '#dc2626',         # Alert Red
    'success': '#10b981',        # Summit Green
    'ice': '#0ea5e9',           # Glacier Blue
    'rock': '#78716c',          # Stone Grey
    'cloud': '#e2e8f0',         # Cloud Grey
    'text': '#1e293b',          # Deep Text
    'text_light': '#f8fafc',    # NUEVO: Texto claro para fondos oscuros
    'text_muted': '#64748b',    # NUEVO: Texto secundario
    'gradient_primary': 'linear-gradient(135deg, #1e3a8a, #3b82f6)',
    'gradient_success': 'linear-gradient(135deg, #059669, #10b981)',
    'gradient_warning': 'linear-gradient(135deg, #f59e0b, #fbbf24)'
}

# Custom CSS for the enhanced nature theme
external_stylesheets = [
    dbc.themes.MINTY,
    dbc.icons.FONT_AWESOME,
    'https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap', 'style1.css'
]

# Configure the app with bootstrap
app = dash.Dash(
    __name__, 
    external_stylesheets=external_stylesheets,
    suppress_callback_exceptions=True,
    meta_tags=[{"name": "viewport", "content": "width=device-width, initial-scale=1"}]
)

server = app.server
app.title = "๐Ÿ”๏ธ Himalaya Explorer - Your Summit Journey Begins Here"

# Enhanced app layout with compelling narrative
app.layout = dbc.Container([
    # Hero Section with Compelling Narrative
    html.Div([
        html.H1([
            "๐Ÿ”๏ธ HIMALAYA EXPLORER",
            html.Br(),
            html.Small("Your Journey to the World's Highest Peaks Starts Here", 
                      style={"fontSize": "1.2rem", "opacity": "0.9"})
        ], className="display-3 text-center mb-3 fw-bold"),
        html.P([
            "๐ŸŒŸ Discover the secrets of successful expeditions โ€ข ๐Ÿ“Š Analyze decades of climbing data โ€ข ",
            "๐Ÿค– Predict your summit chances with AI โ€ข โ›ท๏ธ Plan safer adventures"
        ], className="lead text-center mb-0", style={"fontSize": "1.1rem"})
    ], className="hero-section"),
    
    # Smart Expedition Filters
    html.Div(id='global-filters-container', children=[
        dbc.Card([
            dbc.CardHeader([
                html.I(className="fas fa-filter section-icon"),
                "๐ŸŽฏ Smart Expedition Filters - Customize Your Exploration"
            ]),
            dbc.CardBody([
                html.P("โœจ Select your parameters to explore targeted insights from over 10,000+ expedition records",
                       className="text-muted mb-3"),
                dbc.Row([
                    dbc.Col([
                        html.Label([html.I(className="fas fa-mountain me-2"), "๐Ÿ”๏ธ Choose Your Peaks (max 3):"]),
                        dcc.Dropdown(
                            id='global-peak-dropdown',
                            options=[{'label': f"๐Ÿ”๏ธ {peak}", 'value': peak} for peak in sorted(df['pkname'].unique())],
                            value=[sorted(df['pkname'].unique())[0]],
                            multi=True, placeholder="๐Ÿ” Search and select peaks...",
                            className="mb-3", style={"borderRadius": "10px"}
                        ),
                    ], md=4),
                    dbc.Col([
                        html.Label([html.I(className="fas fa-snowflake me-2"), "๐ŸŒจ๏ธ Season:"]),
                        dcc.Dropdown(
                            id='global-season-dropdown',
                            options=[{'label': '๐ŸŒ All Seasons', 'value': 'all'}] +
                                    [{'label': f"{'๐ŸŒธ' if season=='Spring' else 'โ˜€๏ธ' if season=='Summer' else '๐Ÿ‚' if season=='Autumn' else 'โ„๏ธ'} {season}", 'value': season}
                                     for season in sorted(df['season'].unique())],
                            value='all', className="mb-3"
                        ),
                    ], md=3),
                    dbc.Col([
                        html.Label([html.I(className="fas fa-calendar me-2"), "๐Ÿ“… Time Period:"]),
                        dcc.RangeSlider(
                            id='global-year-slider',
                            min=df['year'].min(), max=df['year'].max(), step=1,
                            marks={i: str(i) for i in range(df['year'].min(), df['year'].max() + 1, 10)},
                            value=[df['year'].min(), df['year'].max()], className="mb-3"
                        ),
                    ], md=5),
                ])
            ])
        ], className="filter-card mb-4", style={'display': 'block'}) 
    ]), 
    
    # Navigation with Adventure Story
    dbc.Row([
        dbc.Col([
            html.H3("๐Ÿ“– Choose Your Adventure Story", className="text-center mb-3"),
            dbc.Nav([
                dbc.NavItem(dbc.NavLink([
                    html.I(className="fas fa-mountain me-2"), "๐Ÿ”๏ธ Peak Legends"
                ], id="btn-peaks", active=True, className="mx-1")),
                dbc.NavItem(dbc.NavLink([
                    html.I(className="fas fa-shield-alt me-2"), "โš ๏ธ Safety Intel"
                ], id="btn-safety", className="mx-1")),
                dbc.NavItem(dbc.NavLink([
                    html.I(className="fas fa-brain me-2"), "๐Ÿค– AI Summit Predictor"
                ], id="btn-ml", className="mx-1")),
            ], pills=True, fill=True, className="mb-4")
        ])
    ]),
    
    # Dynamic Content Area
    html.Div(id='page-content')
], fluid=True, className="px-4")

# Navigation callback with enhanced storytelling
@app.callback(
    [Output('page-content', 'children'),
     Output('btn-peaks', 'active'),
     Output('btn-safety', 'active'),
     Output('btn-ml', 'active'),
     Output('global-filters-container', 'style')],
    [Input('btn-peaks', 'n_clicks'),
     Input('btn-safety', 'n_clicks'),
     Input('btn-ml', 'n_clicks')]
)
def render_content(peaks_clicks, safety_clicks, ml_clicks):
    ctx = dash.callback_context
    button_id = ctx.triggered[0]['prop_id'].split('.')[0] if ctx.triggered else ''
    button_states = [False, False, False]
    global_filters_style = {'display': 'block'}  # Estilo por defecto: visible
    
    if button_id == 'btn-peaks' or not button_id:
        button_states[0] = True
        return render_peaks_tab(), *button_states, global_filters_style
    elif button_id == 'btn-safety':
        button_states[1] = True
        return render_safety_tab(), *button_states, global_filters_style
    elif button_id == 'btn-ml':
        button_states[2] = True
        global_filters_style = {'display': 'none'}  # OCULTAR FILTROS GLOBALES
        return render_ml_tab(), *button_states, global_filters_style

# Enhanced Peak Profiles with storytelling
def render_peaks_tab():
    return dbc.Card([
        dbc.CardHeader([
            html.I(className="fas fa-mountain section-icon"),
            "๐Ÿ”๏ธ Legendary Peaks - Where Dreams Meet Reality"
        ]),
        dbc.CardBody([
            dbc.Alert([
                html.I(className="fas fa-info-circle me-2"),
                "๐Ÿ“š Dive into the rich tapestry of Himalayan climbing history. Each peak tells a story of human courage, determination, and the eternal quest to touch the sky."
            ], color="info", className="mb-4"),
            html.Div(id='peak-info-container')
        ])
    ])

# Enhanced callback for peak information with better storytelling
@app.callback(
    Output('peak-info-container', 'children'),
    [Input('global-peak-dropdown', 'value'),
     Input('global-season-dropdown', 'value'),
     Input('global-year-slider', 'value')]
)
def update_peak_info(selected_peaks, selected_season, year_range):
    if not selected_peaks or len(selected_peaks) == 0:
        return dbc.Alert("๐Ÿ” Please select at least one peak to begin your exploration", color="warning")
    
    selected_peaks = selected_peaks[:3]
    filtered_df = df.copy()
    filtered_df = filtered_df[(filtered_df['year'] >= year_range[0]) & (filtered_df['year'] <= year_range[1])]
    
    if selected_season != 'all':
        filtered_df = filtered_df[filtered_df['season'] == selected_season]
    
    peak_expeditions = filtered_df[filtered_df['pkname'].isin(selected_peaks)]
    
    if len(peak_expeditions) == 0:
        return dbc.Alert("๐Ÿ“Š No expedition data found for your selected criteria. Try adjusting your filters!", color="warning")
    
    # Enhanced expedition timeline
    expeditions_by_year = peak_expeditions.groupby(['year', 'pkname']).size().reset_index(name='count')
    fig_timeline = px.area(expeditions_by_year, x='year', y='count', color='pkname',
                        title='๐Ÿ“ˆ The Epic Journey Through Time - Expeditions per Year',
                        markers=True, template='plotly_white')
    
    fig_timeline.update_layout(
        plot_bgcolor='rgba(0,0,0,0)', paper_bgcolor='rgba(0,0,0,0)',
        font={'color': COLORS['text'], 'family': 'Inter'}, height=450,
        title_font_size=16, legend_title="๐Ÿ”๏ธ Peak Name"
    )
    
    # Success rate visualization with better narrative
    success_by_season = peak_expeditions.groupby(['season', 'pkname'])['success1'].mean().reset_index()
    fig_success = px.bar(success_by_season, x='season', y='success1', color='pkname',
                        title='๐ŸŽฏ Success Stories by Season - When Dreams Come True',
                        labels={'success1': '๐Ÿ† Success Rate', 'season': '๐ŸŒฆ๏ธ Season'},
                        barmode='group', template='plotly_white')
    
    fig_success.update_layout(
        plot_bgcolor='rgba(0,0,0,0)', paper_bgcolor='rgba(0,0,0,0)',
        font={'color': COLORS['text'], 'family': 'Inter'}, height=450,
        title_font_size=16, legend_title="๐Ÿ”๏ธ Peak Name"
    )
   
    # Enhanced peaks information table with expandable routes
    peak_info_rows = []
    for idx, selected_peak in enumerate(selected_peaks):
        current_peak_data = filtered_df[filtered_df['pkname'] == selected_peak]
        if len(current_peak_data) == 0:
            continue
            
        peak_data = current_peak_data.iloc[0]
        first_ascent_year = peak_data.get('pyear', 'Unknown')
        first_ascent_team = peak_data.get('pcountry', 'Unknown')
        first_summiters = peak_data.get('psummiters', 'Unknown')
        height = peak_data.get('heightm', 'Unknown')
        location = peak_data.get('location', 'Unknown')
        
        # Get routes for this specific peak
        peak_routes = current_peak_data['route1'].dropna().unique()
        routes_formatted = [route for route in peak_routes if route and str(route).strip()]
        
        # Create routes dropdown content
        routes_content = html.Div([
            dbc.Button([
                html.I(className="fas fa-route me-2"),
                f"View Routes ({len(routes_formatted)})",
                html.I(className="fas fa-caret-down ms-2")
            ], 
            id=f"routes-btn-{idx}",
            color="outline-primary",
            size="sm",
            style={"width": "100%", "fontSize": "0.8rem"}),
            
            dbc.Collapse([
                html.Div([
                    dbc.ListGroup([
                        dbc.ListGroupItem([
                            html.I(className="fas fa-route me-2", style={"color": "#059669"}),
                            route
                        ], style={"fontSize": "0.85rem", "padding": "0.4rem 0.8rem", "border": "none"})
                        for route in routes_formatted
                    ], flush=True) if routes_formatted else html.P("No routes available", 
                        className="text-muted mb-0", style={"fontSize": "0.8rem", "padding": "0.5rem"})
                ], style={"maxHeight": "150px", "overflowY": "auto", "backgroundColor": "#f8fafc", 
                         "border": "1px solid #e2e8f0", "borderRadius": "0.375rem", "marginTop": "0.5rem"})
            ], id=f"routes-collapse-{idx}", is_open=False)
        ])
        
        peak_info_rows.append(html.Tr([
            html.Td([html.I(className="fas fa-mountain me-2"), selected_peak]),
            html.Td([html.I(className="fas fa-ruler-vertical me-2"), f"{height} m"]),
            html.Td([html.I(className="fas fa-map-marker-alt me-2"), location]),
            html.Td([html.I(className="fas fa-calendar me-2"), f"{first_ascent_year}"]),
            html.Td([html.I(className="fas fa-flag me-2"), f"{first_summiters} ({first_ascent_team})"]),
            html.Td(routes_content, style={"minWidth": "200px"})
        ]))

    peaks_table = dbc.Table([
        html.Thead(html.Tr([
            html.Th("๐Ÿ”๏ธ Peak Name"),
            html.Th("๐Ÿ“ Height"),
            html.Th("๐Ÿ“ Location"),
            html.Th("๐ŸŽฏ First Ascent"),
            html.Th("๐Ÿ† Pioneers"),
            html.Th("๐Ÿ›ค๏ธ Epic Routes", style={"minWidth": "200px"})
        ])),
        html.Tbody(peak_info_rows)
    ], bordered=True, hover=True, striped=True, className="mb-4")
    
    return dbc.Card([
        dbc.CardHeader([
            html.I(className="fas fa-chart-line me-2"),
            "๐Ÿ“Š Peak Performance Analytics"
        ]),
        dbc.CardBody([
            html.H5("๐Ÿ”๏ธ Selected Peaks Hall of Fame", className="mb-3"),
            peaks_table,
            
            dbc.Row([
                dbc.Col([dcc.Graph(figure=fig_timeline, className="mb-4")], md=12)
            ]),
            
            dbc.Row([
                dbc.Col([dcc.Graph(figure=fig_success, className="mb-4")], md=12)
            ])
        ])
    ])

# Peak selection limiter
@app.callback(
    Output('global-peak-dropdown', 'value'),
    Input('global-peak-dropdown', 'value')
)
def limit_peaks_selection(selected_peaks):
    if selected_peaks and len(selected_peaks) > 5:
        return selected_peaks[:5]
    return selected_peaks

# Enhanced Safety Analysis with dramatic storytelling
def render_safety_tab():
    return dbc.Card([
        dbc.CardHeader([
            html.I(className="fas fa-shield-alt section-icon"),
            "โš ๏ธ Mountain Safety Intelligence - Knowledge That Saves Lives"
        ]),
        dbc.CardBody([
            dbc.Alert([
                html.I(className="fas fa-exclamation-triangle me-2"),
                "๐Ÿšจ Understanding risk is the first step to managing it. These insights are drawn from decades of expedition data to help you make informed decisions in the world's most challenging environment."
            ], color="warning", className="mb-4"),
            html.Div(id='safety-analysis', className='mt-4')
        ])
    ])

# Enhanced safety analysis callback
@app.callback(
    Output('safety-analysis', 'children'),
    [Input('global-year-slider', 'value'),
     Input('global-peak-dropdown', 'value'),
     Input('global-season-dropdown', 'value')]
)
def update_safety_analysis(years, peaks, season):
    filtered_df = df.copy()
    filtered_df = filtered_df[(filtered_df['year'] >= years[0]) & (filtered_df['year'] <= years[1])]

    if peaks and len(peaks) > 0:
        filtered_df = filtered_df[filtered_df['pkname'].isin(peaks)]

    if season != 'all':
        filtered_df = filtered_df[filtered_df['season'] == season]

    total_expeditions = len(filtered_df)
    
    if total_expeditions == 0:
        return dbc.Alert("๐Ÿ“Š No safety data available for selected parameters", color="warning")
    
    total_members = filtered_df['totmembers'].sum()
    total_deaths = filtered_df['mdeaths'].sum()
    death_rate = (total_deaths / total_members * 100) if total_members > 0 else 0

    yearly_stats = filtered_df.groupby('year').agg({
        'mdeaths': 'sum', 'totmembers': 'sum'
    }).reset_index()
    yearly_stats['mortality_rate'] = yearly_stats['mdeaths'] / yearly_stats['totmembers'] * 100

    recent_mortality = yearly_stats[yearly_stats['year'] >= 2010]['mortality_rate'].mean() if not yearly_stats.empty else 0
    older_mortality = yearly_stats[yearly_stats['year'] < 2010]['mortality_rate'].mean() if not yearly_stats.empty else 0

    trend_emoji = "๐Ÿ“ˆ" if recent_mortality > older_mortality else "๐Ÿ“‰"
    trend_text = "increased" if recent_mortality > older_mortality else "decreased"

    # Enhanced mortality trend visualization
    if not yearly_stats.empty:
        fig_mortality_trend = px.line(yearly_stats, x='year', y='mortality_rate',
                                     title='โš ๏ธ Risk Evolution Over Time - Mortality Rate Trends',
                                     labels={'mortality_rate': '๐Ÿ’€ Mortality Rate (%)', 'year': '๐Ÿ“… Year'},
                                     markers=True, template='plotly_white')
        
        fig_mortality_trend.update_traces(
            line=dict(color=COLORS['danger'], width=3),
            marker=dict(color=COLORS['danger'], size=8)
        )
        fig_mortality_trend.update_layout(
            plot_bgcolor='rgba(0,0,0,0)', paper_bgcolor='rgba(0,0,0,0)',
            font={'color': COLORS['text'], 'family': 'Inter'}, height=400
        )
    else:
        fig_mortality_trend = go.Figure()
        fig_mortality_trend.update_layout(title='๐Ÿ“Š Insufficient data for trend analysis')

    # Peak comparison if multiple peaks selected
    peak_comparison = None
    if peaks and len(peaks) > 1:
        peak_mortality = filtered_df.groupby('pkname').agg({
            'mdeaths': 'sum', 'totmembers': 'sum'
        }).reset_index()
        peak_mortality['mortality_rate'] = peak_mortality['mdeaths'] / peak_mortality['totmembers'] * 100
        
        fig_peak_comparison = px.bar(peak_mortality, x='pkname', y='mortality_rate',
                                   title='โš–๏ธ Risk Comparison Across Peaks',
                                   labels={'mortality_rate': '๐Ÿ’€ Mortality Rate (%)', 'pkname': '๐Ÿ”๏ธ Peak'},
                                   template='plotly_white')
        
        fig_peak_comparison.update_traces(marker_color=COLORS['danger'])
        fig_peak_comparison.update_layout(
            plot_bgcolor='rgba(0,0,0,0)', paper_bgcolor='rgba(0,0,0,0)',
            font={'color': COLORS['text'], 'family': 'Inter'}, height=400
        )
        
        peak_comparison = dbc.Row([
            dbc.Col([dcc.Graph(figure=fig_peak_comparison)], md=12)
        ], className="mb-4")

    return dbc.Row([
        dbc.Col([
            # Critical stats overview
            dbc.Row([
                dbc.Col([
                    html.Div([
                        html.Div("๐Ÿ’€", className="metric-icon"),
                        html.H2(f"{death_rate:.2f}%", className="display-4 mb-0"),
                        html.P("Overall Risk Rate", className="mb-0", #style={"color": "#f8fafc"}
                              )
                    ], className="danger-card stats-card")
                ], md=4),
                dbc.Col([
                    html.Div([
                        html.Div("๐Ÿ‘ฅ", className="metric-icon"),
                        html.H2(f"{total_members:,}", className="display-4 mb-0"),
                        html.P("Brave Climbers", className="mb-0")
                    ], className="stats-card")
                ], md=4),
                dbc.Col([
                    html.Div([
                        html.Div(trend_emoji, className="metric-icon"),
                        html.H2(f"{abs(recent_mortality - older_mortality):.1f}%", className="display-4 mb-0"),
                        html.P(f"Safety {trend_text.title()}", className="mb-0")
                    ], className="success-card stats-card" if trend_text == "decreased" else "danger-card stats-card")
                ], md=4)
            ], className="mb-4"),
            
            # Detailed analysis
            dbc.Card([
                dbc.CardHeader("๐Ÿ“Š Comprehensive Risk Analysis"),
                dbc.CardBody([
                    html.P([
                        f"๐Ÿ“ˆ Safety trends show that mountaineering risks have {trend_text} over time. ",
                        f"Recent expeditions (2010+) show a {recent_mortality:.2f}% risk rate compared to ",
                        f"{older_mortality:.2f}% in earlier periods - a testament to improved equipment, ",
                        "training, and safety protocols."
                    ], className="mb-4"),
                    
                    peak_comparison if peak_comparison is not None else html.Div(),
                    
                    dcc.Graph(figure=fig_mortality_trend, className="mb-4"),
                    
                    dbc.Row([
                        dbc.Col([
                            dbc.Card([
                                dbc.CardHeader("โš ๏ธ Primary Risk Factors"),
                                dbc.CardBody([
                                    dbc.ListGroup([
                                        dbc.ListGroupItem([
                                            html.I(className="fas fa-thermometer-empty me-2"),
                                            "๐ŸŒก๏ธ Extreme weather and exposure"
                                        ]),
                                        dbc.ListGroupItem([
                                            html.I(className="fas fa-mountain me-2"),
                                            "๐Ÿชจ Avalanches and rockfall"
                                        ]),
                                        dbc.ListGroupItem([
                                            html.I(className="fas fa-lungs me-2"),
                                            "๐Ÿซ Altitude sickness complications"
                                        ]),
                                        dbc.ListGroupItem([
                                            html.I(className="fas fa-battery-empty me-2"),
                                            "๐Ÿ’ช Physical exhaustion"
                                        ])
                                    ])
                                ])
                            ])
                        ], md=6),
                        
                        dbc.Col([
                            dbc.Alert([
                                html.I(className="fas fa-lightbulb me-2"),
                                html.Strong("๐Ÿ’ก Safety Wisdom: "),
                                "The mountains don't care about your summit dreams - they only respect your preparation, ",
                                "judgment, and willingness to turn back when conditions demand it. Every successful ",
                                "climber is one who chose to climb another day."
                            ], color="info")
                        ], md=6)
                    ])
                ])
            ])
        ])
    ])

# Enhanced ML Prediction Tab with interactive features
def render_ml_tab():
    return dbc.Card([
        dbc.CardHeader([
            html.I(className="fas fa-brain section-icon"),
            "๐Ÿค– AI Summit Predictor - Will You Reach the Top?"
        ]),
        dbc.CardBody([
            dbc.Alert([
                html.I(className="fas fa-robot me-2"),
                "๐Ÿง  Our AI has analyzed thousands of expeditions to predict your chances of success. Input your expedition parameters below and discover what the data reveals about your summit prospects!"
            ], color="success", className="mb-4"),
            
            dbc.Row([
                dbc.Col([
                    dbc.Card([
                        dbc.CardHeader("๐ŸŽฏ Configure Your Expedition"),
                        dbc.CardBody([
                            html.Div([
                                html.Label([html.I(className="fas fa-mountain me-2"), "๐Ÿ”๏ธ Target Peak:"]),
                                dcc.Dropdown(
                                    id='ml-peak-dropdown',
                                    options=[{'label': f"๐Ÿ”๏ธ {peak}", 'value': peak} for peak in sorted(df['pkname'].unique())],
                                    value=sorted(df['pkname'].unique())[0],
                                    className="mb-3"
                                ),
                                html.Label([html.I(className="fas fa-calendar me-2"), "๐Ÿ“… Expedition Year:"]),
                                dbc.Input(
                                    id='ml-year-input',
                                    type='number',
                                    min=2025, max=2034, step=1,
                                    value=2025,
                                    className="mb-3"
                                ),
                                
                                html.Label([html.I(className="fas fa-snowflake me-2"), "๐ŸŒจ๏ธ Season:"]),
                                dcc.Dropdown(
                                    id='ml-season-dropdown',
                                    options=[
                                        {'label': '๐ŸŒธ Spring', 'value': 'Spring'},
                                        {'label': 'โ˜€๏ธ Summer', 'value': 'Summer'},
                                        {'label': '๐Ÿ‚ Autumn', 'value': 'Autumn'},
                                        {'label': 'โ„๏ธ Winter', 'value': 'Winter'}
                                    ],
                                    value='Spring', className="mb-3"
                                ),
                                html.Label([html.I(className="fas fa-users me-2"), "๐Ÿ‘ฅ Team Size:"]),
                                dbc.Input(
                                    id='ml-members-input',
                                    type='number',
                                    min=1, max=20, step=1,
                                    value=6,
                                    className="mb-3"
                                ),
                                
                                html.Label([html.I(className="fas fa-campground me-2"), "๐Ÿ•๏ธ Number of Camps:"]),
                                dbc.Input(
                                    id='ml-camps-input',
                                    type='number',
                                    min=0, max=6, step=1,
                                    value=3,
                                    className="mb-3"
                                ),
                                
                                html.Label([html.I(className="fas fa-user-friends me-2"), "๐ŸŽ’ Hired Support:"]),
                                dbc.Input(
                                    id='ml-hired-input',
                                    type='number',
                                    min=0, max=50, step=1,
                                    value=10,
                                    className="mb-3"
                                ),
                                html.Label([html.I(className="fas fa-wind me-2"), "๐ŸŽˆ Oxygen Usage:"]),
                                dcc.RadioItems(
                                    id='ml-oxygen-radio',
                                    options=[
                                        {'label': 'โœ… Yes - Supplemental O2', 'value': True},
                                        {'label': '๐Ÿšซ No - Pure Alpine Style', 'value': False}
                                    ],
                                    value=True, className="mb-3"
                                ),
                                
                                html.Label([html.I(className="fas fa-link me-2"), "๐Ÿชข Fixed Ropes:"]),
                                dcc.RadioItems(
                                    id='ml-rope-radio',
                                    options=[
                                        {'label': 'โœ… Yes - Safety First', 'value': True},
                                        {'label': '๐Ÿšซ No - Traditional Style', 'value': False}
                                    ],
                                    value=True, className="mb-4"
                                ),
                                
                                dbc.Button([
                                    html.I(className="fas fa-magic me-2"),
                                    "๐Ÿ”ฎ Predict My Success!"
                                ], id='predict-button', color='primary', size='lg', 
                                className="w-100 mb-3")
                            ])
                        ])
                    ])
                ], md=4),
                
                dbc.Col([
                    html.Div(id='prediction-results')
                ], md=8)
            ])
        ])
    ])

# Callbacks para las rutas colapsables
for i in range(5):  # Mรกximo 5 picos
    @app.callback(
            Output(f'routes-collapse-{i}', 'is_open'),
            Input(f'routes-btn-{i}', 'n_clicks'),
            prevent_initial_call=True
        )
    def toggle_routes_collapse(n_clicks):
            return n_clicks and n_clicks % 2 == 1

# ML Prediction callback
@app.callback(
    Output('prediction-results', 'children'),
    [Input('predict-button', 'n_clicks')],
    [State('ml-peak-dropdown', 'value'),
     State('ml-year-input', 'value'),        
     State('ml-season-dropdown', 'value'),
     State('ml-members-input', 'value'),     
     State('ml-camps-input', 'value'),       
     State('ml-hired-input', 'value'),       
     State('ml-oxygen-radio', 'value'),
     State('ml-rope-radio', 'value')]
)
def predict_expedition_success(n_clicks, peak, year, season, members, camps, hired, oxygen, rope):
    if not n_clicks:
        return dbc.Card([
            dbc.CardBody([
                html.Div([
                    html.I(className="fas fa-rocket", style={"fontSize": "4rem", "color": COLORS['primary']}),
                    html.H3("๐Ÿš€ Ready for Launch?", className="mt-3"),
                    html.P("Configure your expedition parameters and click 'Predict My Success' to discover your summit chances!", 
                           className="text-muted")
                ], className="text-center py-5")
            ])
        ])
    
    try:
        # Get peak data for height
        peak_data = df[df['pkname'] == peak].iloc[0]
        height = peak_data['heightm']
        
        # Prepare prediction data
        prediction_data = pd.DataFrame({
            'year': [year],
            'season': [season],
            'heightm': [height],
            'camps': [camps],
            'totmembers': [members],
            'o2used': [oxygen],
            'tothired': [hired],
            'rope': [rope]
        })
        
        # Make prediction
        success_probability = prediction_model.predict_proba(prediction_data)[0][1]
        success_percentage = success_probability * 100
        
        # Determine risk level and messaging
        if success_percentage >= 70:
            risk_level = "๐ŸŸข HIGH SUCCESS"
            risk_color = "success"
            advice = "๐ŸŽ‰ Excellent conditions for success! Your expedition parameters align with historically successful climbs."
            emoji = "๐Ÿ†"
        elif success_percentage >= 50:
            risk_level = "๐ŸŸก MODERATE SUCCESS"
            risk_color = "warning"
            advice = "โš–๏ธ Balanced odds. Consider optimizing team size, timing, or support to improve your chances."
            emoji = "๐ŸŽฏ"
        else:
            risk_level = "๐Ÿ”ด CHALLENGING CONDITIONS"
            risk_color = "danger"
            advice = "โš ๏ธ Difficult conditions predicted. Review your parameters and consider additional preparation or support."
            emoji = "๐Ÿง—โ€โ™‚๏ธ"
        
        # Historical comparison
        similar_expeditions = df[
            (df['pkname'] == peak) & 
            (df['season'] == season) & 
            (df['totmembers'].between(members-2, members+2))
        ]
        
        historical_success = similar_expeditions['success1'].mean() * 100 if len(similar_expeditions) > 0 else 0
        historical_count = len(similar_expeditions)
        
        # Generate insights
        insights = []
        
        if oxygen:
            insights.append("๐ŸŽˆ Oxygen use significantly improves success rates at extreme altitude")
        else:
            insights.append("๐Ÿซ Alpine style climbing increases difficulty but offers pure achievement")
            
        if members <= 4:
            insights.append("๐Ÿ‘ฅ Small team allows for faster movement and better coordination")
        elif members >= 8:
            insights.append("๐Ÿ‘ฅ Large team provides safety backup but requires more coordination")
            
        if season == 'Spring':
            insights.append("๐ŸŒธ Spring offers the best weather windows for most peaks")
        elif season == 'Winter':
            insights.append("โ„๏ธ Winter climbing presents extreme challenges but fewer crowds")
            
        return dbc.Card([
            dbc.CardHeader([
                html.I(className="fas fa-crystal-ball me-2"),
                f"๐Ÿ”ฎ AI Prediction Results for {peak}"
            ]),
            dbc.CardBody([
                # Main prediction result
                dbc.Alert([
                    html.Div([
                        html.H1([emoji, f" {success_percentage:.1f}%"], 
                               className="display-2 mb-0 text-center"),
                        html.H4(f"{risk_level} PROBABILITY", 
                               className="text-center mb-0 fw-bold")
                    ])
                ], color=risk_color, className="text-center mb-4"),
                
                # Detailed breakdown
                dbc.Row([
                    dbc.Col([
                        dbc.Card([
                            dbc.CardHeader("๐Ÿ“Š Prediction Breakdown"),
                            dbc.CardBody([
                                html.P(advice, className="mb-3"),
                                
                                # Progress bars for key factors
                                html.Div([
                                    html.Label("๐ŸŽฏ Success Probability"),
                                    dbc.Progress(value=success_percentage, color=risk_color.replace('danger', 'warning'), 
                                               className="mb-3", style={"height": "25px"}),
                                    
                                    html.Label("๐Ÿ“ˆ Historical Comparison"),
                                    dbc.Progress(value=historical_success, color="info", 
                                               className="mb-2", style={"height": "20px"}),
                                    html.Small(f"Based on {historical_count} similar expeditions", 
                                             className="text-muted")
                                ])
                            ])
                        ])
                    ], md=6),
                    
                    dbc.Col([
                        dbc.Card([
                            dbc.CardHeader("๐Ÿ’ก AI Insights"),
                            dbc.CardBody([
                                dbc.ListGroup([
                                    dbc.ListGroupItem([
                                        html.I(className="fas fa-lightbulb me-2"),
                                        insight
                                    ]) for insight in insights
                                ], flush=True)
                            ])
                        ])
                    ], md=6)
                ], className="mb-4"),
                
                # Expedition summary
                dbc.Card([
                    dbc.CardHeader("๐Ÿ“‹ Your Expedition Summary"),
                    dbc.CardBody([
                        dbc.Row([
                            dbc.Col([
                                html.Strong("๐Ÿ”๏ธ Peak: "), f"{peak} ({height}m)"
                            ], md=6),
                            dbc.Col([
                                html.Strong("๐Ÿ“… Year: "), f"{year}"
                            ], md=6)
                        ]),
                        dbc.Row([
                            dbc.Col([
                                html.Strong("๐ŸŒจ๏ธ Season: "), f"{season}"
                            ], md=6),
                            dbc.Col([
                                html.Strong("๐Ÿ‘ฅ Team Size: "), f"{members} members"
                            ], md=6)
                        ]),
                        dbc.Row([
                            dbc.Col([
                                html.Strong("๐Ÿ•๏ธ Camps: "), f"{camps}"
                            ], md=6),
                            dbc.Col([
                                html.Strong("๐ŸŽ’ Support: "), f"{hired} hired"
                            ], md=6)
                        ]),
                        dbc.Row([
                            dbc.Col([
                                html.Strong("๐ŸŽˆ Oxygen: "), "Yes" if oxygen else "No"
                            ], md=6),
                            dbc.Col([
                                html.Strong("๐Ÿชข Fixed Ropes: "), "Yes" if rope else "No"
                            ], md=6)
                        ])
                    ])
                ], className="mt-3")
            ])
        ])
        
    except Exception as e:
        return dbc.Alert([
            html.I(className="fas fa-exclamation-triangle me-2"),
            f"๐Ÿšซ Prediction error: {str(e)}. Please check your parameters and try again."
        ], color="danger")