sahar-yaccov commited on
Commit
2eabf34
·
verified ·
1 Parent(s): 13f3df7

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
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! 🚶‍♂️🚌")