NavyDevilDoc commited on
Commit
bbffc6b
·
verified ·
1 Parent(s): 174af4c

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +33 -18
src/streamlit_app.py CHANGED
@@ -505,19 +505,20 @@ def show_motion_comparison():
505
  # Single plot and table section (no placeholders needed)
506
  st.markdown("---")
507
 
508
- # Create a cache key based on all parameters to avoid unnecessary redraws
509
- cache_key = f"{speed1}_{angle1}_{height1}_{speed2}_{angle2}_{height2}_{speed3}_{angle3}_{height3}"
510
-
511
- # Use containers to prevent flickering
512
- plot_container = st.container()
513
- table_container = st.container()
514
-
515
- with plot_container:
516
- # Create comparison plot
 
517
  fig, ax = plt.subplots(figsize=(12, 8))
518
 
519
  max_range = 0
520
- for name, motion, color in trajectories:
521
  data = motion.trajectory_data(motion.calculate_flight_time())
522
  ax.plot(data['x'], data['y'], linewidth=3, label=name, color=color)
523
 
@@ -533,17 +534,21 @@ def show_motion_comparison():
533
  ax.grid(True, alpha=0.3)
534
  ax.legend(fontsize=12)
535
  ax.set_ylim(bottom=0)
536
- ax.set_xlim(0, max_range * 1.1)
537
 
538
- st.pyplot(fig, clear_figure=True)
539
- plt.close(fig) # Explicitly close the figure
540
 
541
- with table_container:
542
- # Comparison table
543
- st.markdown("### 📋 Trajectory Comparison Table")
 
 
 
 
 
544
 
545
  comparison_data = []
546
- for name, motion, _ in trajectories:
547
  info = motion.get_launch_info()
548
  comparison_data.append({
549
  "Trajectory": name,
@@ -555,7 +560,17 @@ def show_motion_comparison():
555
  "Max Height (m)": f"{info['max_height']:.1f}"
556
  })
557
 
558
- st.table(comparison_data)
 
 
 
 
 
 
 
 
 
 
559
 
560
  if __name__ == "__main__":
561
  main()
 
505
  # Single plot and table section (no placeholders needed)
506
  st.markdown("---")
507
 
508
+ # Create comparison plot using st.cache_data to prevent re-rendering
509
+ @st.cache_data
510
+ def create_comparison_plot(speed1, angle1, height1, speed2, angle2, height2, speed3, angle3, height3):
511
+ # Recreate trajectories inside cached function
512
+ trajectories_cached = [
513
+ ("Trajectory 1", Motion2D(launch_speed=speed1, launch_angle=angle1, launch_height=height1), 'blue'),
514
+ ("Trajectory 2", Motion2D(launch_speed=speed2, launch_angle=angle2, launch_height=height2), 'red'),
515
+ ("Trajectory 3", Motion2D(launch_speed=speed3, launch_angle=angle3, launch_height=height3), 'green')
516
+ ]
517
+
518
  fig, ax = plt.subplots(figsize=(12, 8))
519
 
520
  max_range = 0
521
+ for name, motion, color in trajectories_cached:
522
  data = motion.trajectory_data(motion.calculate_flight_time())
523
  ax.plot(data['x'], data['y'], linewidth=3, label=name, color=color)
524
 
 
534
  ax.grid(True, alpha=0.3)
535
  ax.legend(fontsize=12)
536
  ax.set_ylim(bottom=0)
537
+ ax.set_xlim(0, max_range * 1.1 if max_range > 0 else 10)
538
 
539
+ return fig
 
540
 
541
+ # Create cached comparison data
542
+ @st.cache_data
543
+ def create_comparison_data(speed1, angle1, height1, speed2, angle2, height2, speed3, angle3, height3):
544
+ trajectories_data = [
545
+ ("Trajectory 1", Motion2D(launch_speed=speed1, launch_angle=angle1, launch_height=height1)),
546
+ ("Trajectory 2", Motion2D(launch_speed=speed2, launch_angle=angle2, launch_height=height2)),
547
+ ("Trajectory 3", Motion2D(launch_speed=speed3, launch_angle=angle3, launch_height=height3))
548
+ ]
549
 
550
  comparison_data = []
551
+ for name, motion in trajectories_data:
552
  info = motion.get_launch_info()
553
  comparison_data.append({
554
  "Trajectory": name,
 
560
  "Max Height (m)": f"{info['max_height']:.1f}"
561
  })
562
 
563
+ return comparison_data
564
+
565
+ # Display cached plot
566
+ fig = create_comparison_plot(speed1, angle1, height1, speed2, angle2, height2, speed3, angle3, height3)
567
+ st.pyplot(fig, clear_figure=True)
568
+ plt.close(fig)
569
+
570
+ # Display cached comparison table
571
+ st.markdown("### 📋 Trajectory Comparison Table")
572
+ comparison_data = create_comparison_data(speed1, angle1, height1, speed2, angle2, height2, speed3, angle3, height3)
573
+ st.table(comparison_data)
574
 
575
  if __name__ == "__main__":
576
  main()