Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -236,6 +236,76 @@ elif menu == "History":
|
|
236 |
else:
|
237 |
st.write("No history records found.")
|
238 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
239 |
# Graph view to display a line chart of item selections over time
|
240 |
elif menu == "Graph":
|
241 |
st.title("Breakfast Poll History - Graph View")
|
@@ -247,20 +317,27 @@ elif menu == "Graph":
|
|
247 |
# Prepare data for plotting
|
248 |
if st.session_state.history:
|
249 |
history_data = []
|
|
|
|
|
250 |
for record in st.session_state.history:
|
251 |
# Extract only the date part (YYYY-MM-DD) for display
|
252 |
-
date = record['Date']
|
253 |
for index, row in record['Summary'].iterrows():
|
|
|
254 |
for drink in row['Drinks'].split(', '):
|
255 |
-
history_data.append({'Date': date, 'Item': drink, 'Type': 'Drink'})
|
256 |
for food in row['Food'].split(', '):
|
257 |
-
history_data.append({'Date': date, 'Item': food, 'Type': 'Food'})
|
|
|
|
|
|
|
|
|
258 |
|
259 |
# Create a DataFrame from history data
|
260 |
history_df = pd.DataFrame(history_data)
|
261 |
-
|
262 |
# Count occurrences of each item per date
|
263 |
-
item_counts = history_df.groupby(['Date', 'Item', 'Type']).size().reset_index(name='Count')
|
264 |
|
265 |
# Separate items into Drinks and Food, and sort them alphabetically
|
266 |
drinks = sorted(item_counts[item_counts['Type'] == 'Drink']['Item'].unique())
|
@@ -269,7 +346,7 @@ elif menu == "Graph":
|
|
269 |
# Create a dictionary to store the checkbox values for each item
|
270 |
item_visibility = {}
|
271 |
|
272 |
-
# Create interactive checkboxes for Drinks and
|
273 |
st.sidebar.header("Select Items to Display")
|
274 |
|
275 |
# Drinks Section
|
@@ -286,14 +363,24 @@ elif menu == "Graph":
|
|
286 |
# Add a unique key to each checkbox to avoid duplicate widget IDs
|
287 |
item_visibility[item] = st.sidebar.checkbox(item, value=True, key=f"checkbox_{item}_Food")
|
288 |
|
289 |
-
#
|
|
|
|
|
|
|
|
|
|
|
290 |
selected_items = [item for item, visible in item_visibility.items() if visible]
|
291 |
-
|
|
|
292 |
|
293 |
# Create a line plot for each selected item over time
|
294 |
plt.figure(figsize=(12, 6))
|
295 |
sns.lineplot(data=filtered_item_counts, x='Date', y='Count', hue='Item', marker='o')
|
296 |
|
|
|
|
|
|
|
|
|
297 |
# Customize the plot
|
298 |
plt.xticks(rotation=45)
|
299 |
plt.title('Item Selections Over Time')
|
|
|
236 |
else:
|
237 |
st.write("No history records found.")
|
238 |
|
239 |
+
# # Graph view to display a line chart of item selections over time
|
240 |
+
# elif menu == "Graph":
|
241 |
+
# st.title("Breakfast Poll History - Graph View")
|
242 |
+
|
243 |
+
# # Load the history if not already loaded
|
244 |
+
# if not st.session_state.history:
|
245 |
+
# st.session_state.history = load_history()
|
246 |
+
|
247 |
+
# # Prepare data for plotting
|
248 |
+
# if st.session_state.history:
|
249 |
+
# history_data = []
|
250 |
+
# for record in st.session_state.history:
|
251 |
+
# # Extract only the date part (YYYY-MM-DD) for display
|
252 |
+
# date = record['Date']
|
253 |
+
# for index, row in record['Summary'].iterrows():
|
254 |
+
# for drink in row['Drinks'].split(', '):
|
255 |
+
# history_data.append({'Date': date, 'Item': drink, 'Type': 'Drink'})
|
256 |
+
# for food in row['Food'].split(', '):
|
257 |
+
# history_data.append({'Date': date, 'Item': food, 'Type': 'Food'})
|
258 |
+
|
259 |
+
# # Create a DataFrame from history data
|
260 |
+
# history_df = pd.DataFrame(history_data)
|
261 |
+
|
262 |
+
# # Count occurrences of each item per date
|
263 |
+
# item_counts = history_df.groupby(['Date', 'Item', 'Type']).size().reset_index(name='Count')
|
264 |
+
|
265 |
+
# # Separate items into Drinks and Food, and sort them alphabetically
|
266 |
+
# drinks = sorted(item_counts[item_counts['Type'] == 'Drink']['Item'].unique())
|
267 |
+
# foods = sorted(item_counts[item_counts['Type'] == 'Food']['Item'].unique())
|
268 |
+
|
269 |
+
# # Create a dictionary to store the checkbox values for each item
|
270 |
+
# item_visibility = {}
|
271 |
+
|
272 |
+
# # Create interactive checkboxes for Drinks and Food in the sidebar
|
273 |
+
# st.sidebar.header("Select Items to Display")
|
274 |
+
|
275 |
+
# # Drinks Section
|
276 |
+
# if drinks:
|
277 |
+
# st.sidebar.subheader("Drinks")
|
278 |
+
# for item in drinks:
|
279 |
+
# # Add a unique key to each checkbox to avoid duplicate widget IDs
|
280 |
+
# item_visibility[item] = st.sidebar.checkbox(item, value=True, key=f"checkbox_{item}_Drink")
|
281 |
+
|
282 |
+
# # Food Section
|
283 |
+
# if foods:
|
284 |
+
# st.sidebar.subheader("Food")
|
285 |
+
# for item in foods:
|
286 |
+
# # Add a unique key to each checkbox to avoid duplicate widget IDs
|
287 |
+
# item_visibility[item] = st.sidebar.checkbox(item, value=True, key=f"checkbox_{item}_Food")
|
288 |
+
|
289 |
+
# # Filter the data based on selected items
|
290 |
+
# selected_items = [item for item, visible in item_visibility.items() if visible]
|
291 |
+
# filtered_item_counts = item_counts[item_counts['Item'].isin(selected_items)]
|
292 |
+
|
293 |
+
# # Create a line plot for each selected item over time
|
294 |
+
# plt.figure(figsize=(12, 6))
|
295 |
+
# sns.lineplot(data=filtered_item_counts, x='Date', y='Count', hue='Item', marker='o')
|
296 |
+
|
297 |
+
# # Customize the plot
|
298 |
+
# plt.xticks(rotation=45)
|
299 |
+
# plt.title('Item Selections Over Time')
|
300 |
+
# plt.xlabel('Date')
|
301 |
+
# plt.ylabel('Number of Selections')
|
302 |
+
# plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.15), ncol=3)
|
303 |
+
|
304 |
+
# # Display the plot
|
305 |
+
# st.pyplot(plt.gcf())
|
306 |
+
# else:
|
307 |
+
# st.write("No historical data available to plot.")
|
308 |
+
|
309 |
# Graph view to display a line chart of item selections over time
|
310 |
elif menu == "Graph":
|
311 |
st.title("Breakfast Poll History - Graph View")
|
|
|
317 |
# Prepare data for plotting
|
318 |
if st.session_state.history:
|
319 |
history_data = []
|
320 |
+
user_data = {} # Store user-specific data
|
321 |
+
|
322 |
for record in st.session_state.history:
|
323 |
# Extract only the date part (YYYY-MM-DD) for display
|
324 |
+
date = record['Date'].split("_")[0] # Use only the YYYY-MM-DD portion of the date
|
325 |
for index, row in record['Summary'].iterrows():
|
326 |
+
user = row['Name']
|
327 |
for drink in row['Drinks'].split(', '):
|
328 |
+
history_data.append({'Date': date, 'Item': drink, 'Type': 'Drink', 'User': user})
|
329 |
for food in row['Food'].split(', '):
|
330 |
+
history_data.append({'Date': date, 'Item': food, 'Type': 'Food', 'User': user})
|
331 |
+
|
332 |
+
# Append user data for selection
|
333 |
+
if user not in user_data:
|
334 |
+
user_data[user] = True # Initialize all users as visible by default
|
335 |
|
336 |
# Create a DataFrame from history data
|
337 |
history_df = pd.DataFrame(history_data)
|
338 |
+
|
339 |
# Count occurrences of each item per date
|
340 |
+
item_counts = history_df.groupby(['Date', 'Item', 'Type', 'User']).size().reset_index(name='Count')
|
341 |
|
342 |
# Separate items into Drinks and Food, and sort them alphabetically
|
343 |
drinks = sorted(item_counts[item_counts['Type'] == 'Drink']['Item'].unique())
|
|
|
346 |
# Create a dictionary to store the checkbox values for each item
|
347 |
item_visibility = {}
|
348 |
|
349 |
+
# Create interactive checkboxes for Drinks, Food, and Users in the sidebar
|
350 |
st.sidebar.header("Select Items to Display")
|
351 |
|
352 |
# Drinks Section
|
|
|
363 |
# Add a unique key to each checkbox to avoid duplicate widget IDs
|
364 |
item_visibility[item] = st.sidebar.checkbox(item, value=True, key=f"checkbox_{item}_Food")
|
365 |
|
366 |
+
# User Section: Create a checkbox for each user to toggle their visibility
|
367 |
+
st.sidebar.subheader("Users")
|
368 |
+
for user in user_data.keys():
|
369 |
+
user_data[user] = st.sidebar.checkbox(user, value=True, key=f"checkbox_user_{user}")
|
370 |
+
|
371 |
+
# Filter the data based on selected items and users
|
372 |
selected_items = [item for item, visible in item_visibility.items() if visible]
|
373 |
+
selected_users = [user for user, visible in user_data.items() if visible]
|
374 |
+
filtered_item_counts = item_counts[item_counts['Item'].isin(selected_items) & item_counts['User'].isin(selected_users)]
|
375 |
|
376 |
# Create a line plot for each selected item over time
|
377 |
plt.figure(figsize=(12, 6))
|
378 |
sns.lineplot(data=filtered_item_counts, x='Date', y='Count', hue='Item', marker='o')
|
379 |
|
380 |
+
# Customize the plot to show integer y-axis labels
|
381 |
+
y_max = filtered_item_counts['Count'].max() + 1
|
382 |
+
plt.yticks(range(0, y_max)) # Show only integer labels on the y-axis
|
383 |
+
|
384 |
# Customize the plot
|
385 |
plt.xticks(rotation=45)
|
386 |
plt.title('Item Selections Over Time')
|