shukdevdatta123 commited on
Commit
f38dba6
·
verified ·
1 Parent(s): 1891862

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -43
app.py CHANGED
@@ -1,6 +1,7 @@
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
@@ -54,7 +55,7 @@ else:
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
 
@@ -107,7 +108,6 @@ else:
107
  fare_data = df[(df['Origin'] == origin) & (df['Destination'].isin(destinations))]
108
 
109
  # Display the fare data
110
- # Display the fare data in a creative and aesthetic format
111
  if not fare_data.empty:
112
  for index, row in fare_data.iterrows():
113
  origin_to_dest_fare = row['Fare (৳)']
@@ -133,65 +133,91 @@ else:
133
  else:
134
  st.write("Please select both an origin and at least one destination.")
135
 
136
- st.write("Below is the Map for Dhaka Metro Rail 💶:")
137
 
138
- # Plotting the map using Plotly
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
- # Add a map marker for each station
146
- for i, row in unique_stations.iterrows():
147
- if row['Station'] == origin:
148
- # Mark the origin as green
149
- fig.add_trace(go.Scattermapbox(
150
- mode="markers+text",
151
- lon=[row['Lon']],
152
- lat=[row['Lat']],
153
- marker={'size': 12, 'color': 'green'},
154
- text=row['Station'],
155
- textposition="top center",
156
- name=row['Station'],
157
- customdata=[row['Station']] # customdata is now a list
158
- ))
159
- elif row['Station'] in destinations:
160
- # Mark the destination as blue
161
- fig.add_trace(go.Scattermapbox(
162
- mode="markers+text",
163
- lon=[row['Lon']],
164
- lat=[row['Lat']],
165
- marker={'size': 12, 'color': 'blue'},
166
- text=row['Station'],
167
- textposition="top center",
168
- name=row['Station'],
169
- customdata=[row['Station']] # customdata is now a list
170
- ))
171
- else:
172
- # Mark all other stations as red
173
- fig.add_trace(go.Scattermapbox(
174
- mode="markers+text",
175
- lon=[row['Lon']],
176
- lat=[row['Lat']],
177
- marker={'size': 12, 'color': 'red'},
178
- text=row['Station'],
179
- textposition="top center",
180
- name=row['Station'],
181
- customdata=[row['Station']] # customdata is now a list
 
 
182
  ))
183
 
184
- # Map layout
 
 
 
 
 
 
 
 
 
 
 
 
185
  fig.update_layout(
186
  mapbox=dict(
187
  style="open-street-map",
188
  center=go.layout.mapbox.Center(lat=23.780, lon=90.400), # Center on Dhaka
189
  zoom=11
190
  ),
 
 
 
 
 
 
 
 
 
191
  margin={"r":0,"t":0,"l":0,"b":0},
192
  showlegend=False,
193
- title="Dhaka Metro Rail Location Map"
194
  )
195
 
 
 
 
196
  # Show plot in Streamlit
197
  st.plotly_chart(fig)
 
1
  import streamlit as st
2
  import pandas as pd
3
  import plotly.graph_objects as go
4
+ import time
5
 
6
  # Load the Excel file
7
  file_path = 'Dhaka Metro Rail Fare 2.XLSX' # Ensure the correct file path
 
55
  1. Select your **Location station** from the dropdown menu.
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
 
 
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 (৳)']
 
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=[destination_coords[1]],
178
+ lat=[destination_coords[0]],
179
+ marker={'size': 15, 'color': 'orange'},
180
+ text="🚆 Train",
181
+ textposition="top center"
182
+ )],
183
+ name=f"frame_{num_frames}"
184
  ))
185
 
186
+ # Add base station markers
187
+ for i, row in unique_stations.iterrows():
188
+ fig.add_trace(go.Scattermapbox(
189
+ mode="markers+text",
190
+ lon=[row['Lon']],
191
+ lat=[row['Lat']],
192
+ marker={'size': 10, 'color': 'blue'},
193
+ text=row['Station'],
194
+ textposition="top center",
195
+ name=row['Station']
196
+ ))
197
+
198
+ # Map layout with animation
199
  fig.update_layout(
200
  mapbox=dict(
201
  style="open-street-map",
202
  center=go.layout.mapbox.Center(lat=23.780, lon=90.400), # Center on Dhaka
203
  zoom=11
204
  ),
205
+ updatemenus=[dict(
206
+ type="buttons",
207
+ showactive=False,
208
+ buttons=[dict(
209
+ label="Play",
210
+ method="animate",
211
+ args=[None, dict(frame=dict(duration=100, redraw=True), fromcurrent=True)]
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
  # Show plot in Streamlit
223
  st.plotly_chart(fig)