Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ import altair as alt
|
|
| 7 |
import pydeck as pdk
|
| 8 |
from datetime import datetime, timedelta
|
| 9 |
|
| 10 |
-
# Mapbox demo token
|
| 11 |
|
| 12 |
# if not os.getenv("MAPBOX_API_KEY"):
|
| 13 |
# pdk.settings.mapbox_api_key = (
|
|
@@ -15,9 +15,7 @@ from datetime import datetime, timedelta
|
|
| 15 |
# "w5iJj8yOTFoNUG0GI_LhwA"
|
| 16 |
# )
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
# 2. CONSTANTS & DATA HELPERS
|
| 20 |
-
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 21 |
RESOURCE_ID = "15408d78-9734-4ea1-b3e5-a0f99568dd9b"
|
| 22 |
BASE_URL = "https://data.sanjoseca.gov/api/3/action/datastore_search"
|
| 23 |
PAGE_LIMIT = 1_000
|
|
@@ -30,7 +28,7 @@ def fetch_data() -> pd.DataFrame:
|
|
| 30 |
try:
|
| 31 |
batch = requests.get(BASE_URL, params=params, timeout=20).json()["result"]["records"]
|
| 32 |
except Exception:
|
| 33 |
-
st.error("
|
| 34 |
return pd.DataFrame(columns=["CrashDateTime"])
|
| 35 |
if not batch:
|
| 36 |
break
|
|
@@ -59,9 +57,7 @@ def time_filter(df: pd.DataFrame, months: str) -> pd.DataFrame:
|
|
| 59 |
start = end - timedelta(days=int(months) * 30)
|
| 60 |
return df[(df["CrashDateTime"] >= start) & (df["CrashDateTime"] <= end)]
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
# 3. PAGE HEADER, MOTIVATION, DATASET INFO, REFRESH
|
| 64 |
-
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 65 |
st.set_page_config("Sanβ―JosΓ© Crash Dashboard", layout="wide", page_icon="π¦")
|
| 66 |
|
| 67 |
st.title("π¦Β Sanβ―JosΓ© Crash Safety Explorer")
|
|
@@ -82,9 +78,7 @@ with right:
|
|
| 82 |
if st.button("πΒ Refresh"):
|
| 83 |
st.cache_data.clear()
|
| 84 |
|
| 85 |
-
#
|
| 86 |
-
# 4. FILTERS
|
| 87 |
-
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 88 |
months = st.selectbox(
|
| 89 |
"Select time range to explore",
|
| 90 |
["6", "9", "12", "All"],
|
|
@@ -93,9 +87,7 @@ months = st.selectbox(
|
|
| 93 |
)
|
| 94 |
st.divider()
|
| 95 |
|
| 96 |
-
#
|
| 97 |
-
# 5. DATA LOAD
|
| 98 |
-
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 99 |
df = time_filter(preprocess(fetch_data()), months)
|
| 100 |
if df.empty:
|
| 101 |
st.warning("No data available for the selected period.")
|
|
@@ -112,9 +104,7 @@ def two_col_layout(chart, narrative_md):
|
|
| 112 |
with story:
|
| 113 |
st.markdown(narrative_md, unsafe_allow_html=True)
|
| 114 |
|
| 115 |
-
#
|
| 116 |
-
# 6. NAVIGATION TABS
|
| 117 |
-
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 118 |
tabs = st.tabs([
|
| 119 |
"Weather & Injuries",
|
| 120 |
"Speeding Trend",
|
|
@@ -250,7 +240,7 @@ While asphalt dominates totals, **wet/oily** pavement poses disproportionate ris
|
|
| 250 |
"""
|
| 251 |
)
|
| 252 |
|
| 253 |
-
# βββββββββ TABΒ 5 β INJURY CORRELATION
|
| 254 |
with tabs[4]:
|
| 255 |
# st.header("Correlation Among Injury Stats")
|
| 256 |
# st.markdown("Numeric relationships between injury types and year.")
|
|
@@ -286,9 +276,7 @@ Use this matrix to spot redundant metrics or unexpected links worth deeper inves
|
|
| 286 |
"""
|
| 287 |
)
|
| 288 |
|
| 289 |
-
#
|
| 290 |
-
# 7. CONCLUSION & FOOTER
|
| 291 |
-
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 292 |
st.divider()
|
| 293 |
st.markdown("""
|
| 294 |
## Conclusion
|
|
@@ -301,4 +289,4 @@ Insights support:
|
|
| 301 |
|
| 302 |
Continued monitoring keeps stakeholders aligned on progress toward the cityβs goal of *zero traffic deaths*.
|
| 303 |
""")
|
| 304 |
-
st.caption("© Cityβ―ofΒ Sanβ―JosΓ© OpenβDataΒ β’ Dashboard for ISβ―445β―/β―
|
|
|
|
| 7 |
import pydeck as pdk
|
| 8 |
from datetime import datetime, timedelta
|
| 9 |
|
| 10 |
+
### If CrashMap is not working on the local system- Uncomment the belwo Mapbox demo token
|
| 11 |
|
| 12 |
# if not os.getenv("MAPBOX_API_KEY"):
|
| 13 |
# pdk.settings.mapbox_api_key = (
|
|
|
|
| 15 |
# "w5iJj8yOTFoNUG0GI_LhwA"
|
| 16 |
# )
|
| 17 |
|
| 18 |
+
### CONSTANTS & DATA HELPERS
|
|
|
|
|
|
|
| 19 |
RESOURCE_ID = "15408d78-9734-4ea1-b3e5-a0f99568dd9b"
|
| 20 |
BASE_URL = "https://data.sanjoseca.gov/api/3/action/datastore_search"
|
| 21 |
PAGE_LIMIT = 1_000
|
|
|
|
| 28 |
try:
|
| 29 |
batch = requests.get(BASE_URL, params=params, timeout=20).json()["result"]["records"]
|
| 30 |
except Exception:
|
| 31 |
+
st.error("Cannot reach the Sanβ―JosΓ© OpenβData API.")
|
| 32 |
return pd.DataFrame(columns=["CrashDateTime"])
|
| 33 |
if not batch:
|
| 34 |
break
|
|
|
|
| 57 |
start = end - timedelta(days=int(months) * 30)
|
| 58 |
return df[(df["CrashDateTime"] >= start) & (df["CrashDateTime"] <= end)]
|
| 59 |
|
| 60 |
+
### PAGE HEADER, MOTIVATION, DATASET INFO, REFRESH
|
|
|
|
|
|
|
| 61 |
st.set_page_config("Sanβ―JosΓ© Crash Dashboard", layout="wide", page_icon="π¦")
|
| 62 |
|
| 63 |
st.title("π¦Β Sanβ―JosΓ© Crash Safety Explorer")
|
|
|
|
| 78 |
if st.button("πΒ Refresh"):
|
| 79 |
st.cache_data.clear()
|
| 80 |
|
| 81 |
+
### FILTERS
|
|
|
|
|
|
|
| 82 |
months = st.selectbox(
|
| 83 |
"Select time range to explore",
|
| 84 |
["6", "9", "12", "All"],
|
|
|
|
| 87 |
)
|
| 88 |
st.divider()
|
| 89 |
|
| 90 |
+
### DATA LOAD
|
|
|
|
|
|
|
| 91 |
df = time_filter(preprocess(fetch_data()), months)
|
| 92 |
if df.empty:
|
| 93 |
st.warning("No data available for the selected period.")
|
|
|
|
| 104 |
with story:
|
| 105 |
st.markdown(narrative_md, unsafe_allow_html=True)
|
| 106 |
|
| 107 |
+
### NAVIGATION TABS
|
|
|
|
|
|
|
| 108 |
tabs = st.tabs([
|
| 109 |
"Weather & Injuries",
|
| 110 |
"Speeding Trend",
|
|
|
|
| 240 |
"""
|
| 241 |
)
|
| 242 |
|
| 243 |
+
# βββββββββ TABΒ 5 β INJURY CORRELATION βββββββββ
|
| 244 |
with tabs[4]:
|
| 245 |
# st.header("Correlation Among Injury Stats")
|
| 246 |
# st.markdown("Numeric relationships between injury types and year.")
|
|
|
|
| 276 |
"""
|
| 277 |
)
|
| 278 |
|
| 279 |
+
### CONCLUSION & FOOTER
|
|
|
|
|
|
|
| 280 |
st.divider()
|
| 281 |
st.markdown("""
|
| 282 |
## Conclusion
|
|
|
|
| 289 |
|
| 290 |
Continued monitoring keeps stakeholders aligned on progress toward the cityβs goal of *zero traffic deaths*.
|
| 291 |
""")
|
| 292 |
+
st.caption("© Cityβ―ofΒ Sanβ―JosΓ© OpenβDataΒ β’ Dashboard for ISβ―445β―/β―Group2 - Aniruddha Jenifer Radha Sanidhya Smruti")
|