nakas commited on
Commit
0048666
·
1 Parent(s): f1aa366
Files changed (1) hide show
  1. app.py +39 -10
app.py CHANGED
@@ -354,8 +354,14 @@ class RainViewerMap:
354
  def create_gradio_app():
355
  rain_viewer = RainViewerMap()
356
 
357
- # Get available times for dropdown
358
- time_options = rain_viewer.get_available_times()
 
 
 
 
 
 
359
 
360
  with gr.Blocks(title="RainViewer Radar Map") as demo:
361
  gr.Markdown("# Weather Radar & Hurricane Tracker")
@@ -413,14 +419,23 @@ def create_gradio_app():
413
 
414
  def update_map(lat, lon, zoom, show_radar_flag, show_hurricanes_flag, selected_time):
415
  time_index = 0
416
- if selected_time and ":" in selected_time:
417
- time_index = int(selected_time.split(":")[0])
 
 
 
 
 
 
 
418
 
419
  return rain_viewer.create_map(lat, lon, zoom, show_radar_flag, time_index, show_hurricanes_flag)
420
 
421
  def refresh_times():
422
  new_times = rain_viewer.get_available_times()
423
- return gr.Dropdown(choices=new_times, value=new_times[-1] if new_times else None)
 
 
424
 
425
  def get_storm_list_html():
426
  """Generate HTML for storm list display"""
@@ -495,19 +510,33 @@ def create_gradio_app():
495
  outputs=storm_list
496
  )
497
 
498
- # Initialize storm list on load
 
 
 
 
 
 
 
499
  demo.load(
500
- fn=get_storm_list_html,
501
- outputs=storm_list
502
  )
503
 
504
- # Auto-update on input changes
505
- for input_component in [lat_input, lon_input, zoom_input, show_radar, show_hurricanes, time_dropdown]:
506
  input_component.change(
507
  fn=update_map,
508
  inputs=[lat_input, lon_input, zoom_input, show_radar, show_hurricanes, time_dropdown],
509
  outputs=map_html
510
  )
 
 
 
 
 
 
 
511
 
512
  return demo
513
 
 
354
  def create_gradio_app():
355
  rain_viewer = RainViewerMap()
356
 
357
+ # Get available times for dropdown with error handling
358
+ try:
359
+ time_options = rain_viewer.get_available_times()
360
+ if not time_options:
361
+ time_options = ["No data available"]
362
+ except Exception as e:
363
+ print(f"Error getting initial time options: {e}")
364
+ time_options = ["Loading..."]
365
 
366
  with gr.Blocks(title="RainViewer Radar Map") as demo:
367
  gr.Markdown("# Weather Radar & Hurricane Tracker")
 
419
 
420
  def update_map(lat, lon, zoom, show_radar_flag, show_hurricanes_flag, selected_time):
421
  time_index = 0
422
+ if selected_time and ":" in selected_time and selected_time != "No data available" and selected_time != "Loading...":
423
+ try:
424
+ time_index = int(selected_time.split(":")[0])
425
+ # Validate time_index against available data
426
+ available_times = rain_viewer.get_available_times()
427
+ if time_index >= len(available_times):
428
+ time_index = len(available_times) - 1 if available_times else 0
429
+ except (ValueError, IndexError):
430
+ time_index = 0 # Default to first time if parsing fails
431
 
432
  return rain_viewer.create_map(lat, lon, zoom, show_radar_flag, time_index, show_hurricanes_flag)
433
 
434
  def refresh_times():
435
  new_times = rain_viewer.get_available_times()
436
+ # Return both updated choices and a safe default value
437
+ safe_value = new_times[0] if new_times else None # Use first item instead of last
438
+ return gr.Dropdown(choices=new_times, value=safe_value)
439
 
440
  def get_storm_list_html():
441
  """Generate HTML for storm list display"""
 
510
  outputs=storm_list
511
  )
512
 
513
+ # Initialize components on load
514
+ def initialize_app():
515
+ storm_html = get_storm_list_html()
516
+ # Also refresh time options on load to ensure they're current
517
+ current_times = rain_viewer.get_available_times()
518
+ safe_time_value = current_times[0] if current_times else None
519
+ return storm_html, gr.Dropdown(choices=current_times, value=safe_time_value)
520
+
521
  demo.load(
522
+ fn=initialize_app,
523
+ outputs=[storm_list, time_dropdown]
524
  )
525
 
526
+ # Auto-update only on manual controls (not time dropdown to prevent conflicts)
527
+ for input_component in [lat_input, lon_input, zoom_input, show_radar, show_hurricanes]:
528
  input_component.change(
529
  fn=update_map,
530
  inputs=[lat_input, lon_input, zoom_input, show_radar, show_hurricanes, time_dropdown],
531
  outputs=map_html
532
  )
533
+
534
+ # Only update map when time dropdown is explicitly changed by user
535
+ time_dropdown.select(
536
+ fn=update_map,
537
+ inputs=[lat_input, lon_input, zoom_input, show_radar, show_hurricanes, time_dropdown],
538
+ outputs=map_html
539
+ )
540
 
541
  return demo
542