Spaces:
Build error
Build error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +43 -0
src/streamlit_app.py
CHANGED
|
@@ -12,6 +12,7 @@ from streamlit_folium import st_folium
|
|
| 12 |
import json
|
| 13 |
import os
|
| 14 |
from pathlib import Path
|
|
|
|
| 15 |
|
| 16 |
# point Streamlit at a writable folder
|
| 17 |
os.environ["STREAMLIT_CONFIG_DIR"] = "/tmp/.streamlit"
|
|
@@ -303,6 +304,48 @@ fig.update_layout(
|
|
| 303 |
|
| 304 |
# 4. Render in Streamlit
|
| 305 |
st.plotly_chart(fig, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
|
| 307 |
# -------------------------------- Plot 2: Heat Map --------------------------------
|
| 308 |
st.markdown("<div class='sectionheader'> HeatMap </div>", unsafe_allow_html=True)
|
|
|
|
| 12 |
import json
|
| 13 |
import os
|
| 14 |
from pathlib import Path
|
| 15 |
+
import circlify
|
| 16 |
|
| 17 |
# point Streamlit at a writable folder
|
| 18 |
os.environ["STREAMLIT_CONFIG_DIR"] = "/tmp/.streamlit"
|
|
|
|
| 304 |
|
| 305 |
# 4. Render in Streamlit
|
| 306 |
st.plotly_chart(fig, use_container_width=True)
|
| 307 |
+
##########################################################
|
| 308 |
+
# Aggregate total incidents by region and take top 15
|
| 309 |
+
region_counts = (
|
| 310 |
+
df.groupby("RegionName")
|
| 311 |
+
.size()
|
| 312 |
+
.reset_index(name="Count")
|
| 313 |
+
.nlargest(15, "Count")
|
| 314 |
+
)
|
| 315 |
+
|
| 316 |
+
# 2. Compute circle‐packing layout
|
| 317 |
+
circles = circlify.circlify(
|
| 318 |
+
region_counts["Count"].tolist(),
|
| 319 |
+
show_enclosure=False,
|
| 320 |
+
target_enclosure=circlify.Circle(x=0, y=0, r=1)
|
| 321 |
+
)
|
| 322 |
+
|
| 323 |
+
# 3. Plot packed bubbles
|
| 324 |
+
fig, ax = plt.subplots(figsize=(6, 6))
|
| 325 |
+
ax.axis("off")
|
| 326 |
+
|
| 327 |
+
# Determine plot limits so everything fits
|
| 328 |
+
lim = max(
|
| 329 |
+
abs(circle.x) + circle.r
|
| 330 |
+
for circle in circles
|
| 331 |
+
)
|
| 332 |
+
ax.set_xlim(-lim, lim)
|
| 333 |
+
ax.set_ylim(-lim, lim)
|
| 334 |
+
|
| 335 |
+
# Draw each circle
|
| 336 |
+
for circle, (_, row) in zip(circles, region_counts.iterrows()):
|
| 337 |
+
x, y, r = circle.x, circle.y, circle.r
|
| 338 |
+
ax.add_patch(plt.Circle((x, y), r, alpha=0.6, linewidth=2, ec="white"))
|
| 339 |
+
ax.text(
|
| 340 |
+
x, y,
|
| 341 |
+
f"{row['RegionName']}\n{row['Count']:,}",
|
| 342 |
+
ha="center", va="center", fontsize=8
|
| 343 |
+
)
|
| 344 |
+
|
| 345 |
+
ax.set_title("Packed Bubble: Top 15 Regions by Incident Count", fontsize=14)
|
| 346 |
+
|
| 347 |
+
# 4. Render in Streamlit
|
| 348 |
+
st.pyplot(fig)
|
| 349 |
|
| 350 |
# -------------------------------- Plot 2: Heat Map --------------------------------
|
| 351 |
st.markdown("<div class='sectionheader'> HeatMap </div>", unsafe_allow_html=True)
|