Spaces:
Build error
Build error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +37 -38
src/streamlit_app.py
CHANGED
|
@@ -231,46 +231,47 @@ relying on specific numbers. By pairing each slice with its label, the chart pro
|
|
| 231 |
volume and which are comparatively rare, helping stakeholders focus on the offenses that matter most.</div>""",unsafe_allow_html=True)
|
| 232 |
|
| 233 |
# -------------------------------- Plot 6: Bubble Map of Incident Counts by Region --------------------------------
|
| 234 |
-
st.markdown("<div class='sectionheader'>Crime Hotspots by Region</div>", unsafe_allow_html=True)
|
| 235 |
|
| 236 |
-
# 1. Aggregate counts and centroids
|
| 237 |
-
region_stats = (
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
)
|
| 247 |
|
| 248 |
-
# 2. Build the bubble map
|
| 249 |
-
fig = px.scatter_mapbox(
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
)
|
| 262 |
|
| 263 |
-
# 3. Tidy layout
|
| 264 |
-
fig.update_layout(
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
)
|
| 269 |
|
| 270 |
-
# 4. Render
|
| 271 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 272 |
|
| 273 |
# -------------------------------- Plot 7: Line Chart for Incident Counts by Region --------------------------------
|
|
|
|
| 274 |
# 1. Aggregate total incidents by year
|
| 275 |
yearly_region = (
|
| 276 |
df
|
|
@@ -292,10 +293,7 @@ fig = px.line(
|
|
| 292 |
x="year",
|
| 293 |
y="Count",
|
| 294 |
color="RegionName",
|
| 295 |
-
title=(
|
| 296 |
-
f"Incident Trends Over Time" +
|
| 297 |
-
(f" – {sel_region}" if sel_region!="All" else "")
|
| 298 |
-
),
|
| 299 |
labels={"year":"Year", "Count":"Incident Count"}
|
| 300 |
)
|
| 301 |
|
|
@@ -305,6 +303,7 @@ for trace in fig.data:
|
|
| 305 |
|
| 306 |
st.plotly_chart(fig, use_container_width=True)
|
| 307 |
# -------------------------------- Plot 8: Line Chart for Incident Counts by Region --------------------------------
|
|
|
|
| 308 |
# 1. Compute counts per region and crime
|
| 309 |
counts = (
|
| 310 |
df
|
|
|
|
| 231 |
volume and which are comparatively rare, helping stakeholders focus on the offenses that matter most.</div>""",unsafe_allow_html=True)
|
| 232 |
|
| 233 |
# -------------------------------- Plot 6: Bubble Map of Incident Counts by Region --------------------------------
|
| 234 |
+
# st.markdown("<div class='sectionheader'>Crime Hotspots by Region</div>", unsafe_allow_html=True)
|
| 235 |
|
| 236 |
+
# # 1. Aggregate counts and centroids
|
| 237 |
+
# region_stats = (
|
| 238 |
+
# df
|
| 239 |
+
# .groupby("RegionName")
|
| 240 |
+
# .agg(
|
| 241 |
+
# Count = pd.NamedAgg(column="crm_cd_desc", aggfunc="size"),
|
| 242 |
+
# Latitude = pd.NamedAgg(column="lat", aggfunc="mean"),
|
| 243 |
+
# Longitude = pd.NamedAgg(column="lon", aggfunc="mean")
|
| 244 |
+
# )
|
| 245 |
+
# .reset_index()
|
| 246 |
+
# )
|
| 247 |
|
| 248 |
+
# # 2. Build the bubble map
|
| 249 |
+
# fig = px.scatter_mapbox(
|
| 250 |
+
# region_stats,
|
| 251 |
+
# lat="Latitude",
|
| 252 |
+
# lon="Longitude",
|
| 253 |
+
# size="Count", # bubble size ~ incident volume
|
| 254 |
+
# color="Count", # color gradient for emphasis
|
| 255 |
+
# hover_name="RegionName",
|
| 256 |
+
# hover_data={"Count":True, "Latitude":False, "Longitude":False},
|
| 257 |
+
# size_max=30, # max bubble diameter
|
| 258 |
+
# zoom=10, # adjust to focus your city
|
| 259 |
+
# mapbox_style="open-street-map",
|
| 260 |
+
# title="Crime Volume by Region (Bubble Map)"
|
| 261 |
+
# )
|
| 262 |
|
| 263 |
+
# # 3. Tidy layout
|
| 264 |
+
# fig.update_layout(
|
| 265 |
+
# margin=dict(t=50, b=0, l=0, r=0),
|
| 266 |
+
# legend_title_text="Incident Count",
|
| 267 |
+
# title_x=0.5
|
| 268 |
+
# )
|
| 269 |
|
| 270 |
+
# # 4. Render
|
| 271 |
+
# st.plotly_chart(fig, use_container_width=True)
|
| 272 |
|
| 273 |
# -------------------------------- Plot 7: Line Chart for Incident Counts by Region --------------------------------
|
| 274 |
+
st.markdown("<div class='sectionheader'>Incidents Trends over Time </div>", unsafe_allow_html=True)
|
| 275 |
# 1. Aggregate total incidents by year
|
| 276 |
yearly_region = (
|
| 277 |
df
|
|
|
|
| 293 |
x="year",
|
| 294 |
y="Count",
|
| 295 |
color="RegionName",
|
| 296 |
+
title=(" "),
|
|
|
|
|
|
|
|
|
|
| 297 |
labels={"year":"Year", "Count":"Incident Count"}
|
| 298 |
)
|
| 299 |
|
|
|
|
| 303 |
|
| 304 |
st.plotly_chart(fig, use_container_width=True)
|
| 305 |
# -------------------------------- Plot 8: Line Chart for Incident Counts by Region --------------------------------
|
| 306 |
+
st.markdown("<div class='sectionheader'>Crime Composition by Region: Top 5 Offenses </div>", unsafe_allow_html=True)
|
| 307 |
# 1. Compute counts per region and crime
|
| 308 |
counts = (
|
| 309 |
df
|