Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,217 +7,151 @@ import time
|
|
7 |
file_path = 'Dhaka Metro Rail Fare 2.XLSX' # Ensure the correct file path
|
8 |
df = pd.read_excel(file_path)
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
2. Select your **destination(s)** by clicking the destination buttons.
|
57 |
-
3. The fare from your location to the selected destination(s) will be displayed below.
|
58 |
-
4. You can also see the stations marked on a map with an animated train journey!
|
59 |
-
|
60 |
-
**Note:** The map highlights your location in green and destinations in blue.
|
61 |
-
|
62 |
-
If you face any issues or need further assistance, feel free to [Contact Support on WhatsApp](https://wa.me/+8801719296601).
|
63 |
-
""")
|
64 |
-
|
65 |
-
# Define the default "Select Journey from" message
|
66 |
-
default_origin = "Select Journey from"
|
67 |
-
|
68 |
-
# Dropdown for selecting origin (with "Select Journey from" as a default placeholder)
|
69 |
-
origin = st.selectbox(
|
70 |
-
"Select your Location:",
|
71 |
-
[default_origin] + df['Origin'].unique().tolist(), # Add the "Select Journey from" option at the top
|
72 |
-
index=0 # Ensure the first option is selected by default
|
73 |
-
)
|
74 |
-
|
75 |
-
# Initialize session state for destination selection if not already set
|
76 |
-
if 'destination_select' not in st.session_state:
|
77 |
-
st.session_state.destination_select = []
|
78 |
-
|
79 |
-
# Display buttons for each destination in 3 columns
|
80 |
-
if origin != default_origin:
|
81 |
-
st.write(f"Select your destination(s) from {origin}:")
|
82 |
-
|
83 |
-
# Create 3 columns
|
84 |
-
cols = st.columns(3)
|
85 |
-
|
86 |
-
# Loop through all possible destinations and create a button for each in the columns
|
87 |
-
dest_buttons = df['Destination'].unique()
|
88 |
-
for i, dest in enumerate(dest_buttons):
|
89 |
-
col_idx = i % 3 # Determine column index based on button position
|
90 |
-
with cols[col_idx]:
|
91 |
-
if st.button(f"Select {dest}", key=f"btn_{dest}"):
|
92 |
-
if dest not in st.session_state.destination_select:
|
93 |
-
st.session_state.destination_select.append(dest)
|
94 |
-
else:
|
95 |
-
st.session_state.destination_select.remove(dest)
|
96 |
-
|
97 |
-
# Clear all selected destinations button
|
98 |
-
if st.button("Clear All Destinations"):
|
99 |
-
st.session_state.destination_select = []
|
100 |
-
|
101 |
-
# Display selected destinations and calculate fares
|
102 |
-
destinations = st.session_state.destination_select
|
103 |
-
|
104 |
-
if origin == default_origin:
|
105 |
-
st.write("Please select a valid origin station to proceed.")
|
106 |
-
elif origin and destinations:
|
107 |
# Filter the dataframe based on user selection
|
108 |
fare_data = df[(df['Origin'] == origin) & (df['Destination'].isin(destinations))]
|
109 |
-
|
110 |
-
# Display the fare data
|
111 |
if not fare_data.empty:
|
112 |
for index, row in fare_data.iterrows():
|
113 |
origin_to_dest_fare = row['Fare (৳)']
|
114 |
destination = row['Destination']
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
else:
|
132 |
-
st.write("No fare data available for the selected origin and destinations.")
|
133 |
-
else:
|
134 |
-
st.write("Please select both an origin and at least one destination.")
|
135 |
-
|
136 |
-
st.write("Below is the Animated Map for Dhaka Metro Rail 💶:")
|
137 |
-
|
138 |
-
# Plotting the map using Plotly with animation
|
139 |
-
fig = go.Figure()
|
140 |
-
|
141 |
-
# Add markers for each unique station
|
142 |
-
unique_stations = pd.concat([df[['Origin', 'Origin_Lat', 'Origin_Lon']].rename(columns={'Origin': 'Station', 'Origin_Lat': 'Lat', 'Origin_Lon': 'Lon'}),
|
143 |
-
df[['Destination', 'Destination_Lat', 'Destination_Lon']].rename(columns={'Destination': 'Station', 'Destination_Lat': 'Lat', 'Destination_Lon': 'Lon'})]).drop_duplicates()
|
144 |
-
|
145 |
-
# Store the animation frames for the train's movement
|
146 |
-
frames = []
|
147 |
-
|
148 |
-
# For each selected destination, animate the train from the origin to the destination
|
149 |
-
if origin != default_origin and destinations:
|
150 |
-
for dest in destinations:
|
151 |
-
origin_coords = (coordinates[origin][0], coordinates[origin][1])
|
152 |
-
destination_coords = (coordinates[dest][0], coordinates[dest][1])
|
153 |
-
|
154 |
-
# Create frames for animation (move from origin to destination)
|
155 |
-
num_frames = 30
|
156 |
-
lat_diff = (destination_coords[0] - origin_coords[0]) / num_frames
|
157 |
-
lon_diff = (destination_coords[1] - origin_coords[1]) / num_frames
|
158 |
-
|
159 |
-
frames = [
|
160 |
-
go.Frame(
|
161 |
-
data=[go.Scattermapbox(
|
162 |
-
mode="markers+text",
|
163 |
-
lon=[origin_coords[1] + lon_diff * i],
|
164 |
-
lat=[origin_coords[0] + lat_diff * i],
|
165 |
-
marker={'size': 15, 'color': 'orange'},
|
166 |
-
text="🚆 Train",
|
167 |
-
textposition="top center"
|
168 |
-
)],
|
169 |
-
name=f"frame_{i}"
|
170 |
-
) for i in range(num_frames)
|
171 |
-
]
|
172 |
-
|
173 |
-
# Add final frame for destination
|
174 |
-
frames.append(go.Frame(
|
175 |
-
data=[go.Scattermapbox(
|
176 |
mode="markers+text",
|
177 |
-
lon=[
|
178 |
-
lat=[
|
179 |
-
marker={'size':
|
180 |
-
text=
|
181 |
-
textposition="top center"
|
182 |
-
|
183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
))
|
185 |
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
212 |
)]
|
213 |
-
)
|
214 |
-
margin={"r":0,"t":0,"l":0,"b":0},
|
215 |
-
showlegend=False,
|
216 |
-
title="Dhaka Metro Rail Location Map with Train Animation"
|
217 |
-
)
|
218 |
-
|
219 |
-
# Add frames for animation
|
220 |
-
fig.frames = frames
|
221 |
|
222 |
-
|
223 |
-
|
|
|
7 |
file_path = 'Dhaka Metro Rail Fare 2.XLSX' # Ensure the correct file path
|
8 |
df = pd.read_excel(file_path)
|
9 |
|
10 |
+
# Coordinates for stations (same as in your original code)
|
11 |
+
coordinates = {
|
12 |
+
"Uttara North": (23.869066, 90.367445),
|
13 |
+
"Uttara Center": (23.860118, 90.365106),
|
14 |
+
"Uttara South": (23.845934, 90.363175),
|
15 |
+
"Pallabi": (23.82619516961383, 90.36481554252525),
|
16 |
+
"Mirpur 11": (23.819438208310213, 90.36528532902963),
|
17 |
+
"Mirpur 10": (23.808582994847285, 90.36821595330717),
|
18 |
+
"Kazipara": (23.800017952100532, 90.37178261495391),
|
19 |
+
"Shewrapara": (23.79070140857881, 90.37564622631841),
|
20 |
+
"Agargaon": (23.778385546736345, 90.3800557456356),
|
21 |
+
"Bijoy Sarani": (23.766638127271825, 90.38307537134754),
|
22 |
+
"Farmgate": (23.75923604938459, 90.38694218434738),
|
23 |
+
"Kawran Bazar": (23.751392319539104, 90.39275707447003),
|
24 |
+
"Shahbagh": (23.740324209546923, 90.39600784811131),
|
25 |
+
"Dhaka University": (23.732091083122114, 90.39659408796354),
|
26 |
+
"Bangladesh Secretariat": (23.73004754106779, 90.40764881366906),
|
27 |
+
"Motijheel": (23.72816566933198, 90.41923497972823),
|
28 |
+
"Kamalapur": (23.732367758919807, 90.42547378971085)
|
29 |
+
}
|
30 |
+
|
31 |
+
# Add latitude and longitude for origin and destination
|
32 |
+
df['Origin_Lat'] = df['Origin'].map(lambda x: coordinates.get(x, (None, None))[0])
|
33 |
+
df['Origin_Lon'] = df['Origin'].map(lambda x: coordinates.get(x, (None, None))[1])
|
34 |
+
df['Destination_Lat'] = df['Destination'].map(lambda x: coordinates.get(x, (None, None))[0])
|
35 |
+
df['Destination_Lon'] = df['Destination'].map(lambda x: coordinates.get(x, (None, None))[1])
|
36 |
+
|
37 |
+
# Filter rows with missing coordinates
|
38 |
+
df.dropna(subset=['Origin_Lat', 'Origin_Lon', 'Destination_Lat', 'Destination_Lon'], inplace=True)
|
39 |
+
|
40 |
+
# Streamlit UI setup
|
41 |
+
st.title("Dhaka Metro Rail Fare Checker 🚇")
|
42 |
+
st.write("Below is the fare chart for Dhaka Metro Rail 💶:")
|
43 |
+
|
44 |
+
# Dropdown for selecting origin (with "Select Journey from" as a default placeholder)
|
45 |
+
origin = st.selectbox(
|
46 |
+
"Select your Location:",
|
47 |
+
['Select Journey from'] + df['Origin'].unique().tolist()
|
48 |
+
)
|
49 |
+
|
50 |
+
# Display buttons for destinations
|
51 |
+
if origin != 'Select Journey from':
|
52 |
+
st.write(f"Select your destination(s) from {origin}:")
|
53 |
+
destinations = st.multiselect("Choose Destinations", df[df['Origin'] == origin]['Destination'].unique().tolist())
|
54 |
+
|
55 |
+
if destinations:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
# Filter the dataframe based on user selection
|
57 |
fare_data = df[(df['Origin'] == origin) & (df['Destination'].isin(destinations))]
|
|
|
|
|
58 |
if not fare_data.empty:
|
59 |
for index, row in fare_data.iterrows():
|
60 |
origin_to_dest_fare = row['Fare (৳)']
|
61 |
destination = row['Destination']
|
62 |
+
st.write(f"Fare from {origin} to {destination}: {origin_to_dest_fare}৳")
|
63 |
+
|
64 |
+
# Map for Dhaka Metro
|
65 |
+
fig = go.Figure()
|
66 |
+
|
67 |
+
# Define train icon
|
68 |
+
train_icon = "train" # You can use Plotly's built-in train icon or any other icon if available
|
69 |
+
|
70 |
+
# Add markers for each unique station
|
71 |
+
unique_stations = pd.concat([df[['Origin', 'Origin_Lat', 'Origin_Lon']].rename(columns={'Origin': 'Station', 'Origin_Lat': 'Lat', 'Origin_Lon': 'Lon'}),
|
72 |
+
df[['Destination', 'Destination_Lat', 'Destination_Lon']].rename(columns={'Destination': 'Station', 'Destination_Lat': 'Lat', 'Destination_Lon': 'Lon'})]).drop_duplicates()
|
73 |
+
|
74 |
+
# Add markers for stations
|
75 |
+
for i, row in unique_stations.iterrows():
|
76 |
+
if row['Station'] == origin:
|
77 |
+
fig.add_trace(go.Scattermapbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
mode="markers+text",
|
79 |
+
lon=[row['Lon']],
|
80 |
+
lat=[row['Lat']],
|
81 |
+
marker={'size': 12, 'color': 'green'},
|
82 |
+
text=row['Station'],
|
83 |
+
textposition="top center",
|
84 |
+
name=row['Station']
|
85 |
+
))
|
86 |
+
elif row['Station'] in destinations:
|
87 |
+
fig.add_trace(go.Scattermapbox(
|
88 |
+
mode="markers+text",
|
89 |
+
lon=[row['Lon']],
|
90 |
+
lat=[row['Lat']],
|
91 |
+
marker={'size': 12, 'color': 'blue'},
|
92 |
+
text=row['Station'],
|
93 |
+
textposition="top center",
|
94 |
+
name=row['Station']
|
95 |
+
))
|
96 |
+
|
97 |
+
# Add animated train movement
|
98 |
+
train_path = [] # Coordinates for the train's path
|
99 |
+
for destination in destinations:
|
100 |
+
destination_row = df[(df['Origin'] == origin) & (df['Destination'] == destination)].iloc[0]
|
101 |
+
path = [coordinates[origin], (destination_row['Destination_Lat'], destination_row['Destination_Lon'])]
|
102 |
+
train_path.extend(path)
|
103 |
+
|
104 |
+
# Animation to move the train along the path
|
105 |
+
for i in range(len(train_path)):
|
106 |
+
fig.add_trace(go.Scattermapbox(
|
107 |
+
mode="markers",
|
108 |
+
lon=[train_path[i][1]],
|
109 |
+
lat=[train_path[i][0]],
|
110 |
+
marker={'size': 20, 'symbol': train_icon, 'color': 'red'},
|
111 |
+
name="Train",
|
112 |
+
showlegend=False
|
113 |
))
|
114 |
|
115 |
+
# Map layout
|
116 |
+
fig.update_layout(
|
117 |
+
mapbox=dict(
|
118 |
+
style="open-street-map",
|
119 |
+
center=go.layout.mapbox.Center(lat=23.780, lon=90.400),
|
120 |
+
zoom=11
|
121 |
+
),
|
122 |
+
margin={"r":0,"t":0,"l":0,"b":0},
|
123 |
+
showlegend=False
|
124 |
+
)
|
125 |
+
|
126 |
+
# Set up animation frame and delay to move the train
|
127 |
+
frames = []
|
128 |
+
for i in range(1, len(train_path)):
|
129 |
+
frame = go.Frame(
|
130 |
+
data=[go.Scattermapbox(
|
131 |
+
mode="markers",
|
132 |
+
lon=[train_path[i][1]],
|
133 |
+
lat=[train_path[i][0]],
|
134 |
+
marker={'size': 20, 'symbol': train_icon, 'color': 'red'},
|
135 |
+
name="Train"
|
136 |
+
)],
|
137 |
+
name=f"Frame_{i}"
|
138 |
+
)
|
139 |
+
frames.append(frame)
|
140 |
+
|
141 |
+
fig.frames = frames
|
142 |
+
|
143 |
+
# Animate train movement
|
144 |
+
fig.update_layout(
|
145 |
+
updatemenus=[dict(
|
146 |
+
type="buttons",
|
147 |
+
showactive=False,
|
148 |
+
buttons=[dict(
|
149 |
+
label="Play",
|
150 |
+
method="animate",
|
151 |
+
args=[None, dict(frame=dict(duration=2000, redraw=True), fromcurrent=True)]
|
152 |
+
)]
|
153 |
)]
|
154 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
|
156 |
+
# Show plot in Streamlit
|
157 |
+
st.plotly_chart(fig)
|