NavyDevilDoc commited on
Commit
8b10f08
Β·
verified Β·
1 Parent(s): f32e14b

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +21 -57
src/streamlit_app.py CHANGED
@@ -3,25 +3,6 @@ import numpy as np
3
  import matplotlib.pyplot as plt
4
  from kinematics_visualizer import Motion1D, Motion2D, KinematicsVisualizer
5
 
6
- @st.cache_data
7
- def calculate_trajectory_data(speed, angle, height):
8
- """Cache trajectory calculations to prevent reruns"""
9
- motion = Motion2D(launch_speed=speed, launch_angle=angle, launch_height=height)
10
- return {
11
- 'motion': motion,
12
- 'data': motion.trajectory_data(motion.calculate_flight_time()),
13
- 'info': motion.get_launch_info()
14
- }
15
-
16
- @st.cache_data
17
- def create_comparison_plot_data(trajectories_params):
18
- """Cache plot data generation"""
19
- trajectories = []
20
- for name, speed, angle, height, color in trajectories_params:
21
- cached_data = calculate_trajectory_data(speed, angle, height)
22
- trajectories.append((name, cached_data, color))
23
- return trajectories
24
-
25
  # Configure Streamlit page
26
  st.set_page_config(
27
  page_title="Physics Tutorial: Kinematics",
@@ -80,24 +61,13 @@ st.markdown("""
80
  def main():
81
  st.markdown('<div class="main-header">πŸš€ Physics Tutorial: Kinematics</div>', unsafe_allow_html=True)
82
 
83
- # Use session state to prevent unnecessary reruns
84
- if 'current_tab' not in st.session_state:
85
- st.session_state.current_tab = "1D Motion"
86
-
87
  # Sidebar for navigation
88
  st.sidebar.title("πŸ“š Select Tutorial")
89
  tutorial_type = st.sidebar.radio(
90
  "Choose a physics concept:",
91
- ["1D Motion", "2D Projectile Motion", "Compare Motions"],
92
- index=["1D Motion", "2D Projectile Motion", "Compare Motions"].index(st.session_state.current_tab)
93
  )
94
 
95
- # Only trigger rerun if tab actually changed
96
- if tutorial_type != st.session_state.current_tab:
97
- st.session_state.current_tab = tutorial_type
98
- # Clear matplotlib plots when switching tabs
99
- plt.close('all')
100
-
101
  if tutorial_type == "1D Motion":
102
  show_1d_motion()
103
  elif tutorial_type == "2D Projectile Motion":
@@ -503,44 +473,38 @@ def show_motion_comparison():
503
 
504
  st.markdown("**Compare up to 3 different projectile motions side by side**")
505
 
506
- # Create tabs for different trajectories
507
- tab1, tab2, tab3 = st.tabs(["πŸš€ Trajectory 1", "🎯 Trajectory 2", "⚽ Trajectory 3"])
508
 
509
  trajectories = []
510
 
511
- with tab1:
512
- col1, col2 = st.columns(2)
513
- with col1:
514
- speed1 = st.slider("Speed 1 (m/s)", 5.0, 50.0, 20.0, key="speed1")
515
- angle1 = st.slider("Angle 1 (Β°)", 0.0, 90.0, 30.0, key="angle1")
516
- with col2:
517
- height1 = st.slider("Height 1 (m)", 0.0, 30.0, 0.0, key="height1")
518
-
519
  motion1 = Motion2D(launch_speed=speed1, launch_angle=angle1, launch_height=height1)
520
  trajectories.append(("Trajectory 1", motion1, 'blue'))
521
 
522
- with tab2:
523
- col1, col2 = st.columns(2)
524
- with col1:
525
- speed2 = st.slider("Speed 2 (m/s)", 5.0, 50.0, 25.0, key="speed2")
526
- angle2 = st.slider("Angle 2 (Β°)", 0.0, 90.0, 45.0, key="angle2")
527
- with col2:
528
- height2 = st.slider("Height 2 (m)", 0.0, 30.0, 0.0, key="height2")
529
-
530
  motion2 = Motion2D(launch_speed=speed2, launch_angle=angle2, launch_height=height2)
531
  trajectories.append(("Trajectory 2", motion2, 'red'))
532
 
533
- with tab3:
534
- col1, col2 = st.columns(2)
535
- with col1:
536
- speed3 = st.slider("Speed 3 (m/s)", 5.0, 50.0, 30.0, key="speed3")
537
- angle3 = st.slider("Angle 3 (Β°)", 0.0, 90.0, 60.0, key="angle3")
538
- with col2:
539
- height3 = st.slider("Height 3 (m)", 0.0, 30.0, 5.0, key="height3")
540
-
541
  motion3 = Motion2D(launch_speed=speed3, launch_angle=angle3, launch_height=height3)
542
  trajectories.append(("Trajectory 3", motion3, 'green'))
543
 
 
 
 
544
  # Create comparison plot
545
  fig, ax = plt.subplots(figsize=(12, 8))
546
 
 
3
  import matplotlib.pyplot as plt
4
  from kinematics_visualizer import Motion1D, Motion2D, KinematicsVisualizer
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  # Configure Streamlit page
7
  st.set_page_config(
8
  page_title="Physics Tutorial: Kinematics",
 
61
  def main():
62
  st.markdown('<div class="main-header">πŸš€ Physics Tutorial: Kinematics</div>', unsafe_allow_html=True)
63
 
 
 
 
 
64
  # Sidebar for navigation
65
  st.sidebar.title("πŸ“š Select Tutorial")
66
  tutorial_type = st.sidebar.radio(
67
  "Choose a physics concept:",
68
+ ["1D Motion", "2D Projectile Motion", "Compare Motions"]
 
69
  )
70
 
 
 
 
 
 
 
71
  if tutorial_type == "1D Motion":
72
  show_1d_motion()
73
  elif tutorial_type == "2D Projectile Motion":
 
473
 
474
  st.markdown("**Compare up to 3 different projectile motions side by side**")
475
 
476
+ # Use columns instead of tabs to avoid tab-related flickering
477
+ col1, col2, col3 = st.columns(3)
478
 
479
  trajectories = []
480
 
481
+ with col1:
482
+ st.subheader("πŸš€ Trajectory 1")
483
+ speed1 = st.slider("Speed 1", 5.0, 50.0, 20.0, key="comp_speed1")
484
+ angle1 = st.slider("Angle 1", 0.0, 90.0, 30.0, key="comp_angle1")
485
+ height1 = st.slider("Height 1", 0.0, 30.0, 0.0, key="comp_height1")
 
 
 
486
  motion1 = Motion2D(launch_speed=speed1, launch_angle=angle1, launch_height=height1)
487
  trajectories.append(("Trajectory 1", motion1, 'blue'))
488
 
489
+ with col2:
490
+ st.subheader("🎯 Trajectory 2")
491
+ speed2 = st.slider("Speed 2", 5.0, 50.0, 25.0, key="comp_speed2")
492
+ angle2 = st.slider("Angle 2", 0.0, 90.0, 45.0, key="comp_angle2")
493
+ height2 = st.slider("Height 2", 0.0, 30.0, 0.0, key="comp_height2")
 
 
 
494
  motion2 = Motion2D(launch_speed=speed2, launch_angle=angle2, launch_height=height2)
495
  trajectories.append(("Trajectory 2", motion2, 'red'))
496
 
497
+ with col3:
498
+ st.subheader("⚽ Trajectory 3")
499
+ speed3 = st.slider("Speed 3", 5.0, 50.0, 30.0, key="comp_speed3")
500
+ angle3 = st.slider("Angle 3", 0.0, 90.0, 60.0, key="comp_angle3")
501
+ height3 = st.slider("Height 3", 0.0, 30.0, 5.0, key="comp_height3")
 
 
 
502
  motion3 = Motion2D(launch_speed=speed3, launch_angle=angle3, launch_height=height3)
503
  trajectories.append(("Trajectory 3", motion3, 'green'))
504
 
505
+ # Single plot and table section (no placeholders needed)
506
+ st.markdown("---")
507
+
508
  # Create comparison plot
509
  fig, ax = plt.subplots(figsize=(12, 8))
510