CCockrum commited on
Commit
c93cce0
·
verified ·
1 Parent(s): 1558392

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -26
app.py CHANGED
@@ -94,7 +94,7 @@ class OceanCurrentMapper:
94
 
95
  def create_current_map(self, region: str, resolution: str, show_vectors: bool,
96
  show_speed: bool, vector_scale: float) -> go.Figure:
97
- """Create interactive ocean current map"""
98
 
99
  # Get current data
100
  current_data = self.generate_synthetic_current_data(region, resolution)
@@ -110,7 +110,12 @@ class OceanCurrentMapper:
110
  colorscale='Viridis',
111
  name='Current Speed (m/s)',
112
  showscale=True,
113
- colorbar=dict(title="Speed (m/s)", x=1.02)
 
 
 
 
 
114
  ))
115
 
116
  # Add vector field if requested
@@ -142,22 +147,42 @@ class OceanCurrentMapper:
142
  showarrow=True
143
  )
144
 
145
- # Update layout
 
 
 
 
146
  fig.update_layout(
147
  title=f'Ocean Currents - {region}',
148
- xaxis_title='Longitude',
149
- yaxis_title='Latitude',
 
 
 
 
 
 
 
 
150
  showlegend=True,
151
- width=None, # Let it auto-size
152
- height=500, # Smaller height for embedding
153
  autosize=True,
154
- margin=dict(l=50, r=50, t=80, b=50)
 
 
 
 
 
 
155
  )
156
 
 
 
 
 
157
  return fig
158
 
159
  def get_forecast_data(self, region: str, forecast_hours: int) -> go.Figure:
160
- """Generate forecast visualization"""
161
 
162
  # Create time series for forecast
163
  times = [datetime.now() + timedelta(hours=i) for i in range(forecast_hours)]
@@ -179,8 +204,9 @@ class OceanCurrentMapper:
179
  fig = make_subplots(
180
  rows=3, cols=1,
181
  subplot_titles=('Current Speed (m/s)', 'Wave Height (m)', 'Wind Speed (m/s)'),
182
- vertical_spacing=0.08,
183
- shared_xaxes=True
 
184
  )
185
 
186
  # Current Speed subplot
@@ -222,14 +248,15 @@ class OceanCurrentMapper:
222
  row=3, col=1
223
  )
224
 
225
- # Update layout
226
  fig.update_layout(
227
  title=f'Ocean Conditions Forecast - {region}',
228
- showlegend=False, # Remove legend since subplot titles are clear
229
- width=None,
230
- height=600, # Increased height for 3 subplots
231
  autosize=True,
232
- margin=dict(l=50, r=50, t=80, b=50)
 
233
  )
234
 
235
  # Update x-axis labels
