MarcosRodrigo commited on
Commit
71f8411
·
verified ·
1 Parent(s): 193a158

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -27
app.py CHANGED
@@ -73,13 +73,18 @@ def load_history():
73
  history.append({"Date": date, "Summary": summary_df})
74
  return history
75
 
76
- # Save the current summary to a text file and upload to the repository
77
- def save_summary_to_file(summary_df):
78
  timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
79
- filename = f"{HISTORY_DIR}/{timestamp}.txt"
80
- local_filepath = f"{timestamp}.txt"
81
- summary_df.to_csv(local_filepath, index=False)
82
- upload_file(path_or_fileobj=local_filepath, path_in_repo=filename, repo_id=REPO_ID, token=hf_token, repo_type="space")
 
 
 
 
 
83
 
84
  # Load persistent history and temporary selections on app start
85
  if "history" not in st.session_state:
@@ -119,7 +124,7 @@ if menu == "Poll":
119
  st.session_state.current_selections.append({"Name": st.session_state.users[-1], "Drinks": selected_drinks})
120
  st.session_state.step = 3
121
 
122
- # Step 3: Select Food
123
  if st.session_state.step == 3:
124
  st.header("Step 3: Select your food(s)")
125
  food_options = [
@@ -127,34 +132,28 @@ if menu == "Poll":
127
  "Palmera de chocolate blanco", "Yogurt", "Pincho de tortilla", "Nada"
128
  ]
129
  selected_food = st.multiselect("Choose your food:", food_options)
130
- if st.button("Next", key="step3_next") and selected_food:
131
  st.session_state.current_selections[-1]["Food"] = selected_food
132
- st.session_state.step = 4
133
-
134
- # Step 4: Display Summary
135
- if st.session_state.step == 4:
136
- st.header("Step 4: Summary of Selections")
137
- if st.session_state.current_selections:
138
  df = pd.DataFrame(st.session_state.current_selections)
139
- st.table(df)
140
-
141
- if st.button("Submit Summary", key="submit_summary"):
142
- save_summary_to_file(df)
143
- save_current_selection_to_file(pd.DataFrame(st.session_state.current_selections))
144
- upload_temp_file_to_repo()
145
- timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
146
- st.session_state.history.append({"Date": timestamp, "Summary": df})
147
- st.success(f"Summary submitted at {timestamp}")
148
- st.session_state.step = 1
149
- st.experimental_set_query_params(step="reset")
150
-
151
- # "Current" view to display the current summary of all users' selections
152
  elif menu == "Current":
153
  st.title("Current Selections of All Users")
154
  if st.button("Reload Selections"):
155
  download_temp_file_from_repo() # Download the latest version of the shared file
156
  current_df = load_current_selections()
157
  st.table(current_df)
 
 
 
 
 
 
 
158
 
159
  # History view to check past summaries
160
  elif menu == "History":
 
73
  history.append({"Date": date, "Summary": summary_df})
74
  return history
75
 
76
+ # Save the current summary to a text file in the history directory
77
+ def save_summary_to_history():
78
  timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
79
+ history_filename = f"{HISTORY_DIR}/{timestamp}.txt"
80
+ # Read the current selections file
81
+ if os.path.exists(TEMP_FILE):
82
+ summary_df = pd.read_csv(TEMP_FILE)
83
+ # Save the DataFrame to a text file in the history directory
84
+ summary_df.to_csv(history_filename, index=False)
85
+ # Upload the file to the repository
86
+ upload_file(path_or_fileobj=history_filename, path_in_repo=history_filename, repo_id=REPO_ID, token=hf_token, repo_type="space")
87
+ return timestamp
88
 
89
  # Load persistent history and temporary selections on app start
90
  if "history" not in st.session_state:
 
124
  st.session_state.current_selections.append({"Name": st.session_state.users[-1], "Drinks": selected_drinks})
125
  st.session_state.step = 3
126
 
127
+ # Step 3: Select Food and Save Selections
128
  if st.session_state.step == 3:
129
  st.header("Step 3: Select your food(s)")
130
  food_options = [
 
132
  "Palmera de chocolate blanco", "Yogurt", "Pincho de tortilla", "Nada"
133
  ]
134
  selected_food = st.multiselect("Choose your food:", food_options)
135
+ if st.button("Save Selections", key="save_selections") and selected_food:
136
  st.session_state.current_selections[-1]["Food"] = selected_food
 
 
 
 
 
 
137
  df = pd.DataFrame(st.session_state.current_selections)
138
+ save_current_selection_to_file(df)
139
+ upload_temp_file_to_repo()
140
+ st.success(f"Selections saved for {st.session_state.users[-1]}!")
141
+ st.session_state.step = 1
142
+
143
+ # "Current" view to display the current summary of all users' selections and submit to history
 
 
 
 
 
 
 
144
  elif menu == "Current":
145
  st.title("Current Selections of All Users")
146
  if st.button("Reload Selections"):
147
  download_temp_file_from_repo() # Download the latest version of the shared file
148
  current_df = load_current_selections()
149
  st.table(current_df)
150
+ if st.button("Submit Summary to History"):
151
+ timestamp = save_summary_to_history()
152
+ st.success(f"Summary saved to history at {timestamp}")
153
+ # Clear the current selections
154
+ os.remove(TEMP_FILE)
155
+ upload_temp_file_to_repo() # Upload the cleared file
156
+ st.experimental_set_query_params(step="reset")
157
 
158
  # History view to check past summaries
159
  elif menu == "History":