Update src/streamlit_app.py
Browse files- src/streamlit_app.py +30 -36
src/streamlit_app.py
CHANGED
|
@@ -91,59 +91,53 @@ st.altair_chart(chart1, use_container_width=True)
|
|
| 91 |
###############################################################
|
| 92 |
|
| 93 |
import plotly.express as px
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
st.sidebar.header("Filters")
|
| 99 |
-
selected_year = st.sidebar.slider(
|
| 100 |
-
"Select Year",
|
| 101 |
-
int(df['Year'].min()),
|
| 102 |
-
int(df['Year'].max()),
|
| 103 |
-
int(df['Year'].max())
|
| 104 |
-
)
|
| 105 |
|
| 106 |
-
# ββ
|
| 107 |
-
|
|
|
|
| 108 |
|
| 109 |
-
|
| 110 |
-
st.title(f"Top 10 Crime Types in {selected_year}")
|
| 111 |
|
| 112 |
-
# ββ Compute top 10 crime types ββ
|
| 113 |
top_crimes = (
|
| 114 |
-
filtered[
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
)
|
| 120 |
-
top_crimes["
|
| 121 |
-
top_crimes['Percentage'] = top_crimes['Count'] / top_crimes['Count'].sum()
|
| 122 |
|
| 123 |
-
# ββ Plotly
|
| 124 |
fig = px.pie(
|
| 125 |
top_crimes,
|
| 126 |
-
names=
|
| 127 |
-
values=
|
| 128 |
hole=0.4,
|
| 129 |
-
color_discrete_sequence=px.colors.qualitative.Safe,
|
|
|
|
| 130 |
)
|
| 131 |
|
| 132 |
-
# ββ Style the slices ββ
|
| 133 |
fig.update_traces(
|
| 134 |
-
textposition=
|
| 135 |
-
textinfo=
|
| 136 |
-
pull=[0.05]*len(top_crimes),
|
| 137 |
-
marker=dict(line=dict(color=
|
| 138 |
)
|
| 139 |
|
| 140 |
-
# ββ Layout adjustments ββ
|
| 141 |
fig.update_layout(
|
| 142 |
-
|
| 143 |
-
legend_title_text='Crime Type',
|
| 144 |
margin=dict(t=60, b=20, l=20, r=20),
|
| 145 |
-
title_x=0.5
|
| 146 |
)
|
| 147 |
|
| 148 |
-
# ββ Render in Streamlit ββ
|
| 149 |
st.plotly_chart(fig, use_container_width=True)
|
|
|
|
| 91 |
###############################################################
|
| 92 |
|
| 93 |
import plotly.express as px
|
| 94 |
+
# ββ 3. Parse year from date ββ
|
| 95 |
+
# Detect the column containing βdateβ
|
| 96 |
+
date_col = next((c for c in df.columns if "date" in c.lower()), None)
|
| 97 |
+
if date_col is None:
|
| 98 |
+
st.error("Could not find a date column in the dataset.")
|
| 99 |
+
st.stop()
|
| 100 |
|
| 101 |
+
df[date_col] = pd.to_datetime(df[date_col], errors="coerce")
|
| 102 |
+
df["Year"] = df[date_col].dt.year
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
# ββ 4. Year filter (above chart) ββ
|
| 105 |
+
years = sorted(df["Year"].dropna().unique())
|
| 106 |
+
selected_year = st.selectbox("Select Year", years, index=len(years)-1)
|
| 107 |
|
| 108 |
+
filtered = df[df["Year"] == selected_year]
|
|
|
|
| 109 |
|
| 110 |
+
# ββ 5. Compute top 10 crime types for that year ββ
|
| 111 |
top_crimes = (
|
| 112 |
+
filtered["crm_cd_desc"]
|
| 113 |
+
.value_counts()
|
| 114 |
+
.nlargest(10)
|
| 115 |
+
.rename_axis("Crime Type")
|
| 116 |
+
.reset_index(name="Count")
|
| 117 |
)
|
| 118 |
+
top_crimes["Percentage"] = top_crimes["Count"] / top_crimes["Count"].sum()
|
|
|
|
| 119 |
|
| 120 |
+
# ββ 6. Plotly donut chart ββ
|
| 121 |
fig = px.pie(
|
| 122 |
top_crimes,
|
| 123 |
+
names="Crime Type",
|
| 124 |
+
values="Count",
|
| 125 |
hole=0.4,
|
| 126 |
+
color_discrete_sequence=px.colors.qualitative.Safe,
|
| 127 |
+
title=f"Top 10 Crime Types in {selected_year}"
|
| 128 |
)
|
| 129 |
|
|
|
|
| 130 |
fig.update_traces(
|
| 131 |
+
textposition="outside",
|
| 132 |
+
textinfo="label+percent",
|
| 133 |
+
pull=[0.05] * len(top_crimes),
|
| 134 |
+
marker=dict(line=dict(color="white", width=2))
|
| 135 |
)
|
| 136 |
|
|
|
|
| 137 |
fig.update_layout(
|
| 138 |
+
legend_title_text="Crime Type",
|
|
|
|
| 139 |
margin=dict(t=60, b=20, l=20, r=20),
|
| 140 |
+
title_x=0.5
|
| 141 |
)
|
| 142 |
|
|
|
|
| 143 |
st.plotly_chart(fig, use_container_width=True)
|