Thamed-Chowdhury commited on
Commit
d10a806
·
verified ·
1 Parent(s): 2284aa5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +141 -141
app.py CHANGED
@@ -1,141 +1,141 @@
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
- # Convert user input dates to datetime
32
- start_date = pd.to_datetime(start_string, format='%d-%m-%Y')
33
- end_date = pd.to_datetime(end_string, format='%d-%m-%Y')
34
-
35
- # Filter rows based on the specified date range
36
- filtered_entries = df3[(df3['Publish Date'] >= start_date) & (df3['Publish Date'] <= end_date)]
37
- filtered_entries.reset_index(inplace=True, drop=True)
38
-
39
- # Display the filtered data
40
- st.dataframe(filtered_entries)
41
-
42
- # Create a bar chart for accident count over days
43
- if not filtered_entries.empty:
44
- # Create a bar chart for accident count over days
45
- if not filtered_entries.empty:
46
- # Fixing date formats
47
- # for i in range(len(filtered_entries)):
48
- # if '/' in filtered_entries['Publish Date'][i]:
49
- # day=int(filtered_entries['Publish Date'][i].split('/')[0])
50
- # mon=int(filtered_entries['Publish Date'][i].split('/')[1])
51
- # yr=int(filtered_entries['Publish Date'][i].split('/')[2])
52
- # filtered_entries['Publish Date'][i]=f"{day}-{mon}-{yr}"
53
- import pandas as pd
54
- import plotly.express as px
55
-
56
- # Convert 'Accident Date' to datetime format
57
- filtered_entries['Accident Date'] = pd.to_datetime(filtered_entries['Accident Date'], format='%d-%m-%Y')
58
-
59
- # Count accidents per date and sort by date
60
- accident_counts = filtered_entries['Accident Date'].value_counts().sort_index()
61
-
62
- # Reset the index and rename columns
63
- accident_counts = accident_counts.reset_index()
64
- accident_counts.columns = ['Accident Date', 'Accident Count']
65
-
66
- # Convert 'Accident Date' back to string format
67
- accident_counts['Accident Date'] = accident_counts['Accident Date'].dt.strftime('%d-%m-%Y')
68
- filtered_entries['Accident Date'] = accident_counts['Accident Date']
69
- fig1 = px.bar(accident_counts,
70
- x='Accident Date',
71
- y='Accident Count',
72
- title="Accident Count Over Days",
73
- labels={'Accident Date': 'Date', 'Accident Count': 'Number of Accidents'},
74
- color='Accident Count',
75
- color_continuous_scale='Viridis')
76
- st.plotly_chart(fig1)
77
- # Convert 'Accident Date' to datetime format
78
- filtered_entries['Accident Date'] = pd.to_datetime(filtered_entries['Accident Date'], format='%d-%m-%Y')
79
-
80
- # Group by 'Accident Date' and sum the 'Killed' column
81
- killed_per_day = filtered_entries.groupby('Accident Date')['Killed'].sum().reset_index()
82
- killed_per_day.columns = ['Accident Date', 'Total Killed']
83
-
84
- # Sort the dates in ascending order
85
- killed_per_day = killed_per_day.sort_values(by='Accident Date')
86
-
87
- # Convert 'Accident Date' back to string format
88
- killed_per_day['Accident Date'] = killed_per_day['Accident Date'].dt.strftime('%d-%m-%Y')
89
-
90
- fig2 = px.bar(killed_per_day,
91
- x='Accident Date',
92
- y='Total Killed',
93
- title="Number of People Killed Each Day",
94
- labels={'Accident Date': 'Date', 'Total Killed': 'Number of People Killed'},
95
- color='Total Killed',
96
- color_continuous_scale='Reds')
97
- st.plotly_chart(fig2)
98
-
99
- # Bar chart showing the number of accidents in each district
100
- district_accidents = filtered_entries['District'].value_counts().reset_index()
101
- district_accidents.columns = ['District', 'Number of Accidents']
102
- fig3 = px.bar(district_accidents,
103
- x='District',
104
- y='Number of Accidents',
105
- title="Accidents in Each District",
106
- labels={'Number of Accidents': 'Number of Accidents', 'District': 'District'},
107
- color='Number of Accidents',
108
- color_continuous_scale='Cividis')
109
- st.plotly_chart(fig3)
110
-
111
- ### Pie Chart Code ###
112
- yes_count=0
113
- no_count=0
114
- not_available_count=0
115
- for i in range(len(filtered_entries)):
116
- if ('Yes' in filtered_entries['Pedestrian_Involved'][i] or 'yes' in filtered_entries['Pedestrian_Involved'][i]): yes_count+=1
117
- if ('No' in filtered_entries['Pedestrian_Involved'][i] or 'no' in filtered_entries['Pedestrian_Involved'][i]): no_count+=1
118
- if ('Not Available' in filtered_entries['Pedestrian_Involved'][i]): not_available_count+=1
119
- Pedestrian_Involved_list = ['Yes', 'No', 'Not Available']
120
- Count_list = [yes_count, no_count, not_available_count]
121
- # dictionary of lists
122
- dict = {'Pedestrian Involved': Pedestrian_Involved_list, 'Count':Count_list}
123
- pedestrian_involvement = pd.DataFrame(dict)
124
- # Pie chart showing the percentage of accidents involving pedestrians vs. those that don't
125
- # pedestrian_involvement = filtered_entries['Pedestrian_Involved'].value_counts().reset_index()
126
- # pedestrian_involvement.columns = ['Pedestrian Involved', 'Count']
127
-
128
- fig4 = px.pie(pedestrian_involvement,
129
- names='Pedestrian Involved',
130
- values='Count',
131
- title="Accidents Involving Pedestrians",
132
- labels={'Pedestrian Involved': 'Pedestrian Involved'},
133
- color_discrete_sequence=['Green', 'Red', 'Blue'])
134
- st.plotly_chart(fig4)
135
-
136
- else:
137
- st.write("No data available for the selected date range.")
138
-
139
- # Display selected start and end dates
140
- st.write("Start date is:", start)
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
+ # Convert user input dates to datetime
32
+ start_date = pd.to_datetime(start_string, format='%d-%m-%Y')
33
+ end_date = pd.to_datetime(end_string, format='%d-%m-%Y')
34
+
35
+ # Filter rows based on the specified date range
36
+ filtered_entries = df3[(df3['Publish Date'] >= start_date) & (df3['Publish Date'] <= end_date)]
37
+ filtered_entries.reset_index(inplace=True, drop=True)
38
+
39
+ # Display the filtered data
40
+ st.dataframe(filtered_entries)
41
+
42
+ # Create a bar chart for accident count over days
43
+ if not filtered_entries.empty:
44
+ # Create a bar chart for accident count over days
45
+ if not filtered_entries.empty:
46
+ # Fixing date formats
47
+ for i in range(len(filtered_entries)):
48
+ if '/' in filtered_entries['Accident Date'][i]:
49
+ day=int(filtered_entries['Accident Date'][i].split('/')[0])
50
+ mon=int(filtered_entries['Accident Date'][i].split('/')[1])
51
+ yr=int(filtered_entries['Acident Date'][i].split('/')[2])
52
+ filtered_entries['Accident Date'][i]=f"{day}-{mon}-{yr}"
53
+ import pandas as pd
54
+ import plotly.express as px
55
+
56
+ # Convert 'Accident Date' to datetime format
57
+ filtered_entries['Accident Date'] = pd.to_datetime(filtered_entries['Accident Date'], format='%d-%m-%Y')
58
+
59
+ # Count accidents per date and sort by date
60
+ accident_counts = filtered_entries['Accident Date'].value_counts().sort_index()
61
+
62
+ # Reset the index and rename columns
63
+ accident_counts = accident_counts.reset_index()
64
+ accident_counts.columns = ['Accident Date', 'Accident Count']
65
+
66
+ # Convert 'Accident Date' back to string format
67
+ accident_counts['Accident Date'] = accident_counts['Accident Date'].dt.strftime('%d-%m-%Y')
68
+ filtered_entries['Accident Date'] = accident_counts['Accident Date']
69
+ fig1 = px.bar(accident_counts,
70
+ x='Accident Date',
71
+ y='Accident Count',
72
+ title="Accident Count Over Days",
73
+ labels={'Accident Date': 'Date', 'Accident Count': 'Number of Accidents'},
74
+ color='Accident Count',
75
+ color_continuous_scale='Viridis')
76
+ st.plotly_chart(fig1)
77
+ # Convert 'Accident Date' to datetime format
78
+ filtered_entries['Accident Date'] = pd.to_datetime(filtered_entries['Accident Date'], format='%d-%m-%Y')
79
+
80
+ # Group by 'Accident Date' and sum the 'Killed' column
81
+ killed_per_day = filtered_entries.groupby('Accident Date')['Killed'].sum().reset_index()
82
+ killed_per_day.columns = ['Accident Date', 'Total Killed']
83
+
84
+ # Sort the dates in ascending order
85
+ killed_per_day = killed_per_day.sort_values(by='Accident Date')
86
+
87
+ # Convert 'Accident Date' back to string format
88
+ killed_per_day['Accident Date'] = killed_per_day['Accident Date'].dt.strftime('%d-%m-%Y')
89
+
90
+ fig2 = px.bar(killed_per_day,
91
+ x='Accident Date',
92
+ y='Total Killed',
93
+ title="Number of People Killed Each Day",
94
+ labels={'Accident Date': 'Date', 'Total Killed': 'Number of People Killed'},
95
+ color='Total Killed',
96
+ color_continuous_scale='Reds')
97
+ st.plotly_chart(fig2)
98
+
99
+ # Bar chart showing the number of accidents in each district
100
+ district_accidents = filtered_entries['District'].value_counts().reset_index()
101
+ district_accidents.columns = ['District', 'Number of Accidents']
102
+ fig3 = px.bar(district_accidents,
103
+ x='District',
104
+ y='Number of Accidents',
105
+ title="Accidents in Each District",
106
+ labels={'Number of Accidents': 'Number of Accidents', 'District': 'District'},
107
+ color='Number of Accidents',
108
+ color_continuous_scale='Cividis')
109
+ st.plotly_chart(fig3)
110
+
111
+ ### Pie Chart Code ###
112
+ yes_count=0
113
+ no_count=0
114
+ not_available_count=0
115
+ for i in range(len(filtered_entries)):
116
+ if ('Yes' in filtered_entries['Pedestrian_Involved'][i] or 'yes' in filtered_entries['Pedestrian_Involved'][i]): yes_count+=1
117
+ if ('No' in filtered_entries['Pedestrian_Involved'][i] or 'no' in filtered_entries['Pedestrian_Involved'][i]): no_count+=1
118
+ if ('Not Available' in filtered_entries['Pedestrian_Involved'][i]): not_available_count+=1
119
+ Pedestrian_Involved_list = ['Yes', 'No', 'Not Available']
120
+ Count_list = [yes_count, no_count, not_available_count]
121
+ # dictionary of lists
122
+ dict = {'Pedestrian Involved': Pedestrian_Involved_list, 'Count':Count_list}
123
+ pedestrian_involvement = pd.DataFrame(dict)
124
+ # Pie chart showing the percentage of accidents involving pedestrians vs. those that don't
125
+ # pedestrian_involvement = filtered_entries['Pedestrian_Involved'].value_counts().reset_index()
126
+ # pedestrian_involvement.columns = ['Pedestrian Involved', 'Count']
127
+
128
+ fig4 = px.pie(pedestrian_involvement,
129
+ names='Pedestrian Involved',
130
+ values='Count',
131
+ title="Accidents Involving Pedestrians",
132
+ labels={'Pedestrian Involved': 'Pedestrian Involved'},
133
+ color_discrete_sequence=['Green', 'Red', 'Blue'])
134
+ st.plotly_chart(fig4)
135
+
136
+ else:
137
+ st.write("No data available for the selected date range.")
138
+
139
+ # Display selected start and end dates
140
+ st.write("Start date is:", start)
141
+ st.write("End date is:", end)