namdini commited on
Commit
dae512c
·
verified ·
1 Parent(s): a6022f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -28
app.py CHANGED
@@ -2,7 +2,11 @@ import streamlit as st
2
  import pandas as pd
3
  import plotly.express as px
4
  import altair as alt
 
 
 
5
 
 
6
  def load_and_preprocess_data(file_path):
7
  # Read the data
8
  df = pd.read_csv(file_path)
@@ -100,6 +104,31 @@ def get_top_violations(df, age_group):
100
 
101
  return violations_df.head()
102
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  def create_injuries_fatalities_chart(crash_data):
104
 
105
  # 5th visualization title
@@ -220,35 +249,56 @@ def main():
220
  # Load data
221
  df = load_and_preprocess_data('1.08_Crash_Data_Report_(detail).csv')
222
 
223
- # Create simple dropdown for age groups
224
- age_groups = ['All Ages', '16-25', '26-35', '36-45', '46-55', '56-65', '65+']
225
- selected_age = st.selectbox('Select Age Group:', age_groups)
226
 
227
- # Create and display crash severity vs violation type chart
228
- fig = create_severity_violation_chart(df, selected_age)
229
- st.plotly_chart(fig, use_container_width=True)
230
-
231
- # Display statistics
232
- if selected_age == 'All Ages':
233
- total_incidents = len(df)
234
- else:
235
- total_incidents = len(df[
236
- (df['Age_Group_Drv1'] == selected_age) |
237
- (df['Age_Group_Drv2'] == selected_age)
238
- ])
239
-
240
- # Create two columns for statistics
241
- col1, col2 = st.columns(2)
242
-
243
- with col1:
244
- st.markdown(f"### Total Incidents")
245
- st.markdown(f"**{total_incidents:,}** incidents for {selected_age}")
 
 
 
 
 
 
 
 
 
 
246
 
247
- # Display top violations table
248
- with col2:
249
- st.markdown("### Top Violations")
250
- top_violations = get_top_violations(df, selected_age)
251
- st.table(top_violations)
 
 
 
 
 
 
 
 
 
 
 
 
252
 
253
  # Create 5th Visualization: Injuries and fatalities chart
254
  injuries_fatalities_chart = create_injuries_fatalities_chart(df)
@@ -257,4 +307,4 @@ def main():
257
 
258
 
259
  if __name__ == "__main__":
260
- main()
 
2
  import pandas as pd
3
  import plotly.express as px
4
  import altair as alt
5
+ import folium
6
+ from folium.plugins import HeatMap, MarkerCluster
7
+ from streamlit_folium import st_folium
8
 
9
+ @st.cache_data
10
  def load_and_preprocess_data(file_path):
11
  # Read the data
12
  df = pd.read_csv(file_path)
 
104
 
105
  return violations_df.head()
106
 
107
+ @st.cache_data
108
+ def create_map(df, selected_year):
109
+ filtered_df = df[df['Year'] == selected_year]
110
+
111
+ m = folium.Map(
112
+ location=[33.4255, -111.9400],
113
+ zoom_start=12,
114
+ control_scale=True,
115
+ tiles='CartoDB positron'
116
+ )
117
+
118
+ marker_cluster = MarkerCluster().add_to(m)
119
+
120
+ for _, row in filtered_df.iterrows():
121
+ folium.Marker(
122
+ location=[row['Latitude'], row['Longitude']],
123
+ popup=f"Accident at {row['Longitude']}, {row['Latitude']}<br>Date: {row['DateTime']}<br>Severity: {row['Injuryseverity']}",
124
+ icon=folium.Icon(color='red')
125
+ ).add_to(marker_cluster)
126
+
127
+ heat_data = filtered_df[['Latitude', 'Longitude']].values.tolist()
128
+ HeatMap(heat_data, radius=15, max_zoom=13, min_opacity=0.3).add_to(m)
129
+
130
+ return m
131
+
132
  def create_injuries_fatalities_chart(crash_data):
133
 
134
  # 5th visualization title
 
249
  # Load data
250
  df = load_and_preprocess_data('1.08_Crash_Data_Report_(detail).csv')
251
 
252
+ # Create tabs for different visualizations
253
+ tab1, tab2 = st.tabs(["Crash Statistics", "Crash Map"])
 
254
 
255
+ with tab1:
256
+ # Age group selection
257
+ age_groups = ['All Ages', '16-25', '26-35', '36-45', '46-55', '56-65', '65+']
258
+ selected_age = st.selectbox('Select Age Group:', age_groups)
259
+
260
+ # Create and display chart
261
+ fig = create_severity_violation_chart(df, selected_age)
262
+ st.plotly_chart(fig, use_container_width=True)
263
+
264
+ # Display statistics
265
+ if selected_age == 'All Ages':
266
+ total_incidents = len(df)
267
+ else:
268
+ total_incidents = len(df[
269
+ (df['Age_Group_Drv1'] == selected_age) |
270
+ (df['Age_Group_Drv2'] == selected_age)
271
+ ])
272
+
273
+ # Create two columns for statistics
274
+ col1, col2 = st.columns(2)
275
+
276
+ with col1:
277
+ st.markdown(f"### Total Incidents")
278
+ st.markdown(f"**{total_incidents:,}** incidents for {selected_age}")
279
+
280
+ with col2:
281
+ st.markdown("### Top Violations")
282
+ top_violations = get_top_violations(df, selected_age)
283
+ st.table(top_violations)
284
 
285
+ with tab2:
286
+ # Year selection for map
287
+ years = sorted(df['Year'].unique())
288
+ selected_year = st.selectbox('Select Year:', years)
289
+
290
+ # Create and display map
291
+ st.markdown("### Crash Location Map")
292
+ map_placeholder = st.empty()
293
+ with map_placeholder:
294
+ m = create_map(df, selected_year)
295
+ map_data = st_folium(
296
+ m,
297
+ width=800,
298
+ height=600,
299
+ key=f"map_{selected_year}",
300
+ returned_objects=["null_drawing"]
301
+ )
302
 
303
  # Create 5th Visualization: Injuries and fatalities chart
304
  injuries_fatalities_chart = create_injuries_fatalities_chart(df)
 
307
 
308
 
309
  if __name__ == "__main__":
310
+ main()