vgosavi2 commited on
Commit
9dffac3
Β·
verified Β·
1 Parent(s): e6da3fa

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- # 4. Pie Chart 1: Top 10 Crime Types
96
- st.header("Pie Chart 1: Top 10 Crime Types Distribution")
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
- # ── Filtered dataframe ──
107
- filtered = df[df['Year'] == selected_year]
 
108
 
109
- # ── Title with selected year ──
110
- st.title(f"Top 10 Crime Types in {selected_year}")
111
 
112
- # ── Compute top 10 crime types ──
113
  top_crimes = (
114
- filtered['crm_cd_desc']
115
- .value_counts()
116
- .nlargest(10)
117
- .rename_axis('Crime Type')
118
- .reset_index(name='Count')
119
  )
120
- top_crimes["Count"] = pd.to_numeric(top_crimes["Count"], errors="coerce")
121
- top_crimes['Percentage'] = top_crimes['Count'] / top_crimes['Count'].sum()
122
 
123
- # ── Plotly pie chart ──
124
  fig = px.pie(
125
  top_crimes,
126
- names='Crime Type',
127
- values='Count',
128
  hole=0.4,
129
- color_discrete_sequence=px.colors.qualitative.Safe, # prettier palette
 
130
  )
131
 
132
- # ── Style the slices ──
133
  fig.update_traces(
134
- textposition='outside', # labels outside slices
135
- textinfo='label+percent', # show both name and percent
136
- pull=[0.05]*len(top_crimes), # slight β€œpull” for all slices
137
- marker=dict(line=dict(color='white', width=2)) # white borders
138
  )
139
 
140
- # ── Layout adjustments ──
141
  fig.update_layout(
142
- showlegend=True,
143
- legend_title_text='Crime Type',
144
  margin=dict(t=60, b=20, l=20, r=20),
145
- title_x=0.5 # center the main title
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)