@@ -289,6 +316,7 @@ def create_current_map(region, resolution, show_vectors, show_speed, vector_scal
289
  # Return empty plot on error
290
  fig = go.Figure()
291
  fig.add_annotation(text=f"Error: {str(e)}", x=0.5, y=0.5, showarrow=False)
 
292
  return fig
293
 
294
  def create_forecast(region, forecast_hours):
@@ -300,6 +328,7 @@ def create_forecast(region, forecast_hours):
300
  # Return empty plot on error
301
  fig = go.Figure()
302
  fig.add_annotation(text=f"Error: {str(e)}", x=0.5, y=0.5, showarrow=False)
 
303
  return fig
304
 
305
  def analyze_conditions(region):
@@ -310,7 +339,7 @@ def analyze_conditions(region):
310
  traceback.print_exc()
311
  return f"Error analyzing conditions: {str(e)}"
312
 
313
- # Define the Gradio interface
314
  with gr.Blocks(title="Ocean Current Mapper", theme=gr.themes.Ocean()) as demo:
315
  gr.Markdown("""
316
  <h1 style="font-size: 3em; text-align: center; color: #2E86AB; margin-bottom: 0.5em;">
@@ -330,7 +359,7 @@ with gr.Blocks(title="Ocean Current Mapper", theme=gr.themes.Ocean()) as demo:
330
 
331
  with gr.Tab("Current Map"):
332
  with gr.Row():
333
- with gr.Column(scale=1):
334
  region = gr.Dropdown(
335
  choices=["Gulf of Mexico", "California Coast", "Atlantic Coast", "Global"],
336
  value="Gulf of Mexico",
@@ -352,8 +381,13 @@ with gr.Blocks(title="Ocean Current Mapper", theme=gr.themes.Ocean()) as demo:
352
  )
353
  update_map = gr.Button("Update Map", variant="primary")
354
 
355
- with gr.Column(scale=2):
356
- current_map = gr.Plot(label="Ocean Current Map")
 
 
 
 
 
357
 
358
  update_map.click(
359
  fn=create_current_map,
@@ -363,7 +397,7 @@ with gr.Blocks(title="Ocean Current Mapper", theme=gr.themes.Ocean()) as demo:
363
 
364
  with gr.Tab("Forecast"):
365
  with gr.Row():
366
- with gr.Column(scale=1):
367
  forecast_region = gr.Dropdown(
368
  choices=["Gulf of Mexico", "California Coast", "Atlantic Coast", "Global"],
369
  value="Gulf of Mexico",
@@ -378,8 +412,13 @@ with gr.Blocks(title="Ocean Current Mapper", theme=gr.themes.Ocean()) as demo:
378
  )
379
  update_forecast = gr.Button("Generate Forecast", variant="primary")
380
 
381
- with gr.Column(scale=2):
382
- forecast_plot = gr.Plot(label="Ocean Conditions Forecast")
 
 
 
 
 
383
 
384
  update_forecast.click(
385
  fn=create_forecast,
@@ -389,7 +428,7 @@ with gr.Blocks(title="Ocean Current Mapper", theme=gr.themes.Ocean()) as demo:
389
 
390
  with gr.Tab("Surfing Conditions"):
391
  with gr.Row():
392
- with gr.Column(scale=1):
393
  surf_region = gr.Dropdown(
394
  choices=["Gulf of Mexico", "California Coast", "Atlantic Coast"],
395
  value="California Coast",
@@ -397,7 +436,7 @@ with gr.Blocks(title="Ocean Current Mapper", theme=gr.themes.Ocean()) as demo:
397
  )
398
  analyze_button = gr.Button("Analyze Conditions", variant="primary")
399
 
400
- with gr.Column(scale=2):
401
  surf_analysis = gr.Textbox(
402
  label="Surfing Conditions Analysis",
403
  lines=8,
@@ -447,7 +486,6 @@ if __name__ == "__main__":
447
  print("Starting Ocean Current Mapper...")
448
  demo.launch(
449
  share=True,
450
- height=600,
451
  show_error=True,
452
  inbrowser=False,
453
  server_name="0.0.0.0",
 
94
 
95
  def create_current_map(self, region: str, resolution: str, show_vectors: bool,
96
  show_speed: bool, vector_scale: float) -> go.Figure:
97
+ """Create interactive ocean current map with improved sizing"""
98
 
99
  # Get current data
100
  current_data = self.generate_synthetic_current_data(region, resolution)
 
110
  colorscale='Viridis',
111
  name='Current Speed (m/s)',
112
  showscale=True,
113
+ colorbar=dict(
114
+ title="Speed (m/s)",
115
+ x=1.02,
116
+ thickness=15,
117
+ len=0.7
118
+ )
119
  ))
120
 
121
  # Add vector field if requested
 
147
  showarrow=True
148
  )
149
 
150
+ # Calculate aspect ratio for better proportions
151
+ lat_range = current_data['latitude'].max() - current_data['latitude'].min()
152
+ lon_range = current_data['longitude'].max() - current_data['longitude'].min()
153
+
154
+ # Update layout with improved sizing
155
  fig.update_layout(
156
  title=f'Ocean Currents - {region}',
157
+ xaxis=dict(
158
+ title='Longitude',
159
+ scaleanchor="y",
160
+ scaleratio=1,
161
+ constrain="domain"
162
+ ),
163
+ yaxis=dict(
164
+ title='Latitude',
165
+ constrain="domain"
166
+ ),
167
  showlegend=True,
 
 
168
  autosize=True,
169
+ # Set explicit dimensions for better control
170
+ width=800,
171
+ height=600,
172
+ margin=dict(l=60, r=100, t=80, b=60),
173
+ # Add responsive config
174
+ dragmode='pan',
175
+ hovermode='closest'
176
  )
177
 
178
+ # Set axis ranges for better proportions
179
+ fig.update_xaxes(range=[current_data['longitude'].min(), current_data['longitude'].max()])
180
+ fig.update_yaxes(range=[current_data['latitude'].min(), current_data['latitude'].max()])
181
+
182
  return fig
183
 
184
  def get_forecast_data(self, region: str, forecast_hours: int) -> go.Figure:
185
+ """Generate forecast visualization with improved sizing"""
186
 
187
  # Create time series for forecast
188
  times = [datetime.now() + timedelta(hours=i) for i in range(forecast_hours)]
 
204
  fig = make_subplots(
205
  rows=3, cols=1,
206
  subplot_titles=('Current Speed (m/s)', 'Wave Height (m)', 'Wind Speed (m/s)'),
207
+ vertical_spacing=0.1,
208
+ shared_xaxes=True,
209
+ specs=[[{"secondary_y": False}], [{"secondary_y": False}], [{"secondary_y": False}]]
210
  )
211
 
212
  # Current Speed subplot
 
248
  row=3, col=1
249
  )
250
 
251
+ # Update layout with better sizing
252
  fig.update_layout(
253
  title=f'Ocean Conditions Forecast - {region}',
254
+ showlegend=False,
255
+ width=800,
256
+ height=650,
257
  autosize=True,
258
+ margin=dict(l=60, r=50, t=80, b=60),
259
+ hovermode='x unified'
260
  )
261
 
262
  # Update x-axis labels
 
316
  # Return empty plot on error
317
  fig = go.Figure()
318
  fig.add_annotation(text=f"Error: {str(e)}", x=0.5, y=0.5, showarrow=False)
319
+ fig.update_layout(width=800, height=600)
320
  return fig
321
 
322
  def create_forecast(region, forecast_hours):
 
328
  # Return empty plot on error
329
  fig = go.Figure()
330
  fig.add_annotation(text=f"Error: {str(e)}", x=0.5, y=0.5, showarrow=False)
331
+ fig.update_layout(width=800, height=600)
332
  return fig
333
 
334
  def analyze_conditions(region):
 
339
  traceback.print_exc()
340
  return f"Error analyzing conditions: {str(e)}"
341
 
342
+ # Define the Gradio interface with improved layout
343
  with gr.Blocks(title="Ocean Current Mapper", theme=gr.themes.Ocean()) as demo:
344
  gr.Markdown("""
345
  <h1 style="font-size: 3em; text-align: center; color: #2E86AB; margin-bottom: 0.5em;">
 
359
 
360
  with gr.Tab("Current Map"):
361
  with gr.Row():
362
+ with gr.Column(scale=1, min_width=300):
363
  region = gr.Dropdown(
364
  choices=["Gulf of Mexico", "California Coast", "Atlantic Coast", "Global"],
365
  value="Gulf of Mexico",
 
381
  )
382
  update_map = gr.Button("Update Map", variant="primary")
383
 
384
+ with gr.Column(scale=2, min_width=600):
385
+ current_map = gr.Plot(
386
+ label="Ocean Current Map",
387
+ show_label=False,
388
+ container=True,
389
+ scale=1
390
+ )
391
 
392
  update_map.click(
393
  fn=create_current_map,
 
397
 
398
  with gr.Tab("Forecast"):
399
  with gr.Row():
400
+ with gr.Column(scale=1, min_width=300):
401
  forecast_region = gr.Dropdown(
402
  choices=["Gulf of Mexico", "California Coast", "Atlantic Coast", "Global"],
403
  value="Gulf of Mexico",
 
412
  )
413
  update_forecast = gr.Button("Generate Forecast", variant="primary")
414
 
415
+ with gr.Column(scale=2, min_width=600):
416
+ forecast_plot = gr.Plot(
417
+ label="Ocean Conditions Forecast",
418
+ show_label=False,
419
+ container=True,
420
+ scale=1
421
+ )
422
 
423
  update_forecast.click(
424
  fn=create_forecast,
 
428
 
429
  with gr.Tab("Surfing Conditions"):
430
  with gr.Row():
431
+ with gr.Column(scale=1, min_width=300):
432
  surf_region = gr.Dropdown(
433
  choices=["Gulf of Mexico", "California Coast", "Atlantic Coast"],
434
  value="California Coast",
 
436
  )
437
  analyze_button = gr.Button("Analyze Conditions", variant="primary")
438
 
439
+ with gr.Column(scale=2, min_width=600):
440
  surf_analysis = gr.Textbox(
441
  label="Surfing Conditions Analysis",
442
  lines=8,
 
486
  print("Starting Ocean Current Mapper...")
487
  demo.launch(
488
  share=True,
 
489
  show_error=True,
490
  inbrowser=False,
491
  server_name="0.0.0.0",