elli-teu commited on
Commit
39a6a86
·
1 Parent(s): 2bd6eac

Added inout from user

Browse files
Files changed (1) hide show
  1. app.py +62 -44
app.py CHANGED
@@ -74,21 +74,31 @@ def get_buses():
74
  short_bus_list = list(pd.unique(bus_df["route_short_name"]))
75
  return bus_df, bus_list, short_bus_list
76
 
77
- # Plot graphs
78
- def plot_graphs(data):
79
- st.write("### Data Preview")
80
- st.dataframe(data.head())
81
-
82
- #st.write("### Histogram")
83
- #column = st.selectbox("Select column for histogram", data.columns)
84
- #fig, ax = plt.subplots()
85
- #sns.histplot(data[column], kde=True, ax=ax)
86
- #st.pyplot(fig)
87
-
88
- #st.write("### Correlation Matrix")
89
- #fig, ax = plt.subplots()
90
- #sns.heatmap(data.corr(), annot=True, cmap="coolwarm", ax=ax)
91
- #st.pyplot(fig)
 
 
 
 
 
 
 
 
 
 
92
 
93
  # Streamlit UI
94
  def main():
@@ -152,38 +162,45 @@ def main():
152
  )
153
  st.write("### Selected Bus")
154
  st.write(f"{search}: {bus}")
 
 
 
 
 
 
 
 
 
 
155
 
156
- #Plocka alla akruella trip_ids från buses
157
- trips = buses_df[buses_df["route_long_name"]==bus]
158
 
159
- trip_ids = list(trips["trip_id"])
 
 
160
 
161
- st.write(trip_ids[0])
162
-
163
- #Nu vill vi plotta!
164
- categories = {0 : 'EMPTY',
165
- 1: 'MANY_SEATS_AVAILABLE',
166
- 2:'FEW_SEATS_AVAILABLE',
167
- 3:'STANDING_ROOM_ONLY',
168
- 4:'CRUSHED_STANDING_ROOM_ONLY',
169
- 5: 'FULL'}
170
- #Steg 2, fixa sa date ersätts av stop genom att mappa location till stop
171
  plot_df = st.session_state.data[st.session_state.data["trip_id"]==trip_ids[0]]
172
- plot_df = plot_df[["datetime", "vehicle_occupancystatus"]]
173
- st.write(plot_df.head())
174
- st.write(plot_df.tail())
175
- #plot_df = plot_df.set_index("datetime")
176
- plot_df["Y_category"] = plot_df["vehicle_occupancystatus"].map(categories)
177
- #st.line_chart(plot_df)
178
- # Create the Altair chart
179
- chart = alt.Chart(plot_df).mark_line(point=True).encode(
180
- x=alt.X('datetime:T', title="Datetime"), # Use column name as string
181
- y=alt.Y('Y_category:N', title="Vehicle Occupancy Status (Categories)"), # Treat Y as categorical
182
- tooltip=['datetime', 'Y_category'] # Add tooltips for interactivity
183
- ).properties(
184
- title="Vehicle Occupancy Status Over Time"
185
- )
186
- st.altair_chart(chart, use_container_width=True)
187
 
188
 
189
 
@@ -194,7 +211,8 @@ def main():
194
 
195
  # Display data and graphs
196
  if st.session_state.data is not None:
197
- plot_graphs(st.session_state.data)
 
198
 
199
  main()
200
 
 
74
  short_bus_list = list(pd.unique(bus_df["route_short_name"]))
75
  return bus_df, bus_list, short_bus_list
76
 
77
+ def plot_graph(plot_df):
78
+ #Nu vill vi plotta!
79
+ categories = {0 : 'EMPTY',
80
+ 1: 'MANY_SEATS_AVAILABLE',
81
+ 2:'FEW_SEATS_AVAILABLE',
82
+ 3:'STANDING_ROOM_ONLY',
83
+ 4:'CRUSHED_STANDING_ROOM_ONLY',
84
+ 5: 'FULL'}
85
+ #Steg 2, fixa sa date ersätts av stop genom att mappa location till stop
86
+
87
+ plot_df = plot_df[["datetime", "vehicle_occupancystatus"]]
88
+ st.write(plot_df.head())
89
+ st.write(plot_df.tail())
90
+ #plot_df = plot_df.set_index("datetime")
91
+ plot_df["Y_category"] = plot_df["vehicle_occupancystatus"].map(categories)
92
+ #st.line_chart(plot_df)
93
+ # Create the Altair chart
94
+ chart = alt.Chart(plot_df).mark_line(point=True).encode(
95
+ x=alt.X('datetime:T', title="Datetime"), # Use column name as string
96
+ y=alt.Y('Y_category:N', title="Vehicle Occupancy Status (Categories)"), # Treat Y as categorical
97
+ tooltip=['datetime', 'Y_category'] # Add tooltips for interactivity
98
+ ).properties(
99
+ title="Vehicle Occupancy Status Over Time"
100
+ )
101
+ st.altair_chart(chart, use_container_width=True)
102
 
103
  # Streamlit UI
104
  def main():
 
162
  )
163
  st.write("### Selected Bus")
164
  st.write(f"{search}: {bus}")
165
+
166
+ today = datetime.now()
167
+ tomorrow = today + timedelta(days=1)
168
+ today = today.date()
169
+ tomorrow = tomorrow.date()
170
+
171
+ date_options = {
172
+ today.strftime("%d %B %Y") : today,
173
+ tomorrow.strftime("%d %B %Y") : tomorrow
174
+ }
175
 
176
+ day_choice = st.sidebar.radio("Select the day:", options=list(date_options.keys()))
 
177
 
178
+ # Add time input widgets in the sidebar
179
+ start_time = st.sidebar.time_input("Select a start time", value=None)
180
+ end_time = st.sidebar.time_input("Select an end time", value=None)
181
 
182
+ #Plocka alla aktuella trip_ids från buses
183
+ trips = buses_df[buses_df["route_long_name"]==bus]
184
+ bus_trips = st.session_state.data[st.session_state.data["route_long_name"]==bus]
185
+ bus_trips["datetime"] = pd.to_datetime(bus_trips["datetime"])
186
+ bus_trips["datetime"] = bus_trips["datetime"].dt.tz_convert(None)
187
+
188
+ #TODO remove
189
+ trip_ids = list(trips["trip_id"])
 
 
190
  plot_df = st.session_state.data[st.session_state.data["trip_id"]==trip_ids[0]]
191
+
192
+ print(f"start time {type(start_time)}")
193
+ print(f"end time {type(end_time)}")
194
+ print(f"day {type(day_choice)}")
195
+
196
+ if start_time != None and end_time != None:
197
+ #TODO hur filtrera tid?
198
+ st.write(f"Displaying buses between {start_time.strftime('%H:%M')} and {end_time.strftime('%H:%M')} the {day_choice}")
199
+ selected_trips = bus_trips[(bus_trips["datetime"] >= datetime.combine(date_options[day_choice], start_time)) & (bus_trips["datetime"] <= datetime.combine(date_options[day_choice], end_time))]
200
+ trip_ids = list(pd.unique(selected_trips["trip_id"]))
201
+ st.write(f"Length {len(trip_ids)}")
202
+ for id in trip_ids:
203
+ plot_graph(st.session_state.data[st.session_state.data["trip_id"]==id])
 
 
204
 
205
 
206
 
 
211
 
212
  # Display data and graphs
213
  if st.session_state.data is not None:
214
+ #plot_graphs(st.session_state.data)
215
+ st.write("Hi")
216
 
217
  main()
218