shukdevdatta123 commited on
Commit
3a3c7af
·
verified ·
1 Parent(s): ce0ddb1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +177 -163
app.py CHANGED
@@ -1,163 +1,177 @@
1
- import streamlit as st
2
- import pandas as pd
3
- import plotly.graph_objects as go
4
-
5
- # Load the Excel file
6
- file_path = 'Dhaka Metro Rail Fare 2.XLSX' # Ensure the correct file path
7
- df = pd.read_excel(file_path)
8
-
9
- # Ensure necessary columns are present
10
- required_columns = ['Origin', 'Destination', 'Fare (৳)']
11
- if not all(col in df.columns for col in required_columns):
12
- st.write("Please ensure the file contains 'Origin', 'Destination', and 'Fare' columns.")
13
- else:
14
- # Add coordinates for each station (example coordinates for illustration)
15
- coordinates = {
16
- "Uttara North": (23.869066, 90.367445),
17
- "Uttara Center": (23.860118, 90.365106),
18
- "Uttara South": (23.845934, 90.363175),
19
- "Pallabi": (23.82619516961383, 90.36481554252525),
20
- "Mirpur 11": (23.819438208310213, 90.36528532902963),
21
- "Mirpur 10": (23.808582994847285, 90.36821595330717),
22
- "Kazipara": (23.800017952100532, 90.37178261495391),
23
- "Shewrapara": (23.79070140857881, 90.37564622631841),
24
- "Agargaon": (23.778385546736345, 90.3800557456356),
25
- "Bijoy Sarani": (23.766638127271825, 90.38307537134754),
26
- "Farmgate": (23.75923604938459, 90.38694218434738),
27
- "Kawran Bazar": (23.751392319539104, 90.39275707447003),
28
- "Shahbagh": (23.740324209546923, 90.39600784811131),
29
- "Dhaka University": (23.732091083122114, 90.39659408796354),
30
- "Bangladesh Secretariat": (23.73004754106779, 90.40764881366906),
31
- "Motijheel": (23.72816566933198, 90.41923497972823),
32
- "Kamalapur": (23.732367758919807, 90.42547378971085)
33
- }
34
-
35
- # Add latitude and longitude for origin and destination based on the coordinates dictionary
36
- df['Origin_Lat'] = df['Origin'].map(lambda x: coordinates.get(x, (None, None))[0])
37
- df['Origin_Lon'] = df['Origin'].map(lambda x: coordinates.get(x, (None, None))[1])
38
- df['Destination_Lat'] = df['Destination'].map(lambda x: coordinates.get(x, (None, None))[0])
39
- df['Destination_Lon'] = df['Destination'].map(lambda x: coordinates.get(x, (None, None))[1])
40
-
41
- # Filter rows with missing coordinates
42
- df.dropna(subset=['Origin_Lat', 'Origin_Lon', 'Destination_Lat', 'Destination_Lon'], inplace=True)
43
-
44
- # Streamlit UI setup
45
- st.title("Dhaka Metro Rail Fare Checker 🚇")
46
- st.write("Below is the fare chart for Dhaka Metro Rail 💶:")
47
-
48
- # Instruction sidebar
49
- st.sidebar.title("Instructions")
50
- st.sidebar.write("""
51
- **Welcome to the Dhaka Metro Rail Fare Checker!**
52
-
53
- *How to use:*
54
- 1. Select your **Location station** from the dropdown menu.
55
- 2. Select your **destination(s)** from the list. You can select multiple destinations.
56
- 3. The fare from your location to the selected destination(s) will be displayed below.
57
- 4. You can also see the stations marked on a map.
58
-
59
- **Note:** The map highlights your location in green and destinations in blue.
60
-
61
- If you face any issues or need further assistance, feel free to [Contact Support on WhatsApp](https://wa.me/+8801719296601).
62
- """)
63
-
64
- # Define the default "Select Journey from" message
65
- default_origin = "Select Journey from"
66
-
67
- # Dropdown for selecting origin (with "Select Journey from" as a default placeholder)
68
- origin = st.selectbox(
69
- "Select your Location:",
70
- [default_origin] + df['Origin'].unique().tolist(), # Add the "Select Journey from" option at the top
71
- index=0 # Ensure the first option is selected by default
72
- )
73
-
74
- # Initialize session state for destination selection if not already set
75
- if 'destination_select' not in st.session_state:
76
- st.session_state.destination_select = []
77
-
78
- # Multiselect for destinations (pass the default from session state)
79
- destinations = st.multiselect(
80
- "Select your destination(s):",
81
- df['Destination'].unique(),
82
- default=st.session_state.destination_select
83
- )
84
-
85
- # Update session state based on the selected destinations
86
- st.session_state.destination_select = destinations
87
-
88
- # If the user hasn't selected a valid origin yet, don't proceed
89
- if origin == default_origin:
90
- st.write("Please select a valid origin station to proceed.")
91
- elif origin and destinations:
92
- # Filter the dataframe based on user selection
93
- fare_data = df[(df['Origin'] == origin) & (df['Destination'].isin(destinations))]
94
-
95
- # Display the fare data
96
- if not fare_data.empty:
97
- for index, row in fare_data.iterrows():
98
- st.write(f"Fare from {origin} to {row['Destination']} is: {row['Fare (৳)']}৳")
99
- else:
100
- st.write("No fare data available for the selected origin and destinations.")
101
- else:
102
- st.write("Please select both an origin and at least one destination.")
103
-
104
- # Plotting the map using Plotly
105
- fig = go.Figure()
106
-
107
- # Add markers for each unique station
108
- unique_stations = pd.concat([df[['Origin', 'Origin_Lat', 'Origin_Lon']].rename(columns={'Origin': 'Station', 'Origin_Lat': 'Lat', 'Origin_Lon': 'Lon'}),
109
- df[['Destination', 'Destination_Lat', 'Destination_Lon']].rename(columns={'Destination': 'Station', 'Destination_Lat': 'Lat', 'Destination_Lon': 'Lon'})]).drop_duplicates()
110
-
111
- # Add a map marker for each station
112
- for i, row in unique_stations.iterrows():
113
- if row['Station'] == origin:
114
- # Mark the origin as green
115
- fig.add_trace(go.Scattermapbox(
116
- mode="markers+text",
117
- lon=[row['Lon']],
118
- lat=[row['Lat']],
119
- marker={'size': 12, 'color': 'green'},
120
- text=row['Station'],
121
- textposition="top center",
122
- name=row['Station'],
123
- customdata=[row['Station']] # customdata is now a list
124
- ))
125
- elif row['Station'] in destinations:
126
- # Mark the destination as blue
127
- fig.add_trace(go.Scattermapbox(
128
- mode="markers+text",
129
- lon=[row['Lon']],
130
- lat=[row['Lat']],
131
- marker={'size': 12, 'color': 'blue'},
132
- text=row['Station'],
133
- textposition="top center",
134
- name=row['Station'],
135
- customdata=[row['Station']] # customdata is now a list
136
- ))
137
- else:
138
- # Mark all other stations as red
139
- fig.add_trace(go.Scattermapbox(
140
- mode="markers+text",
141
- lon=[row['Lon']],
142
- lat=[row['Lat']],
143
- marker={'size': 12, 'color': 'red'},
144
- text=row['Station'],
145
- textposition="top center",
146
- name=row['Station'],
147
- customdata=[row['Station']] # customdata is now a list
148
- ))
149
-
150
- # Map layout
151
- fig.update_layout(
152
- mapbox=dict(
153
- style="open-street-map",
154
- center=go.layout.mapbox.Center(lat=23.780, lon=90.400), # Center on Dhaka
155
- zoom=11
156
- ),
157
- margin={"r":0,"t":0,"l":0,"b":0},
158
- showlegend=False,
159
- title="Dhaka Metro Rail Location Map"
160
- )
161
-
162
- # Show plot in Streamlit
163
- st.plotly_chart(fig)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import plotly.graph_objects as go
4
+
5
+ # Load the Excel file
6
+ file_path = 'Dhaka Metro Rail Fare 2.XLSX' # Ensure the correct file path
7
+ df = pd.read_excel(file_path)
8
+
9
+ # Ensure necessary columns are present
10
+ required_columns = ['Origin', 'Destination', 'Fare (৳)']
11
+ if not all(col in df.columns for col in required_columns):
12
+ st.write("Please ensure the file contains 'Origin', 'Destination', and 'Fare' columns.")
13
+ else:
14
+ # Add coordinates for each station (example coordinates for illustration)
15
+ coordinates = {
16
+ "Uttara North": (23.869066, 90.367445),
17
+ "Uttara Center": (23.860118, 90.365106),
18
+ "Uttara South": (23.845934, 90.363175),
19
+ "Pallabi": (23.82619516961383, 90.36481554252525),
20
+ "Mirpur 11": (23.819438208310213, 90.36528532902963),
21
+ "Mirpur 10": (23.808582994847285, 90.36821595330717),
22
+ "Kazipara": (23.800017952100532, 90.37178261495391),
23
+ "Shewrapara": (23.79070140857881, 90.37564622631841),
24
+ "Agargaon": (23.778385546736345, 90.3800557456356),
25
+ "Bijoy Sarani": (23.766638127271825, 90.38307537134754),
26
+ "Farmgate": (23.75923604938459, 90.38694218434738),
27
+ "Kawran Bazar": (23.751392319539104, 90.39275707447003),
28
+ "Shahbagh": (23.740324209546923, 90.39600784811131),
29
+ "Dhaka University": (23.732091083122114, 90.39659408796354),
30
+ "Bangladesh Secretariat": (23.73004754106779, 90.40764881366906),
31
+ "Motijheel": (23.72816566933198, 90.41923497972823),
32
+ "Kamalapur": (23.732367758919807, 90.42547378971085)
33
+ }
34
+
35
+ # Add latitude and longitude for origin and destination based on the coordinates dictionary
36
+ df['Origin_Lat'] = df['Origin'].map(lambda x: coordinates.get(x, (None, None))[0])
37
+ df['Origin_Lon'] = df['Origin'].map(lambda x: coordinates.get(x, (None, None))[1])
38
+ df['Destination_Lat'] = df['Destination'].map(lambda x: coordinates.get(x, (None, None))[0])
39
+ df['Destination_Lon'] = df['Destination'].map(lambda x: coordinates.get(x, (None, None))[1])
40
+
41
+ # Filter rows with missing coordinates
42
+ df.dropna(subset=['Origin_Lat', 'Origin_Lon', 'Destination_Lat', 'Destination_Lon'], inplace=True)
43
+
44
+ # Streamlit UI setup
45
+ st.title("Dhaka Metro Rail Fare Checker 🚇")
46
+ st.write("Below is the fare chart for Dhaka Metro Rail 💶:")
47
+
48
+ # Instruction sidebar
49
+ st.sidebar.title("Instructions")
50
+ st.sidebar.write("""
51
+ **Welcome to the Dhaka Metro Rail Fare Checker!**
52
+
53
+ *How to use:*
54
+ 1. Select your **Location station** from the dropdown menu.
55
+ 2. Select your **destination(s)** by clicking the destination buttons.
56
+ 3. The fare from your location to the selected destination(s) will be displayed below.
57
+ 4. You can also see the stations marked on a map.
58
+
59
+ **Note:** The map highlights your location in green and destinations in blue.
60
+
61
+ If you face any issues or need further assistance, feel free to [Contact Support on WhatsApp](https://wa.me/+8801719296601).
62
+ """)
63
+
64
+ # Define the default "Select Journey from" message
65
+ default_origin = "Select Journey from"
66
+
67
+ # Dropdown for selecting origin (with "Select Journey from" as a default placeholder)
68
+ origin = st.selectbox(
69
+ "Select your Location:",
70
+ [default_origin] + df['Origin'].unique().tolist(), # Add the "Select Journey from" option at the top
71
+ index=0 # Ensure the first option is selected by default
72
+ )
73
+
74
+ # Initialize session state for destination selection if not already set
75
+ if 'destination_select' not in st.session_state:
76
+ st.session_state.destination_select = []
77
+
78
+ # Display buttons for each destination in 3 columns
79
+ if origin != default_origin:
80
+ st.write(f"Select your destination(s) from {origin}:")
81
+
82
+ # Create 3 columns
83
+ cols = st.columns(3)
84
+
85
+ # Loop through all possible destinations and create a button for each in the columns
86
+ dest_buttons = df['Destination'].unique()
87
+ for i, dest in enumerate(dest_buttons):
88
+ col_idx = i % 3 # Determine column index based on button position
89
+ with cols[col_idx]:
90
+ if st.button(f"Select {dest}", key=f"btn_{dest}"):
91
+ if dest not in st.session_state.destination_select:
92
+ st.session_state.destination_select.append(dest)
93
+ else:
94
+ st.session_state.destination_select.remove(dest)
95
+
96
+ # Clear all selected destinations button
97
+ if st.button("Clear All Destinations"):
98
+ st.session_state.destination_select = []
99
+
100
+ # Display selected destinations and calculate fares
101
+ destinations = st.session_state.destination_select
102
+
103
+ if origin == default_origin:
104
+ st.write("Please select a valid origin station to proceed.")
105
+ elif origin and destinations:
106
+ # Filter the dataframe based on user selection
107
+ fare_data = df[(df['Origin'] == origin) & (df['Destination'].isin(destinations))]
108
+
109
+ # Display the fare data
110
+ if not fare_data.empty:
111
+ for index, row in fare_data.iterrows():
112
+ st.write(f"Fare from {origin} to {row['Destination']} is: {row['Fare ()']}৳")
113
+ else:
114
+ st.write("No fare data available for the selected origin and destinations.")
115
+ else:
116
+ st.write("Please select both an origin and at least one destination.")
117
+
118
+ # Plotting the map using Plotly
119
+ fig = go.Figure()
120
+
121
+ # Add markers for each unique station
122
+ unique_stations = pd.concat([df[['Origin', 'Origin_Lat', 'Origin_Lon']].rename(columns={'Origin': 'Station', 'Origin_Lat': 'Lat', 'Origin_Lon': 'Lon'}),
123
+ df[['Destination', 'Destination_Lat', 'Destination_Lon']].rename(columns={'Destination': 'Station', 'Destination_Lat': 'Lat', 'Destination_Lon': 'Lon'})]).drop_duplicates()
124
+
125
+ # Add a map marker for each station
126
+ for i, row in unique_stations.iterrows():
127
+ if row['Station'] == origin:
128
+ # Mark the origin as green
129
+ fig.add_trace(go.Scattermapbox(
130
+ mode="markers+text",
131
+ lon=[row['Lon']],
132
+ lat=[row['Lat']],
133
+ marker={'size': 12, 'color': 'green'},
134
+ text=row['Station'],
135
+ textposition="top center",
136
+ name=row['Station'],
137
+ customdata=[row['Station']] # customdata is now a list
138
+ ))
139
+ elif row['Station'] in destinations:
140
+ # Mark the destination as blue
141
+ fig.add_trace(go.Scattermapbox(
142
+ mode="markers+text",
143
+ lon=[row['Lon']],
144
+ lat=[row['Lat']],
145
+ marker={'size': 12, 'color': 'blue'},
146
+ text=row['Station'],
147
+ textposition="top center",
148
+ name=row['Station'],
149
+ customdata=[row['Station']] # customdata is now a list
150
+ ))
151
+ else:
152
+ # Mark all other stations as red
153
+ fig.add_trace(go.Scattermapbox(
154
+ mode="markers+text",
155
+ lon=[row['Lon']],
156
+ lat=[row['Lat']],
157
+ marker={'size': 12, 'color': 'red'},
158
+ text=row['Station'],
159
+ textposition="top center",
160
+ name=row['Station'],
161
+ customdata=[row['Station']] # customdata is now a list
162
+ ))
163
+
164
+ # Map layout
165
+ fig.update_layout(
166
+ mapbox=dict(
167
+ style="open-street-map",
168
+ center=go.layout.mapbox.Center(lat=23.780, lon=90.400), # Center on Dhaka
169
+ zoom=11
170
+ ),
171
+ margin={"r":0,"t":0,"l":0,"b":0},
172
+ showlegend=False,
173
+ title="Dhaka Metro Rail Location Map"
174
+ )
175
+
176
+ # Show plot in Streamlit
177
+ st.plotly_chart(fig)