Update app.py
Browse files
app.py
CHANGED
@@ -31,7 +31,7 @@ california_med_centers = [
|
|
31 |
|
32 |
# Initialize session state
|
33 |
if 'car_position' not in st.session_state:
|
34 |
-
st.session_state.car_position = [37.7631, -122.4576]
|
35 |
if 'is_moving' not in st.session_state:
|
36 |
st.session_state.is_moving = False
|
37 |
if 'start' not in st.session_state:
|
@@ -45,7 +45,7 @@ if 'elapsed_time' not in st.session_state:
|
|
45 |
|
46 |
# Distance calculation (Haversine in miles)
|
47 |
def calculate_distance(lat1, lon1, lat2, lon2):
|
48 |
-
R = 3958.8
|
49 |
lat1_rad, lon1_rad = math.radians(lat1), math.radians(lon1)
|
50 |
lat2_rad, lon2_rad = math.radians(lat2), math.radians(lon2)
|
51 |
dlat = lat2_rad - lat1_rad
|
@@ -54,7 +54,7 @@ def calculate_distance(lat1, lon1, lat2, lon2):
|
|
54 |
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
|
55 |
return R * c
|
56 |
|
57 |
-
# Create map
|
58 |
def create_map():
|
59 |
m = folium.Map(location=st.session_state.car_position, zoom_start=13, tiles="CartoDB Positron")
|
60 |
marker_cluster = MarkerCluster().add_to(m)
|
@@ -67,7 +67,7 @@ def create_map():
|
|
67 |
folium.Marker(
|
68 |
location=st.session_state.car_position,
|
69 |
popup='Hovercar',
|
70 |
-
icon=folium.Icon(color='purple', icon='rocket', prefix='fa')
|
71 |
).add_to(m)
|
72 |
return m
|
73 |
|
@@ -79,37 +79,22 @@ def move_car():
|
|
79 |
st.session_state.car_position[0] += lat_step
|
80 |
st.session_state.car_position[1] += lon_step
|
81 |
|
82 |
-
# Calculate speed
|
83 |
total_distance = calculate_distance(st.session_state.start[1], st.session_state.start[2],
|
84 |
st.session_state.destination[1], st.session_state.destination[2])
|
85 |
-
steps = 50
|
86 |
-
step_time = 0.5
|
87 |
-
total_time = steps * step_time
|
88 |
-
speed = total_distance / (total_time / 3600)
|
89 |
|
90 |
-
# Layout:
|
91 |
-
left_col, right_col = st.columns([
|
92 |
|
93 |
-
# Left column: Map
|
94 |
with left_col:
|
95 |
st.markdown("### π Hovercar Command Center πΈ")
|
96 |
m = create_map()
|
97 |
-
folium_static(m, width=
|
98 |
-
|
99 |
-
# Display stats
|
100 |
-
remaining_distance = calculate_distance(
|
101 |
-
st.session_state.car_position[0], st.session_state.car_position[1],
|
102 |
-
st.session_state.destination[1], st.session_state.destination[2]
|
103 |
-
)
|
104 |
-
st.write(f"**Start**: {st.session_state.start[0]} ({st.session_state.start[1]:.4f}, {st.session_state.start[2]:.4f})")
|
105 |
-
st.write(f"**Destination**: {st.session_state.destination[0]} ({st.session_state.destination[1]:.4f}, {st.session_state.destination[2]:.4f})")
|
106 |
-
st.write(f"**Total Distance**: {total_distance:.1f} miles")
|
107 |
-
st.write(f"**Remaining Distance**: {remaining_distance:.1f} miles")
|
108 |
-
st.write(f"**Speed**: {speed:.1f} mph")
|
109 |
-
st.write(f"**ETA**: {total_time:.1f} seconds")
|
110 |
-
if st.session_state.is_moving:
|
111 |
-
elapsed = st.session_state.elapsed_time
|
112 |
-
st.write(f"**Elapsed Time**: {elapsed:.1f} seconds")
|
113 |
|
114 |
# Controls
|
115 |
col1, col2, col3 = st.columns(3)
|
@@ -129,33 +114,51 @@ with left_col:
|
|
129 |
st.session_state.elapsed_time = 0
|
130 |
st.rerun()
|
131 |
|
132 |
-
# Right column:
|
133 |
with right_col:
|
134 |
-
st.markdown("###
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
st.
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
|
|
145 |
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
st.
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
|
|
154 |
|
155 |
# Movement logic
|
156 |
if st.session_state.is_moving:
|
157 |
move_car()
|
158 |
if st.session_state.start_time:
|
159 |
st.session_state.elapsed_time = time.time() - st.session_state.start_time
|
160 |
-
time.sleep(0.5)
|
161 |
st.rerun()
|
|
|
31 |
|
32 |
# Initialize session state
|
33 |
if 'car_position' not in st.session_state:
|
34 |
+
st.session_state.car_position = [37.7631, -122.4576]
|
35 |
if 'is_moving' not in st.session_state:
|
36 |
st.session_state.is_moving = False
|
37 |
if 'start' not in st.session_state:
|
|
|
45 |
|
46 |
# Distance calculation (Haversine in miles)
|
47 |
def calculate_distance(lat1, lon1, lat2, lon2):
|
48 |
+
R = 3958.8
|
49 |
lat1_rad, lon1_rad = math.radians(lat1), math.radians(lon1)
|
50 |
lat2_rad, lon2_rad = math.radians(lat2), math.radians(lon2)
|
51 |
dlat = lat2_rad - lat1_rad
|
|
|
54 |
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
|
55 |
return R * c
|
56 |
|
57 |
+
# Create map
|
58 |
def create_map():
|
59 |
m = folium.Map(location=st.session_state.car_position, zoom_start=13, tiles="CartoDB Positron")
|
60 |
marker_cluster = MarkerCluster().add_to(m)
|
|
|
67 |
folium.Marker(
|
68 |
location=st.session_state.car_position,
|
69 |
popup='Hovercar',
|
70 |
+
icon=folium.Icon(color='purple', icon='rocket', prefix='fa')
|
71 |
).add_to(m)
|
72 |
return m
|
73 |
|
|
|
79 |
st.session_state.car_position[0] += lat_step
|
80 |
st.session_state.car_position[1] += lon_step
|
81 |
|
82 |
+
# Calculate speed and ETA
|
83 |
total_distance = calculate_distance(st.session_state.start[1], st.session_state.start[2],
|
84 |
st.session_state.destination[1], st.session_state.destination[2])
|
85 |
+
steps = 50
|
86 |
+
step_time = 0.5
|
87 |
+
total_time = steps * step_time
|
88 |
+
speed = total_distance / (total_time / 3600)
|
89 |
|
90 |
+
# Layout: 75% left, 25% right
|
91 |
+
left_col, right_col = st.columns([3, 1])
|
92 |
|
93 |
+
# Left column: Map (9x16 landscape)
|
94 |
with left_col:
|
95 |
st.markdown("### π Hovercar Command Center πΈ")
|
96 |
m = create_map()
|
97 |
+
folium_static(m, width=720, height=1280) # 9:16 ratio scaled to fit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
# Controls
|
100 |
col1, col2, col3 = st.columns(3)
|
|
|
114 |
st.session_state.elapsed_time = 0
|
115 |
st.rerun()
|
116 |
|
117 |
+
# Right column: Stats and two sub-columns for buttons
|
118 |
with right_col:
|
119 |
+
st.markdown("### π‘ Mission Status")
|
120 |
+
remaining_distance = calculate_distance(
|
121 |
+
st.session_state.car_position[0], st.session_state.car_position[1],
|
122 |
+
st.session_state.destination[1], st.session_state.destination[2]
|
123 |
+
)
|
124 |
+
st.write(f"**Start**: {st.session_state.start[0]} ({st.session_state.start[1]:.4f}, {st.session_state.start[2]:.4f})")
|
125 |
+
st.write(f"**Destination**: {st.session_state.destination[0]} ({st.session_state.destination[1]:.4f}, {st.session_state.destination[2]:.4f})")
|
126 |
+
st.write(f"**Total Distance**: {total_distance:.1f} miles")
|
127 |
+
st.write(f"**Remaining Distance**: {remaining_distance:.1f} miles")
|
128 |
+
st.write(f"**Speed**: {speed:.1f} mph")
|
129 |
+
st.write(f"**ETA**: {total_time:.1f} seconds")
|
130 |
+
if st.session_state.is_moving:
|
131 |
+
elapsed = st.session_state.elapsed_time
|
132 |
+
st.write(f"**Elapsed Time**: {elapsed:.1f} seconds")
|
133 |
+
|
134 |
+
# Two sub-columns for start and destination
|
135 |
+
start_col, dest_col = st.columns(2)
|
136 |
|
137 |
+
with start_col:
|
138 |
+
st.write("**π¦ Start Points**")
|
139 |
+
for center in california_med_centers:
|
140 |
+
if st.button(f"π {center[0]}", key=f"start_{center[0]}"):
|
141 |
+
st.session_state.start = center
|
142 |
+
st.session_state.car_position = [center[1], center[2]]
|
143 |
+
st.session_state.is_moving = False
|
144 |
+
st.session_state.start_time = None
|
145 |
+
st.session_state.elapsed_time = 0
|
146 |
+
st.rerun()
|
147 |
|
148 |
+
with dest_col:
|
149 |
+
st.write("**π Destinations**")
|
150 |
+
for center in california_med_centers:
|
151 |
+
if st.button(f"π― {center[0]}", key=f"dest_{center[0]}"):
|
152 |
+
st.session_state.destination = center
|
153 |
+
st.session_state.is_moving = False
|
154 |
+
st.session_state.start_time = None
|
155 |
+
st.session_state.elapsed_time = 0
|
156 |
+
st.rerun()
|
157 |
|
158 |
# Movement logic
|
159 |
if st.session_state.is_moving:
|
160 |
move_car()
|
161 |
if st.session_state.start_time:
|
162 |
st.session_state.elapsed_time = time.time() - st.session_state.start_time
|
163 |
+
time.sleep(0.5)
|
164 |
st.rerun()
|