Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import folium
|
3 |
+
from streamlit_folium import folium_static
|
4 |
+
from folium.plugins import MarkerCluster
|
5 |
+
import time
|
6 |
+
import math
|
7 |
+
|
8 |
+
# Define California medical centers data
|
9 |
+
california_med_centers = [
|
10 |
+
('UCSF Medical Center', 37.7631, -122.4576, 'General medical and surgical', 'San Francisco'),
|
11 |
+
('Cedars-Sinai Medical Center', 34.0762, -118.3790, 'Heart specialty', 'Los Angeles'),
|
12 |
+
('Stanford Health Care', 37.4331, -122.1750, 'Teaching hospital', 'Stanford'),
|
13 |
+
('UCLA Medical Center', 34.0659, -118.4466, 'Research and teaching', 'Los Angeles'),
|
14 |
+
('Scripps La Jolla Hospitals', 32.8851, -117.2255, 'Multiple specialties', 'La Jolla'),
|
15 |
+
('Sharp Memorial Hospital', 32.7992, -117.1542, 'Trauma center', 'San Diego'),
|
16 |
+
('Huntington Hospital', 34.1330, -118.1475, 'Non-profit hospital', 'Pasadena'),
|
17 |
+
('Hoag Memorial Hospital', 33.6045, -117.8664, 'Community hospital', 'Newport Beach'),
|
18 |
+
('UCSD Medical Center', 32.7554, -117.1682, 'Academic health center', 'San Diego'),
|
19 |
+
('UC Davis Medical Center', 38.5539, -121.4554, 'Public academic health', 'Sacramento'),
|
20 |
+
('John Muir Medical Center', 37.9192, -122.0426, 'Heart care', 'Walnut Creek'),
|
21 |
+
('Santa Clara Valley Medical Center', 37.3121, -121.9769, 'County hospital', 'San Jose'),
|
22 |
+
('Kaiser Permanente San Francisco', 37.7741, -122.4179, 'Health maintenance organization', 'San Francisco'),
|
23 |
+
('City of Hope', 34.1285, -117.9665, 'Cancer center', 'Duarte'),
|
24 |
+
('UCI Medical Center', 33.7886, -117.8572, 'University hospital', 'Orange'),
|
25 |
+
('Good Samaritan Hospital', 34.0506, -118.2831, 'Private hospital', 'Los Angeles'),
|
26 |
+
('Los Angeles County General', 34.0581, -118.2917, 'Public hospital', 'Los Angeles'),
|
27 |
+
('California Pacific Medical Center', 37.7864, -122.4357, 'Private non-profit', 'San Francisco'),
|
28 |
+
('Sutter Roseville Medical Center', 38.7521, -121.2760, 'General medical and surgical', 'Roseville'),
|
29 |
+
('St. Joseph Hospital', 33.7821, -117.9188, 'Faith-based care', 'Orange')
|
30 |
+
]
|
31 |
+
|
32 |
+
# Initialize session state
|
33 |
+
if 'car_position' not in st.session_state:
|
34 |
+
st.session_state.car_position = [37.7631, -122.4576] # Default start: UCSF
|
35 |
+
if 'is_moving' not in st.session_state:
|
36 |
+
st.session_state.is_moving = False
|
37 |
+
if 'start' not in st.session_state:
|
38 |
+
st.session_state.start = california_med_centers[0] # Default start
|
39 |
+
if 'destination' not in st.session_state:
|
40 |
+
st.session_state.destination = california_med_centers[1] # Default destination
|
41 |
+
|
42 |
+
# Rough distance calculation (Haversine formula in miles)
|
43 |
+
def calculate_distance(lat1, lon1, lat2, lon2):
|
44 |
+
R = 3958.8 # Earth radius in miles
|
45 |
+
lat1_rad, lon1_rad = math.radians(lat1), math.radians(lon1)
|
46 |
+
lat2_rad, lon2_rad = math.radians(lat2), math.radians(lon2)
|
47 |
+
dlat = lat2_rad - lat1_rad
|
48 |
+
dlon = lon2_rad - lon1_rad
|
49 |
+
a = math.sin(dlat / 2)**2 + math.cos(lat1_rad) * math.cos(lat2_rad) * math.sin(dlon / 2)**2
|
50 |
+
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
|
51 |
+
return R * c
|
52 |
+
|
53 |
+
# Create map
|
54 |
+
def create_map():
|
55 |
+
m = folium.Map(location=st.session_state.car_position, zoom_start=13, tiles="OpenStreetMap")
|
56 |
+
|
57 |
+
# Add medical center markers
|
58 |
+
marker_cluster = MarkerCluster().add_to(m)
|
59 |
+
for center in california_med_centers:
|
60 |
+
folium.Marker(
|
61 |
+
location=[center[1], center[2]],
|
62 |
+
popup=f'<b>{center[0]}</b><br>Description: {center[3]}<br>City: {center[4]}',
|
63 |
+
icon=folium.Icon(color='red')
|
64 |
+
).add_to(marker_cluster)
|
65 |
+
|
66 |
+
# Add car marker
|
67 |
+
folium.Marker(
|
68 |
+
location=st.session_state.car_position,
|
69 |
+
popup='Car',
|
70 |
+
icon=folium.Icon(color='blue', icon='car', prefix='fa')
|
71 |
+
).add_to(m)
|
72 |
+
|
73 |
+
return m
|
74 |
+
|
75 |
+
# Move car toward destination
|
76 |
+
def move_car():
|
77 |
+
target = [st.session_state.destination[1], st.session_state.destination[2]]
|
78 |
+
lat_step = (target[0] - st.session_state.car_position[0]) / 50
|
79 |
+
lon_step = (target[1] - st.session_state.car_position[1]) / 50
|
80 |
+
st.session_state.car_position[0] += lat_step
|
81 |
+
st.session_state.car_position[1] += lon_step
|
82 |
+
|
83 |
+
# Display map in a wider, taller container
|
84 |
+
st.markdown("""
|
85 |
+
<style>
|
86 |
+
.full-width-map {width: 100%; height: 600px;}
|
87 |
+
</style>
|
88 |
+
""", unsafe_allow_html=True)
|
89 |
+
|
90 |
+
with st.container():
|
91 |
+
m = create_map()
|
92 |
+
folium_static(m, width=1200, height=600) # Larger map
|
93 |
+
|
94 |
+
st.markdown("""
|
95 |
+
# 🏥 California Medical Centers 🌆
|
96 |
+
Select a start and destination, then drive the car across the map! The map stays centered on the car.
|
97 |
+
""")
|
98 |
+
|
99 |
+
# Display start, destination, and distance
|
100 |
+
distance = calculate_distance(
|
101 |
+
st.session_state.start[1], st.session_state.start[2],
|
102 |
+
st.session_state.destination[1], st.session_state.destination[2]
|
103 |
+
)
|
104 |
+
remaining_distance = calculate_distance(
|
105 |
+
st.session_state.car_position[0], st.session_state.car_position[1],
|
106 |
+
st.session_state.destination[1], st.session_state.destination[2]
|
107 |
+
)
|
108 |
+
st.write(f"**Start**: {st.session_state.start[0]} ({st.session_state.start[1]}, {st.session_state.start[2]})")
|
109 |
+
st.write(f"**Destination**: {st.session_state.destination[0]} ({st.session_state.destination[1]}, {st.session_state.destination[2]})")
|
110 |
+
st.write(f"**Total Distance**: {distance:.1f} miles")
|
111 |
+
if st.session_state.is_moving:
|
112 |
+
st.write(f"**Remaining Distance**: {remaining_distance:.1f} miles")
|
113 |
+
|
114 |
+
# Controls
|
115 |
+
col1, col2, col3 = st.columns(3)
|
116 |
+
with col1:
|
117 |
+
if st.button("Start Driving"):
|
118 |
+
st.session_state.is_moving = True
|
119 |
+
with col2:
|
120 |
+
if st.button("Stop Driving"):
|
121 |
+
st.session_state.is_moving = False
|
122 |
+
with col3:
|
123 |
+
if st.button("Reset Car to Start"):
|
124 |
+
st.session_state.car_position = [st.session_state.start[1], st.session_state.start[2]]
|
125 |
+
st.session_state.is_moving = False
|
126 |
+
st.rerun()
|
127 |
+
|
128 |
+
# Start and destination selection
|
129 |
+
st.subheader("Set Start and Destination")
|
130 |
+
start_col, dest_col = st.columns(2)
|
131 |
+
with start_col:
|
132 |
+
st.write("Pick Start Location:")
|
133 |
+
for i in range(0, len(california_med_centers), 2):
|
134 |
+
col1, col2 = st.columns(2)
|
135 |
+
with col1:
|
136 |
+
if st.button(f"Start: {california_med_centers[i][0]}", key=f"start_{i}"):
|
137 |
+
st.session_state.start = california_med_centers[i]
|
138 |
+
st.session_state.car_position = [california_med_centers[i][1], california_med_centers[i][2]]
|
139 |
+
st.rerun()
|
140 |
+
if i + 1 < len(california_med_centers):
|
141 |
+
with col2:
|
142 |
+
if st.button(f"Start: {california_med_centers[i+1][0]}", key=f"start_{i+1}"):
|
143 |
+
st.session_state.start = california_med_centers[i+1]
|
144 |
+
st.session_state.car_position = [california_med_centers[i+1][1], california_med_centers[i+1][2]]
|
145 |
+
st.rerun()
|
146 |
+
|
147 |
+
with dest_col:
|
148 |
+
st.write("Pick Destination Location:")
|
149 |
+
for i in range(0, len(california_med_centers), 2):
|
150 |
+
col1, col2 = st.columns(2)
|
151 |
+
with col1:
|
152 |
+
if st.button(f"Dest: {california_med_centers[i][0]}", key=f"dest_{i}"):
|
153 |
+
st.session_state.destination = california_med_centers[i]
|
154 |
+
st.rerun()
|
155 |
+
if i + 1 < len(california_med_centers):
|
156 |
+
with col2:
|
157 |
+
if st.button(f"Dest: {california_med_centers[i+1][0]}", key=f"dest_{i+1}"):
|
158 |
+
st.session_state.destination = california_med_centers[i+1]
|
159 |
+
st.rerun()
|
160 |
+
|
161 |
+
# Simulate car movement with pause
|
162 |
+
if st.session_state.is_moving:
|
163 |
+
move_car()
|
164 |
+
time.sleep(0.5) # Pause to reduce flickering
|
165 |
+
st.rerun()
|