Nirmal16 commited on
Commit
2b3866a
·
verified ·
1 Parent(s): 8307b9d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -2
app.py CHANGED
@@ -243,14 +243,45 @@ def create_injuries_fatalities_chart(crash_data):
243
 
244
  return line_chart
245
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246
  def main():
247
  st.title('Traffic Crash Analysis')
248
 
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
@@ -299,7 +330,17 @@ def main():
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)
305
  st.altair_chart(injuries_fatalities_chart, use_container_width=True)
 
243
 
244
  return line_chart
245
 
246
+ def create_crash_trend_chart(df, weather=None):
247
+ """
248
+ Creates a line graph showing crash trends over time, optionally filtered by weather condition.
249
+ """
250
+ # Filter by weather condition if provided
251
+ if weather and weather != 'All Conditions':
252
+ df = df[df['Weather'] == weather]
253
+
254
+ # Group data by year and count unique Incident IDs
255
+ trend_data = df.groupby('Year')['Incidentid'].nunique().reset_index()
256
+ trend_data.columns = ['Year', 'Crash Count']
257
+
258
+ # Create line graph
259
+ fig = px.line(
260
+ trend_data,
261
+ x='Year',
262
+ y='Crash Count',
263
+ title=f'Crash Trend Over Time ({weather})',
264
+ labels={'Year': 'Year', 'Crash Count': 'Number of Unique Crashes'},
265
+ markers=True,
266
+ height=600
267
+ )
268
+
269
+ fig.update_traces(line=dict(width=2), marker=dict(size=8))
270
+ fig.update_layout(legend_title_text='Trend')
271
+
272
+ return fig
273
+
274
  def main():
275
  st.title('Traffic Crash Analysis')
276
 
277
  # Load data
278
  df = load_and_preprocess_data('1.08_Crash_Data_Report_(detail).csv')
279
+
280
+ if 'Weather' not in df.columns:
281
+ df['Weather'] = 'Unknown'
282
 
283
  # Create tabs for different visualizations
284
+ tab1, tab2, tab3 = st.tabs(["Crash Statistics", "Crash Map", "Crash Trend"])
285
 
286
  with tab1:
287
  # Age group selection
 
330
  key=f"map_{selected_year}",
331
  returned_objects=["null_drawing"]
332
  )
333
+
334
+ with tab3:
335
+ # Weather condition filter
336
+ weather = ['All Conditions'] + sorted(df['Weather'].unique())
337
+ selected_weather = st.selectbox('Select Weather Condition:', weather)
338
+
339
+ # Create and display line graph
340
+ st.markdown("### Crash Trend Over Time")
341
+ trend_fig = create_crash_trend_chart(df, selected_weather)
342
+ st.plotly_chart(trend_fig, use_container_width=True)
343
+
344
  # Create 5th Visualization: Injuries and fatalities chart
345
  injuries_fatalities_chart = create_injuries_fatalities_chart(df)
346
  st.altair_chart(injuries_fatalities_chart, use_container_width=True)