hyzhang00 commited on
Commit
fb187c0
·
verified ·
1 Parent(s): ffa7f90

layout update

Browse files
Files changed (1) hide show
  1. app.py +141 -50
app.py CHANGED
@@ -303,15 +303,42 @@ def create_category_distribution_chart(df, selected_category, selected_year):
303
  return fig
304
 
305
  def main():
306
- st.title('Traffic Accident Dataset')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
307
 
308
  st.markdown("""
309
  **Team Members:**
310
  - Janhavi Tushar Zarapkar ([email protected]
311
  - Hangyue Zhang ([email protected])
312
  - Andrew Nam ([email protected])
313
- - Nirmal Attarde
314
- - Maanas Sandeep Agrawa
315
  """)
316
 
317
 
@@ -368,20 +395,24 @@ def main():
368
  years = sorted(df['Year'].unique())
369
  selected_year = st.selectbox('Select Year:', years)
370
 
371
- # Create and display map
372
- st.markdown("### Crash Location Map")
373
- map_placeholder = st.empty()
374
- with map_placeholder:
375
- m = create_map(df, selected_year)
376
- map_data = st_folium(
377
- m,
378
- width=800,
379
- height=600,
380
- key=f"map_{selected_year}",
381
- returned_objects=["null_drawing"]
382
- )
383
-
384
- st.markdown("""
 
 
 
 
385
  ### Traffic Crash Location Map
386
  This interactive map visualizes traffic accidents in Tempe for the selected year. It combines **marker clustering** and a **heatmap** to show:
387
  1. **Accident Markers**: Red markers indicate individual accidents, with popups displaying the coordinates, date/time, and severity of each incident.
@@ -397,7 +428,6 @@ def main():
397
 
398
  This map provides insights into accident trends and can help guide safety improvements in the city.
399
  """)
400
-
401
 
402
 
403
  with tab3:
@@ -406,23 +436,31 @@ def main():
406
  selected_weather = st.selectbox('Select Weather Condition:', weather)
407
 
408
  # Create and display line graph
409
- st.markdown("### Crash Trend Over Time")
410
- trend_fig = create_crash_trend_chart(df, selected_weather)
411
- st.plotly_chart(trend_fig, use_container_width=True)
412
 
