Spaces:
Runtime error
Runtime error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +71 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,73 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from streamlit_folium import st_folium
|
| 3 |
+
import folium
|
| 4 |
+
import requests
|
| 5 |
+
from app.hooks.use_distance import haversine, estimate_time
|
| 6 |
+
from app.hooks.use_alert import check_alert
|
| 7 |
+
from app.components.bus_stop_search import bus_stop_search_ui
|
| 8 |
+
from streamlit_autorefresh import st_autorefresh
|
| 9 |
|
| 10 |
+
# --- קונפיג ---
|
| 11 |
+
IP_GEO_URL = "http://ip-api.com/json"
|
| 12 |
+
API_URL = "http://127.0.0.1:8000/location" # אפשר לשלוח לשרת
|
| 13 |
+
|
| 14 |
+
# --- Streamlit Setup ---
|
| 15 |
+
st.set_page_config(page_title="🚌 Bus Stop Alert", layout="wide")
|
| 16 |
+
st.title("🚌 Bus Stop Alert")
|
| 17 |
+
st.write("Welcome! Enter a destination and start tracking your route. 🚀")
|
| 18 |
+
|
| 19 |
+
# --- Destination ---
|
| 20 |
+
destination_coords = bus_stop_search_ui()
|
| 21 |
+
if not destination_coords:
|
| 22 |
+
st.info("Please enter a destination to start tracking.")
|
| 23 |
+
st.stop()
|
| 24 |
+
else:
|
| 25 |
+
st.success(f"✅ Destination set at: {destination_coords[0]:.6f}, {destination_coords[1]:.6f}")
|
| 26 |
+
|
| 27 |
+
# --- עדכון אוטומטי כל 15 שניות ---
|
| 28 |
+
st_autorefresh(interval=15000, limit=None, key="gps_refresh") # 15,000ms = 15 שניות
|
| 29 |
+
|
| 30 |
+
# --- פונקציה לקבלת מיקום לפי IP ---
|
| 31 |
+
def get_location_from_ip():
|
| 32 |
+
try:
|
| 33 |
+
res = requests.get(IP_GEO_URL, timeout=5)
|
| 34 |
+
data = res.json()
|
| 35 |
+
if data.get("status") == "success":
|
| 36 |
+
lat, lon = data["lat"], data["lon"]
|
| 37 |
+
# אפשר לשלוח לשרת
|
| 38 |
+
try:
|
| 39 |
+
requests.post(API_URL, json={"lat": lat, "lon": lon}, timeout=3)
|
| 40 |
+
except:
|
| 41 |
+
pass
|
| 42 |
+
return (lat, lon)
|
| 43 |
+
except:
|
| 44 |
+
pass
|
| 45 |
+
# fallback Tel Aviv אם נכשל
|
| 46 |
+
return (32.0853, 34.7818)
|
| 47 |
+
|
| 48 |
+
# --- קבלת מיקום נוכחי ---
|
| 49 |
+
user_coords = get_location_from_ip()
|
| 50 |
+
st.success(f"📍 Your current location (IP-based): {user_coords[0]:.6f}, {user_coords[1]:.6f}")
|
| 51 |
+
|
| 52 |
+
# --- Map ---
|
| 53 |
+
m = folium.Map(location=user_coords, zoom_start=13)
|
| 54 |
+
folium.Marker(user_coords, tooltip="You are here", icon=folium.Icon(color="blue")).add_to(m)
|
| 55 |
+
folium.Marker(destination_coords, tooltip="Destination", icon=folium.Icon(color="red")).add_to(m)
|
| 56 |
+
folium.PolyLine([user_coords, destination_coords], color="green", weight=3, opacity=0.7).add_to(m)
|
| 57 |
+
st_folium(m, width=700, height=500)
|
| 58 |
+
|
| 59 |
+
# --- Distance & Time ---
|
| 60 |
+
distance_km = haversine(user_coords, destination_coords)
|
| 61 |
+
st.metric("Distance to destination", f"{distance_km:.2f} km")
|
| 62 |
+
|
| 63 |
+
speed_kmh = st.number_input("Estimated speed (km/h)", value=30.0, min_value=1.0)
|
| 64 |
+
time_min = estimate_time(distance_km, speed_kmh)
|
| 65 |
+
st.metric("Estimated travel time", f"{time_min:.1f} min")
|
| 66 |
+
|
| 67 |
+
# --- Alert ---
|
| 68 |
+
arrived = check_alert(time_min)
|
| 69 |
+
if arrived:
|
| 70 |
+
st.balloons()
|
| 71 |
+
st.success("🎉 Congratulations! You have arrived at your destination.")
|
| 72 |
+
else:
|
| 73 |
+
st.info("Tracking in progress... Keep moving towards your destination! 🚶♂️🚌")
|