Spaces:
Build error
Build error
elli-teu commited on
Commit ·
96b4442
1
Parent(s): e97cfa6
UI klart?
Browse files
app.py
CHANGED
|
@@ -220,6 +220,7 @@ def visualize(filtered_data, stop_name):
|
|
| 220 |
if row["stop_name"] == stop_name:
|
| 221 |
folium.Marker(
|
| 222 |
[row['stop_lat'], row['stop_lon']],
|
|
|
|
| 223 |
popup=f"Bus stop: {row['stop_name']} Bus occupancy: {categories[row['vehicle_occupancystatus']] }",
|
| 224 |
icon = folium.Icon(color="darkpurple", icon="bus-simple", prefix="fa")
|
| 225 |
).add_to(m)
|
|
@@ -233,6 +234,17 @@ def visualize(filtered_data, stop_name):
|
|
| 233 |
# Display the map
|
| 234 |
st_folium(m, width=700, height=500)
|
| 235 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
# Streamlit UI
|
| 237 |
def main():
|
| 238 |
st.title("Wheely Fun Times - Bus Occupancy Explorer")
|
|
@@ -288,16 +300,14 @@ def main():
|
|
| 288 |
long_names = list(pd.unique(route["route_long_name"]))
|
| 289 |
if len(long_names)==1:
|
| 290 |
bus = long_names[0]
|
| 291 |
-
st.write("### Selected Bus")
|
| 292 |
-
st.write(f"{search}: {bus}")
|
| 293 |
else:
|
| 294 |
bus = st.sidebar.selectbox(
|
| 295 |
"Pick bus route:",
|
| 296 |
options=long_names,
|
| 297 |
help="Select one bus to view details."
|
| 298 |
)
|
| 299 |
-
st.write("### Selected Bus")
|
| 300 |
-
st.write(f"{search}: {bus}")
|
| 301 |
|
| 302 |
# Streamlit checkbox to toggle bus direction
|
| 303 |
if "direction" not in st.session_state:
|
|
@@ -339,16 +349,6 @@ def main():
|
|
| 339 |
start_time = st.sidebar.time_input("Select a start time", value=None)
|
| 340 |
end_time = st.sidebar.time_input("Select an end time", value=None)
|
| 341 |
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
#TODO remove
|
| 345 |
-
#trip_ids = list(trips["trip_id"])
|
| 346 |
-
#plot_df = st.session_state.data[st.session_state.data["trip_id"]==trip_ids[0]]
|
| 347 |
-
|
| 348 |
-
#TODO hållsplats
|
| 349 |
-
#Kolla på route_id för att plocka alla hållplatser
|
| 350 |
-
|
| 351 |
-
|
| 352 |
|
| 353 |
print(f"start time {type(start_time)}")
|
| 354 |
print(f"end time {type(end_time)}")
|
|
@@ -357,32 +357,28 @@ def main():
|
|
| 357 |
if start_time != None and end_time != None:
|
| 358 |
#TODO hur filtrera på tid?
|
| 359 |
st.write(f"Displaying buses between {start_time.strftime('%H:%M')} and {end_time.strftime('%H:%M')} the {day_choice}")
|
| 360 |
-
|
| 361 |
-
& (bus_trips["datetime"] <= datetime.combine(date_options[day_choice], end_time))
|
| 362 |
-
& (bus_trips["direction_id"] == st.session_state.direction )]"""
|
| 363 |
selected_trips = bus_trips[(bus_trips["datetime"] >= datetime.combine(date_options[day_choice], start_time))
|
| 364 |
& (bus_trips["datetime"] <= datetime.combine(date_options[day_choice], end_time))
|
| 365 |
& (bus_trips["direction_id"] == st.session_state.direction )
|
| 366 |
& (bus_trips["stop_name"] == stop_choice)]
|
| 367 |
trip_ids = list(pd.unique(selected_trips["trip_id"]))
|
| 368 |
-
|
| 369 |
chioce = selected_trips[selected_trips["stop_name"]==stop_choice]
|
| 370 |
-
chioce.head()
|
| 371 |
#TODO ta bort stop_name:)
|
| 372 |
chioce = chioce[["trip_id", "stop_name", "datetime"]]
|
| 373 |
#Ev lägga stop_chioce i session_state
|
| 374 |
|
| 375 |
chioce = chioce.sort_values(by=["datetime"])
|
| 376 |
-
chioce = chioce
|
|
|
|
|
|
|
| 377 |
|
| 378 |
for idx, row in chioce.iterrows():
|
| 379 |
-
st.write(f"The bus arrives at {row['stop_name']} at {row['datetime'].strftime('%H:%M')}")
|
| 380 |
plot_graph_title(st.session_state.data[st.session_state.data["trip_id"]==row["trip_id"]], row["stop_name"], row['datetime'].strftime('%H:%M'))
|
| 381 |
-
|
| 382 |
-
st.write(f"Length {len(trip_ids)}")
|
| 383 |
-
for id in trip_ids:
|
| 384 |
-
plot_graph(st.session_state.data[st.session_state.data["trip_id"]==id])
|
| 385 |
-
visualize(st.session_state.data[st.session_state.data["trip_id"]==id], stop_choice)
|
| 386 |
|
| 387 |
|
| 388 |
|
|
@@ -392,9 +388,9 @@ def main():
|
|
| 392 |
|
| 393 |
|
| 394 |
# Display data and graphs
|
| 395 |
-
if st.session_state.data is not None:
|
| 396 |
#plot_graphs(st.session_state.data)
|
| 397 |
-
|
| 398 |
|
| 399 |
main()
|
| 400 |
|
|
|
|
| 220 |
if row["stop_name"] == stop_name:
|
| 221 |
folium.Marker(
|
| 222 |
[row['stop_lat'], row['stop_lon']],
|
| 223 |
+
|
| 224 |
popup=f"Bus stop: {row['stop_name']} Bus occupancy: {categories[row['vehicle_occupancystatus']] }",
|
| 225 |
icon = folium.Icon(color="darkpurple", icon="bus-simple", prefix="fa")
|
| 226 |
).add_to(m)
|
|
|
|
| 234 |
# Display the map
|
| 235 |
st_folium(m, width=700, height=500)
|
| 236 |
|
| 237 |
+
def drop_the_duplicates(df):
|
| 238 |
+
|
| 239 |
+
df = df.drop_duplicates("datetime")
|
| 240 |
+
df["previous"] = df["datetime"].shift(1)
|
| 241 |
+
df = df[((df["datetime"] - df["previous"]) > timedelta(minutes=3)) | (df["previous"].isna())]
|
| 242 |
+
|
| 243 |
+
return df
|
| 244 |
+
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
|
| 248 |
# Streamlit UI
|
| 249 |
def main():
|
| 250 |
st.title("Wheely Fun Times - Bus Occupancy Explorer")
|
|
|
|
| 300 |
long_names = list(pd.unique(route["route_long_name"]))
|
| 301 |
if len(long_names)==1:
|
| 302 |
bus = long_names[0]
|
| 303 |
+
st.write(f"### Selected Bus: {search} {bus}")
|
|
|
|
| 304 |
else:
|
| 305 |
bus = st.sidebar.selectbox(
|
| 306 |
"Pick bus route:",
|
| 307 |
options=long_names,
|
| 308 |
help="Select one bus to view details."
|
| 309 |
)
|
| 310 |
+
st.write(f"### Selected Bus: {search} {bus}")
|
|
|
|
| 311 |
|
| 312 |
# Streamlit checkbox to toggle bus direction
|
| 313 |
if "direction" not in st.session_state:
|
|
|
|
| 349 |
start_time = st.sidebar.time_input("Select a start time", value=None)
|
| 350 |
end_time = st.sidebar.time_input("Select an end time", value=None)
|
| 351 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 352 |
|
| 353 |
print(f"start time {type(start_time)}")
|
| 354 |
print(f"end time {type(end_time)}")
|
|
|
|
| 357 |
if start_time != None and end_time != None:
|
| 358 |
#TODO hur filtrera på tid?
|
| 359 |
st.write(f"Displaying buses between {start_time.strftime('%H:%M')} and {end_time.strftime('%H:%M')} the {day_choice}")
|
| 360 |
+
|
|
|
|
|
|
|
| 361 |
selected_trips = bus_trips[(bus_trips["datetime"] >= datetime.combine(date_options[day_choice], start_time))
|
| 362 |
& (bus_trips["datetime"] <= datetime.combine(date_options[day_choice], end_time))
|
| 363 |
& (bus_trips["direction_id"] == st.session_state.direction )
|
| 364 |
& (bus_trips["stop_name"] == stop_choice)]
|
| 365 |
trip_ids = list(pd.unique(selected_trips["trip_id"]))
|
| 366 |
+
#st.write(f"{len(trip_ids)} buses available")
|
| 367 |
chioce = selected_trips[selected_trips["stop_name"]==stop_choice]
|
| 368 |
+
#chioce.head()
|
| 369 |
#TODO ta bort stop_name:)
|
| 370 |
chioce = chioce[["trip_id", "stop_name", "datetime"]]
|
| 371 |
#Ev lägga stop_chioce i session_state
|
| 372 |
|
| 373 |
chioce = chioce.sort_values(by=["datetime"])
|
| 374 |
+
chioce = drop_the_duplicates(chioce)
|
| 375 |
+
|
| 376 |
+
st.write(f"{chioce['trip_id'].nunique()} buses available")
|
| 377 |
|
| 378 |
for idx, row in chioce.iterrows():
|
| 379 |
+
#st.write(f"The bus arrives at {row['stop_name']} at {row['datetime'].strftime('%H:%M')}")
|
| 380 |
plot_graph_title(st.session_state.data[st.session_state.data["trip_id"]==row["trip_id"]], row["stop_name"], row['datetime'].strftime('%H:%M'))
|
| 381 |
+
visualize(st.session_state.data[st.session_state.data["trip_id"]==row["trip_id"]], stop_choice)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 382 |
|
| 383 |
|
| 384 |
|
|
|
|
| 388 |
|
| 389 |
|
| 390 |
# Display data and graphs
|
| 391 |
+
#if st.session_state.data is not None:
|
| 392 |
#plot_graphs(st.session_state.data)
|
| 393 |
+
|
| 394 |
|
| 395 |
main()
|
| 396 |
|