Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
@@ -1,141 +1,140 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import pandas as pd
|
3 |
-
import plotly.express as px
|
4 |
-
from datetime import datetime
|
5 |
-
|
6 |
-
excel_file_name = 'updated_dataset.csv'
|
7 |
-
# Streamlit title
|
8 |
-
st.title("Bangladesh Accident Monitoring System (BAMS)")
|
9 |
-
|
10 |
-
|
11 |
-
# Display a note to the user
|
12 |
-
st.write("Please Note, First Date must be smaller than Last date. Example: First Date = 25-08-2024 and Last Date = 28-08-2024")
|
13 |
-
|
14 |
-
# Get today's date
|
15 |
-
today = datetime.strptime(datetime.today().strftime('%d-%m-%Y'), '%d-%m-%Y')
|
16 |
-
|
17 |
-
# Input fields for date range
|
18 |
-
start = st.date_input("Enter first date", max_value=today, format="DD-MM-YYYY")
|
19 |
-
start_string = start.strftime('%d-%m-%Y')
|
20 |
-
end = st.date_input("Enter last date", max_value=today, format="DD-MM-YYYY")
|
21 |
-
end_string = end.strftime('%d-%m-%Y')
|
22 |
-
|
23 |
-
# Button to generate dataset based on date range
|
24 |
-
if st.button("Generate Dataset"):
|
25 |
-
# Read the selected excel file
|
26 |
-
df3 = pd.read_csv(excel_file_name)
|
27 |
-
|
28 |
-
# Convert 'Publish Date' column to datetime with 'day-month-year' format
|
29 |
-
df3['Publish Date'] = pd.to_datetime(df3['Publish Date'], format='%d-%m-%Y')
|
30 |
-
|
31 |
-
#
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
#
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
import
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
accident_counts =
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
killed_per_day =
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
district_accidents =
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
if ('
|
117 |
-
if ('
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
#
|
125 |
-
# pedestrian_involvement =
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
st.write("
|
141 |
-
st.write("End date is:", end)
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import plotly.express as px
|
4 |
+
from datetime import datetime
|
5 |
+
|
6 |
+
excel_file_name = 'updated_dataset.csv'
|
7 |
+
# Streamlit title
|
8 |
+
st.title("Bangladesh Accident Monitoring System (BAMS)")
|
9 |
+
|
10 |
+
|
11 |
+
# Display a note to the user
|
12 |
+
st.write("Please Note, First Date must be smaller than Last date. Example: First Date = 25-08-2024 and Last Date = 28-08-2024")
|
13 |
+
|
14 |
+
# Get today's date
|
15 |
+
today = datetime.strptime(datetime.today().strftime('%d-%m-%Y'), '%d-%m-%Y')
|
16 |
+
|
17 |
+
# Input fields for date range
|
18 |
+
start = st.date_input("Enter first date", max_value=today, format="DD-MM-YYYY")
|
19 |
+
start_string = start.strftime('%d-%m-%Y')
|
20 |
+
end = st.date_input("Enter last date", max_value=today, format="DD-MM-YYYY")
|
21 |
+
end_string = end.strftime('%d-%m-%Y')
|
22 |
+
|
23 |
+
# Button to generate dataset based on date range
|
24 |
+
if st.button("Generate Dataset"):
|
25 |
+
# Read the selected excel file
|
26 |
+
df3 = pd.read_csv(excel_file_name)
|
27 |
+
|
28 |
+
# Convert 'Publish Date' column to datetime with 'day-month-year' format
|
29 |
+
df3['Publish Date'] = pd.to_datetime(df3['Publish Date'], format='%d-%m-%Y')
|
30 |
+
|
31 |
+
# Fixing date formats
|
32 |
+
for i in range(len(df3)):
|
33 |
+
if '/' in df3['Accident Date'][i]:
|
34 |
+
day=int(df3['Accident Date'][i].split('/')[0])
|
35 |
+
mon=int(df3['Accident Date'][i].split('/')[1])
|
36 |
+
yr=int(df3['Accident Date'][i].split('/')[2])
|
37 |
+
df3['Publish Date'][i]=f"{day}-{mon}-{yr}"
|
38 |
+
print(df3.tail())
|
39 |
+
# Convert user input dates to datetime
|
40 |
+
start_date = pd.to_datetime(start_string, format='%d-%m-%Y')
|
41 |
+
end_date = pd.to_datetime(end_string, format='%d-%m-%Y')
|
42 |
+
|
43 |
+
# Filter rows based on the specified date range
|
44 |
+
filtered_entries = df3[(df3['Publish Date'] >= start_date) & (df3['Publish Date'] <= end_date)]
|
45 |
+
filtered_entries.reset_index(inplace=True, drop=True)
|
46 |
+
|
47 |
+
# Display the filtered data
|
48 |
+
st.dataframe(filtered_entries)
|
49 |
+
# Create a bar chart for accident count over days
|
50 |
+
if not filtered_entries.empty:
|
51 |
+
# Create a bar chart for accident count over days
|
52 |
+
if not filtered_entries.empty:
|
53 |
+
import plotly.express as px
|
54 |
+
|
55 |
+
# Convert 'Accident Date' to datetime format
|
56 |
+
filtered_entries['Accident Date'] = pd.to_datetime(filtered_entries['Accident Date'], format='%d-%m-%Y')
|
57 |
+
|
58 |
+
# Count accidents per date and sort by date
|
59 |
+
accident_counts = filtered_entries['Accident Date'].value_counts().sort_index()
|
60 |
+
|
61 |
+
# Reset the index and rename columns
|
62 |
+
accident_counts = accident_counts.reset_index()
|
63 |
+
accident_counts.columns = ['Accident Date', 'Accident Count']
|
64 |
+
|
65 |
+
# Convert 'Accident Date' back to string format
|
66 |
+
accident_counts['Accident Date'] = accident_counts['Accident Date'].dt.strftime('%d-%m-%Y')
|
67 |
+
filtered_entries['Accident Date'] = accident_counts['Accident Date']
|
68 |
+
fig1 = px.bar(accident_counts,
|
69 |
+
x='Accident Date',
|
70 |
+
y='Accident Count',
|
71 |
+
title="Accident Count Over Days",
|
72 |
+
labels={'Accident Date': 'Date', 'Accident Count': 'Number of Accidents'},
|
73 |
+
color='Accident Count',
|
74 |
+
color_continuous_scale='Viridis')
|
75 |
+
st.plotly_chart(fig1)
|
76 |
+
# Convert 'Accident Date' to datetime format
|
77 |
+
filtered_entries['Accident Date'] = pd.to_datetime(filtered_entries['Accident Date'], format='%d-%m-%Y')
|
78 |
+
|
79 |
+
# Group by 'Accident Date' and sum the 'Killed' column
|
80 |
+
killed_per_day = filtered_entries.groupby('Accident Date')['Killed'].sum().reset_index()
|
81 |
+
killed_per_day.columns = ['Accident Date', 'Total Killed']
|
82 |
+
|
83 |
+
# Sort the dates in ascending order
|
84 |
+
killed_per_day = killed_per_day.sort_values(by='Accident Date')
|
85 |
+
|
86 |
+
# Convert 'Accident Date' back to string format
|
87 |
+
killed_per_day['Accident Date'] = killed_per_day['Accident Date'].dt.strftime('%d-%m-%Y')
|
88 |
+
|
89 |
+
fig2 = px.bar(killed_per_day,
|
90 |
+
x='Accident Date',
|
91 |
+
y='Total Killed',
|
92 |
+
title="Number of People Killed Each Day",
|
93 |
+
labels={'Accident Date': 'Date', 'Total Killed': 'Number of People Killed'},
|
94 |
+
color='Total Killed',
|
95 |
+
color_continuous_scale='Reds')
|
96 |
+
st.plotly_chart(fig2)
|
97 |
+
|
98 |
+
# Bar chart showing the number of accidents in each district
|
99 |
+
district_accidents = filtered_entries['District'].value_counts().reset_index()
|
100 |
+
district_accidents.columns = ['District', 'Number of Accidents']
|
101 |
+
fig3 = px.bar(district_accidents,
|
102 |
+
x='District',
|
103 |
+
y='Number of Accidents',
|
104 |
+
title="Accidents in Each District",
|
105 |
+
labels={'Number of Accidents': 'Number of Accidents', 'District': 'District'},
|
106 |
+
color='Number of Accidents',
|
107 |
+
color_continuous_scale='Cividis')
|
108 |
+
st.plotly_chart(fig3)
|
109 |
+
|
110 |
+
### Pie Chart Code ###
|
111 |
+
yes_count=0
|
112 |
+
no_count=0
|
113 |
+
not_available_count=0
|
114 |
+
for i in range(len(filtered_entries)):
|
115 |
+
if ('Yes' in filtered_entries['Pedestrian_Involved'][i] or 'yes' in filtered_entries['Pedestrian_Involved'][i]): yes_count+=1
|
116 |
+
if ('No' in filtered_entries['Pedestrian_Involved'][i] or 'no' in filtered_entries['Pedestrian_Involved'][i]): no_count+=1
|
117 |
+
if ('Not Available' in filtered_entries['Pedestrian_Involved'][i]): not_available_count+=1
|
118 |
+
Pedestrian_Involved_list = ['Yes', 'No', 'Not Available']
|
119 |
+
Count_list = [yes_count, no_count, not_available_count]
|
120 |
+
# dictionary of lists
|
121 |
+
dict = {'Pedestrian Involved': Pedestrian_Involved_list, 'Count':Count_list}
|
122 |
+
pedestrian_involvement = pd.DataFrame(dict)
|
123 |
+
# Pie chart showing the percentage of accidents involving pedestrians vs. those that don't
|
124 |
+
# pedestrian_involvement = filtered_entries['Pedestrian_Involved'].value_counts().reset_index()
|
125 |
+
# pedestrian_involvement.columns = ['Pedestrian Involved', 'Count']
|
126 |
+
|
127 |
+
fig4 = px.pie(pedestrian_involvement,
|
128 |
+
names='Pedestrian Involved',
|
129 |
+
values='Count',
|
130 |
+
title="Accidents Involving Pedestrians",
|
131 |
+
labels={'Pedestrian Involved': 'Pedestrian Involved'},
|
132 |
+
color_discrete_sequence=['Green', 'Red', 'Blue'])
|
133 |
+
st.plotly_chart(fig4)
|
134 |
+
|
135 |
+
else:
|
136 |
+
st.write("No data available for the selected date range.")
|
137 |
+
|
138 |
+
# Display selected start and end dates
|
139 |
+
st.write("Start date is:", start)
|
140 |
+
st.write("End date is:", end)
|
|