Update src/streamlit_app.py
Browse files- src/streamlit_app.py +5 -42
src/streamlit_app.py
CHANGED
|
@@ -41,7 +41,11 @@ DATA_PATH = Path(__file__).parent / "crime_data.csv" # /app/src/crime_dat
|
|
| 41 |
@st.cache_data
|
| 42 |
def load_data():
|
| 43 |
return pd.read_csv(DATA_PATH)
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
# 2. Load and early‐exit if missing
|
| 46 |
df = load_data()
|
| 47 |
if df.empty:
|
|
@@ -52,48 +56,8 @@ st.header("Data Preview")
|
|
| 52 |
st.write(f"Total records: {df.shape[0]} | Total columns: {df.shape[1]}")
|
| 53 |
st.dataframe(df.head())
|
| 54 |
|
| 55 |
-
# # 4. Pie Chart 1: Top 10 Crime Types
|
| 56 |
-
# st.header("Pie Chart 1: Top 10 Crime Types Distribution")
|
| 57 |
-
|
| 58 |
-
# # Prepare Top 10 crime types
|
| 59 |
-
# top_crimes = (
|
| 60 |
-
# df["crm_cd_desc"]
|
| 61 |
-
# .value_counts() # Series indexed by crime type
|
| 62 |
-
# .nlargest(10) # keep only the top 10
|
| 63 |
-
# .rename_axis("Crime Type") # name that index “Crime Type”
|
| 64 |
-
# .reset_index(name="Count") # turn index into a column “Crime Type” and values into “Count”
|
| 65 |
-
# )
|
| 66 |
-
|
| 67 |
-
# # Verify your columns are exactly what you expect
|
| 68 |
-
# st.dataframe(top_crimes) # should show “Crime Type” and “Count” with correct numbers
|
| 69 |
-
|
| 70 |
-
# # Now compute percentage
|
| 71 |
-
# total = top_crimes["Count"].sum()
|
| 72 |
-
# top_crimes["Percentage"] = top_crimes["Count"] / total
|
| 73 |
-
# top_crimes["Count"] = pd.to_numeric(top_crimes["Count"], errors="coerce")
|
| 74 |
-
|
| 75 |
-
# # And finally your pie‐chart…
|
| 76 |
-
# chart1 = (
|
| 77 |
-
# alt.Chart(top_crimes)
|
| 78 |
-
# .mark_arc(innerRadius=50)
|
| 79 |
-
# .encode(
|
| 80 |
-
# theta=alt.Theta("Count:Q"),
|
| 81 |
-
# color=alt.Color("Crime Type:N", legend=alt.Legend(title="Crime Type")),
|
| 82 |
-
# tooltip=[
|
| 83 |
-
# alt.Tooltip("Crime Type:N"),
|
| 84 |
-
# alt.Tooltip("Count:Q"),
|
| 85 |
-
# alt.Tooltip("Percentage:Q", format=".1%")
|
| 86 |
-
# ]
|
| 87 |
-
# )
|
| 88 |
-
# .properties(width=400, height=400, title="Top 10 Crime Types")
|
| 89 |
-
# )
|
| 90 |
-
# st.altair_chart(chart1, use_container_width=True)
|
| 91 |
-
|
| 92 |
-
###############################################################
|
| 93 |
-
|
| 94 |
# Pie Chart 1: Top 10 Crime Types
|
| 95 |
years = sorted(df["year"].dropna().astype(int).unique())
|
| 96 |
-
# selected_year = st.selectbox("Select Year", years, index=len(years)-1)
|
| 97 |
|
| 98 |
# Year filter (shorter, above chart)
|
| 99 |
col_empty, col_filter = st.columns([3,1])
|
|
@@ -108,7 +72,6 @@ with col_filter:
|
|
| 108 |
# Filter data for that year
|
| 109 |
filtered = df[df["year"] == selected_year]
|
| 110 |
|
| 111 |
-
|
| 112 |
# Compute top 10 crime types for that year ──
|
| 113 |
top_crimes = (
|
| 114 |
filtered["crm_cd_desc"]
|
|
|
|
| 41 |
@st.cache_data
|
| 42 |
def load_data():
|
| 43 |
return pd.read_csv(DATA_PATH)
|
| 44 |
+
|
| 45 |
+
if st.button("🔄"):
|
| 46 |
+
st.cache_data.clear() # Clear the cache
|
| 47 |
+
st.success("Data is refreshed") # Reload the data
|
| 48 |
+
|
| 49 |
# 2. Load and early‐exit if missing
|
| 50 |
df = load_data()
|
| 51 |
if df.empty:
|
|
|
|
| 56 |
st.write(f"Total records: {df.shape[0]} | Total columns: {df.shape[1]}")
|
| 57 |
st.dataframe(df.head())
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
# Pie Chart 1: Top 10 Crime Types
|
| 60 |
years = sorted(df["year"].dropna().astype(int).unique())
|
|
|
|
| 61 |
|
| 62 |
# Year filter (shorter, above chart)
|
| 63 |
col_empty, col_filter = st.columns([3,1])
|
|
|
|
| 72 |
# Filter data for that year
|
| 73 |
filtered = df[df["year"] == selected_year]
|
| 74 |
|
|
|
|
| 75 |
# Compute top 10 crime types for that year ──
|
| 76 |
top_crimes = (
|
| 77 |
filtered["crm_cd_desc"]
|