Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -129,7 +129,7 @@ def create_map(df, selected_year):
|
|
| 129 |
|
| 130 |
return m
|
| 131 |
|
| 132 |
-
def create_injuries_fatalities_chart(crash_data):
|
| 133 |
|
| 134 |
# 5th visualization title
|
| 135 |
st.header("5. Total Injuries and Fatalities by Month")
|
|
@@ -149,14 +149,14 @@ def create_injuries_fatalities_chart(crash_data):
|
|
| 149 |
# Dropdown for Unit Type selection
|
| 150 |
# st.sidebar.selectbox("Select Unit Type", options=['Total'] + crash_data['Unittype_One'].dropna().unique().tolist()) # previous location of dropdown in sidebar
|
| 151 |
# unit_type = st.selectbox("Select Unit Type", options=['Total'] + crash_data['Unittype_One'].dropna().unique().tolist())
|
| 152 |
-
unit_type_pairs = set()
|
| 153 |
-
for _, row in crash_data[['Unittype_One', 'Unittype_Two']].dropna().iterrows():
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
# unit_type_pairs = list(unit_type_pairs) # modified as below to sort the dropdown options in alphabetical order
|
| 158 |
-
unit_type_pairs = sorted(list(unit_type_pairs))
|
| 159 |
-
unit_type = st.selectbox("Select Unit Type Pair", options=['Total'] + unit_type_pairs)
|
| 160 |
|
| 161 |
# Filter data based on the selected unit type
|
| 162 |
if unit_type == 'Total':
|
|
@@ -243,30 +243,6 @@ def create_injuries_fatalities_chart(crash_data):
|
|
| 243 |
|
| 244 |
return line_chart
|
| 245 |
|
| 246 |
-
def create_crash_trend_chart(df, weather=None):
|
| 247 |
-
if weather and weather != 'All Conditions':
|
| 248 |
-
df = df[df['Weather'] == weather]
|
| 249 |
-
|
| 250 |
-
# Group data by year and count unique Incident IDs
|
| 251 |
-
trend_data = df.groupby('Year')['Incidentid'].nunique().reset_index()
|
| 252 |
-
trend_data.columns = ['Year', 'Crash Count']
|
| 253 |
-
|
| 254 |
-
# Create line graph
|
| 255 |
-
fig = px.line(
|
| 256 |
-
trend_data,
|
| 257 |
-
x='Year',
|
| 258 |
-
y='Crash Count',
|
| 259 |
-
title=f'Crash Trend Over Time ({weather})',
|
| 260 |
-
labels={'Year': 'Year', 'Crash Count': 'Number of Unique Crashes'},
|
| 261 |
-
markers=True,
|
| 262 |
-
height=600
|
| 263 |
-
)
|
| 264 |
-
|
| 265 |
-
fig.update_traces(line=dict(width=2), marker=dict(size=8))
|
| 266 |
-
fig.update_layout(legend_title_text='Trend')
|
| 267 |
-
|
| 268 |
-
return fig
|
| 269 |
-
|
| 270 |
def main():
|
| 271 |
st.title('Traffic Crash Analysis')
|
| 272 |
|
|
@@ -277,7 +253,7 @@ def main():
|
|
| 277 |
df['Weather'] = 'Unknown'
|
| 278 |
|
| 279 |
# Create tabs for different visualizations
|
| 280 |
-
tab1, tab2, tab3 = st.tabs(["Crash Statistics", "Crash Map", "Crash Trend"])
|
| 281 |
|
| 282 |
with tab1:
|
| 283 |
# Age group selection
|
|
@@ -342,5 +318,21 @@ def main():
|
|
| 342 |
trend_fig = create_crash_trend_chart(df, selected_weather)
|
| 343 |
st.plotly_chart(trend_fig, use_container_width=True)
|
| 344 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 345 |
if __name__ == "__main__":
|
| 346 |
main()
|
|
|
|
| 129 |
|
| 130 |
return m
|
| 131 |
|
| 132 |
+
def create_injuries_fatalities_chart(crash_data, unit_type):
|
| 133 |
|
| 134 |
# 5th visualization title
|
| 135 |
st.header("5. Total Injuries and Fatalities by Month")
|
|
|
|
| 149 |
# Dropdown for Unit Type selection
|
| 150 |
# st.sidebar.selectbox("Select Unit Type", options=['Total'] + crash_data['Unittype_One'].dropna().unique().tolist()) # previous location of dropdown in sidebar
|
| 151 |
# unit_type = st.selectbox("Select Unit Type", options=['Total'] + crash_data['Unittype_One'].dropna().unique().tolist())
|
| 152 |
+
# unit_type_pairs = set()
|
| 153 |
+
# for _, row in crash_data[['Unittype_One', 'Unittype_Two']].dropna().iterrows():
|
| 154 |
+
# if row['Unittype_One'] != 'Driverless' or row['Unittype_Two'] != 'Driverless':
|
| 155 |
+
# pair = ' vs '.join(sorted([row['Unittype_One'], row['Unittype_Two']]))
|
| 156 |
+
# unit_type_pairs.add(pair)
|
| 157 |
+
# # unit_type_pairs = list(unit_type_pairs) # modified as below to sort the dropdown options in alphabetical order
|
| 158 |
+
# unit_type_pairs = sorted(list(unit_type_pairs))
|
| 159 |
+
# unit_type = st.selectbox("Select Unit Type Pair", options=['Total'] + unit_type_pairs)
|
| 160 |
|
| 161 |
# Filter data based on the selected unit type
|
| 162 |
if unit_type == 'Total':
|
|
|
|
| 243 |
|
| 244 |
return line_chart
|
| 245 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
def main():
|
| 247 |
st.title('Traffic Crash Analysis')
|
| 248 |
|
|
|
|
| 253 |
df['Weather'] = 'Unknown'
|
| 254 |
|
| 255 |
# Create tabs for different visualizations
|
| 256 |
+
tab1, tab2, tab3, tab4 = st.tabs(["Crash Statistics", "Crash Map", "Crash Trend", "Crash Injuries/Fatalities"])
|
| 257 |
|
| 258 |
with tab1:
|
| 259 |
# Age group selection
|
|
|
|
| 318 |
trend_fig = create_crash_trend_chart(df, selected_weather)
|
| 319 |
st.plotly_chart(trend_fig, use_container_width=True)
|
| 320 |
|
| 321 |
+
with tab3:
|
| 322 |
+
# Dropdown for Unit Type selection
|
| 323 |
+
unit_type_pairs = set()
|
| 324 |
+
for _, row in df[['Unittype_One', 'Unittype_Two']].dropna().iterrows():
|
| 325 |
+
if row['Unittype_One'] != 'Driverless' or row['Unittype_Two'] != 'Driverless':
|
| 326 |
+
pair = ' vs '.join(sorted([row['Unittype_One'], row['Unittype_Two']]))
|
| 327 |
+
unit_type_pairs.add(pair)
|
| 328 |
+
unit_type_pairs = sorted(list(unit_type_pairs))
|
| 329 |
+
unit_type = st.selectbox("Select Unit Type Pair", options=['Total'] + unit_type_pairs)
|
| 330 |
+
|
| 331 |
+
# Create 5th Visualization: Injuries and fatalities chart
|
| 332 |
+
injuries_fatalities_chart = create_injuries_fatalities_chart(df, unit_type)
|
| 333 |
+
st.altair_chart(injuries_fatalities_chart, use_container_width=True)
|
| 334 |
+
st.markdown("#### TODO: add write-up for this 5th chart.")
|
| 335 |
+
|
| 336 |
+
|
| 337 |
if __name__ == "__main__":
|
| 338 |
main()
|