413
- st.markdown("""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
414
  ## **Crash Trend Over Time**
415
  This interactive line chart visualizes the trend of unique traffic crashes over the years, optionally filtered by weather conditions. It highlights how crash frequency changes over time, helping identify trends and potential contributing factors.
416
-
417
  **Key Features:**
418
  * **Time Trend Analysis**: Displays the total number of unique crashes for each year, showing long-term patterns.
419
  * **Weather Filter**: Users can filter the data by weather conditions (e.g., "Rainy", "Sunny") to analyze how weather impacts crash trends.
420
  * **Interactive Tooltips**: Hovering over data points reveals the exact crash count for each year, providing detailed insights.
421
-
422
  **Color Scheme and Design:**
423
  * **Line and Markers**: A smooth line connects data points, with prominent markers for each year to highlight trends clearly.
424
  * **Dynamic Title**: The chart updates its title to reflect the selected weather condition or "All Conditions" for the overall trend.
425
-
426
  **Insights:**
427
  This chart helps uncover:
428
  * Annual fluctuations in crash incidents.
@@ -441,18 +479,62 @@ def main():
441
  unit_type_pairs = sorted(list(unit_type_pairs))
442
  unit_type = st.selectbox("Select Unit Type Pair", options=['Total'] + unit_type_pairs)
443
 
444
- # Create 5th Visualization: Injuries and fatalities chart
445
- injuries_fatalities_chart = create_injuries_fatalities_chart(df, unit_type)
446
- st.altair_chart(injuries_fatalities_chart, use_container_width=True)
447
- st.markdown("""
448
- This line chart shows the **total number of injuries and fatalities by month for the selected unit type pair**. The blue line represents total injuries, while the red line represents total fatalities. Observing the trends over the months can help identify any seasonal patterns or peaks in traffic incidents involving specific unit types.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
449
 
450
- - **Total Injuries**: The blue line indicates how injuries vary over different months, highlighting any particular spikes or declines.
451
- - **Total Fatalities**: The red line shows the trend for fatalities, which is generally much lower compared to injuries.
452
- - **Unit Types**: The dropdown selection allows users to filter the data by specific unit type pairs (e.g., Driver vs Pedestrian) or view the overall trend across all types.
453
 
454
- This visualization aims to provide an intuitive understanding of how injuries and fatalities are distributed across the year, helping stakeholders develop targeted safety measures.
455
- """)
456
 
457
  with tab5:
458
  # Dropdown for category selection
@@ -470,22 +552,31 @@ def main():
470
  years = ['All Years'] + sorted(df['Year'].dropna().unique().astype(int).tolist())
471
  selected_year = st.selectbox("Select Year:", years)
472
 
473
- # Generate and display the distribution chart
474
- st.markdown(f"### Distribution of Incidents by {selected_category}")
475
- distribution_chart = create_category_distribution_chart(df, selected_category, selected_year)
476
- st.plotly_chart(distribution_chart, use_container_width=True)
477
-
478
- st.markdown("""
479
- ## Distribution by Category
480
- This visualization explores the distribution of traffic incidents across various categories, such as Collision Manner, Weather, Surface Condition, Alcohol Use, and Driver Gender. Each bar represents a specific category value (e.g., "Male" or "Female" for Gender), and the bars are divided into segments based on Injury Severity (e.g., Minor, Moderate, Serious, Fatal).
481
-
482
- **Key features include:**
483
- * Interactive Filters: Select a category and filter by year to analyze trends over time.
484
- * Insightful Tooltips: Hover over each segment to view the exact count and percentage of incidents for a given severity level.
485
- * Comparative Analysis: Quickly identify how different conditions or behaviors correlate with injury severity.
486
 
487
- This chart provides actionable insights into factors contributing to traffic incidents and their outcomes, helping stakeholders target interventions and improve road safety.
488
- """)
 
 
 
 
 
 
 
 
 
 
489
 
490
 
491
  if __name__ == "__main__":
 
303
  return fig
304
 
305
  def main():
306
+ st.set_page_config(page_title="Terrific Tempe Traffic", layout="wide")
307
+
308
+ st.markdown("""
309
+ <style>
310
+ .reportview-container {
311
+ font-size: 20px;
312
+ }
313
+ h1, h2, h3, h4, h5, h6 {
314
+ font-size: 150%;
315
+ }
316
+ p {
317
+ font-size: 125%;
318
+ }
319
+ </style>
320
+ """, unsafe_allow_html=True)
321
+
322
+
323
+ st.markdown("""
324
+ <style>
325
+ .title {
326
+ text-align: center;
327
+ padding: 25px;
328
+ }
329
+ </style>
330
+ """, unsafe_allow_html=True)
331
+
332
+ st.markdown("<div class='title'><h1> Accident Analysis for City of Tempe,Arizona </h1></div>", unsafe_allow_html=True)
333
+
334
 
335
  st.markdown("""
336
  **Team Members:**
337
  - Janhavi Tushar Zarapkar ([email protected]
338
  - Hangyue Zhang ([email protected])
339
  - Andrew Nam ([email protected])
340
+ - Nirmal Attarde ([email protected])
341
+ - Maanas Sandeep Agrawa ([email protected])
342
  """)
343
 
344
 
 
395
  years = sorted(df['Year'].unique())
396
  selected_year = st.selectbox('Select Year:', years)
397
 
398
+ map_col, desc_col = st.columns([7, 3])
399
+
400
+ with map_col:
401
+ # Create and display map
402
+ st.markdown("### Crash Location Map")
403
+ map_placeholder = st.empty()
404
+ with map_placeholder:
405
+ m = create_map(df, selected_year)
406
+ map_data = st_folium(
407
+ m,
408
+ width=None,
409
+ height=800,
410
+ key=f"map_{selected_year}",
411
+ returned_objects=["null_drawing"]
412
+ )
413
+
414
+ with desc_col:
415
+ st.markdown("""
416
  ### Traffic Crash Location Map
417
  This interactive map visualizes traffic accidents in Tempe for the selected year. It combines **marker clustering** and a **heatmap** to show:
418
  1. **Accident Markers**: Red markers indicate individual accidents, with popups displaying the coordinates, date/time, and severity of each incident.
 
428
 
429
  This map provides insights into accident trends and can help guide safety improvements in the city.
430
  """)
 
431
 
432
 
433
  with tab3:
 
436
  selected_weather = st.selectbox('Select Weather Condition:', weather)
437
 
438
  # Create and display line graph
 
 
 
439
 
440
+ trend_col, desc_col = st.columns([7, 3])
441
+
442
+ with trend_col:
443
+ st.markdown("### Crash Trend Over Time")
444
+ trend_fig = create_crash_trend_chart(df, selected_weather)
445
+ # Update the figure layout for larger size
446
+ trend_fig.update_layout(
447
+ height=800, # Increased height
448
+ width=None, # Let width be responsive
449
+ margin=dict(l=50, r=50, t=50, b=50)
450
+ )
451
+ st.plotly_chart(trend_fig, use_container_width=True)
452
+
453
+ with desc_col:
454
+ st.markdown("""
455
  ## **Crash Trend Over Time**
456
  This interactive line chart visualizes the trend of unique traffic crashes over the years, optionally filtered by weather conditions. It highlights how crash frequency changes over time, helping identify trends and potential contributing factors.
 
457
  **Key Features:**
458
  * **Time Trend Analysis**: Displays the total number of unique crashes for each year, showing long-term patterns.
459
  * **Weather Filter**: Users can filter the data by weather conditions (e.g., "Rainy", "Sunny") to analyze how weather impacts crash trends.
460
  * **Interactive Tooltips**: Hovering over data points reveals the exact crash count for each year, providing detailed insights.
 
461
  **Color Scheme and Design:**
462
  * **Line and Markers**: A smooth line connects data points, with prominent markers for each year to highlight trends clearly.
463
  * **Dynamic Title**: The chart updates its title to reflect the selected weather condition or "All Conditions" for the overall trend.
 
464
  **Insights:**
465
  This chart helps uncover:
466
  * Annual fluctuations in crash incidents.
 
479
  unit_type_pairs = sorted(list(unit_type_pairs))
480
  unit_type = st.selectbox("Select Unit Type Pair", options=['Total'] + unit_type_pairs)
481
 
482
+ # # Create 5th Visualization: Injuries and fatalities chart
483
+ # injuries_fatalities_chart = create_injuries_fatalities_chart(df, unit_type)
484
+ # st.altair_chart(injuries_fatalities_chart, use_container_width=True)
485
+
486
+ # st.markdown("""
487
+ # This line chart shows the **total number of injuries and fatalities by month for the selected unit type pair**. The blue line represents total injuries, while the red line represents total fatalities. Observing the trends over the months can help identify any seasonal patterns or peaks in traffic incidents involving specific unit types.
488
+
489
+ # - **Total Injuries**: The blue line indicates how injuries vary over different months, highlighting any particular spikes or declines.
490
+ # - **Total Fatalities**: The red line shows the trend for fatalities, which is generally much lower compared to injuries.
491
+ # - **Unit Types**: The dropdown selection allows users to filter the data by specific unit type pairs (e.g., Driver vs Pedestrian) or view the overall trend across all types.
492
+
493
+ # This visualization aims to provide an intuitive understanding of how injuries and fatalities are distributed across the year, helping stakeholders develop targeted safety measures.
494
+ # """)
495
+
496
+ chart_col, desc_col = st.columns([7, 3])
497
+
498
+ with chart_col:
499
+ # Create 5th Visualization: Injuries and fatalities chart
500
+ injuries_fatalities_chart = create_injuries_fatalities_chart(df, unit_type)
501
+ injuries_fatalities_chart = injuries_fatalities_chart.properties(
502
+ height=800 # Make the chart taller to match the description column
503
+ )
504
+ st.altair_chart(injuries_fatalities_chart, use_container_width=True)
505
+
506
+ with desc_col:
507
+ st.markdown("""
508
+ ## Injuries and Fatalities Trends
509
+
510
+ This line chart shows the **total number of injuries and fatalities by month for the selected unit type pair**. The visualization helps identify seasonal patterns and critical trends in traffic incidents involving specific unit types.
511
+
512
+ **Key Features:**
513
+ * **Injuries Trend** (Blue Line)
514
+ - Tracks monthly injury counts
515
+ - Shows seasonal variations
516
+ - Identifies peak incident periods
517
+
518
+ * **Fatalities Trend** (Red Line)
519
+ - Monitors monthly fatality counts
520
+ - Generally lower than injuries
521
+ - Highlights critical safety concerns
522
+
523
+ * **Interactive Selection**
524
+ - Filter by specific unit type pairs
525
+ - Compare different vehicle combinations
526
+ - View overall trends across all types
527
+
528
+ **Applications:**
529
+ - Identify high-risk months
530
+ - Guide seasonal safety measures
531
+ - Inform emergency response planning
532
+ - Support targeted intervention strategies
533
+
534
+ This visualization aids stakeholders in developing effective safety measures and resource allocation strategies throughout the year.
535
+ """)
536
 
 
 
 
537
 
 
 
538
 
539
  with tab5:
540
  # Dropdown for category selection
 
552
  years = ['All Years'] + sorted(df['Year'].dropna().unique().astype(int).tolist())
553
  selected_year = st.selectbox("Select Year:", years)
554
 
555
+ chart_col, desc_col = st.columns([7, 3])
556
+
557
+ with chart_col:
558
+ st.markdown(f"### Distribution of Incidents by {selected_category}")
559
+ distribution_chart = create_category_distribution_chart(df, selected_category, selected_year)
560
+ # Update the figure layout for larger size
561
+ distribution_chart.update_layout(
562
+ height=800, # Increased height
563
+ width=None, # Let width be responsive
564
+ margin=dict(l=50, r=50, t=50, b=50)
565
+ )
566
+ st.plotly_chart(distribution_chart, use_container_width=True)
 
567
 
568
+ with desc_col:
569
+ st.markdown("""
570
+ ## Distribution by Category
571
+ This visualization explores the distribution of traffic incidents across various categories, such as Collision Manner, Weather, Surface Condition, Alcohol Use, and Driver Gender. Each bar represents a specific category value (e.g., "Male" or "Female" for Gender), and the bars are divided into segments based on Injury Severity (e.g., Minor, Moderate, Serious, Fatal).
572
+
573
+ **Key features include:**
574
+ * Interactive Filters: Select a category and filter by year to analyze trends over time.
575
+ * Insightful Tooltips: Hover over each segment to view the exact count and percentage of incidents for a given severity level.
576
+ * Comparative Analysis: Quickly identify how different conditions or behaviors correlate with injury severity.
577
+
578
+ This chart provides actionable insights into factors contributing to traffic incidents and their outcomes, helping stakeholders target interventions and improve road safety.
579
+ """)
580
 
581
 
582
  if __name__ == "__main__":