ghaffarmumtat123 commited on
Commit
2d1db0a
·
verified ·
1 Parent(s): 8336184

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -15
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import streamlit as st
2
  import csv
 
3
 
4
  # Initialize an empty list for storing the daily routine
5
  daily_routine = []
@@ -33,18 +34,21 @@ def display_todo_list():
33
  st.write(f"**Description**: {task['description']}")
34
  st.markdown("---")
35
 
36
- # Function to save the daily routine to a CSV file
37
- def save_to_csv(filename):
38
- try:
39
- with open(filename, mode="w", newline="") as file:
40
- writer = csv.writer(file)
41
- writer.writerow(["Time", "Task", "Description"]) # Write headers
42
- for task in daily_routine:
43
- writer.writerow([task['time'], task['task'], task['description']])
44
-
45
- st.success(f"To-do list saved as '{filename}'")
46
- except Exception as e:
47
- st.error(f"Error: {e}")
 
 
 
48
 
49
  # Streamlit UI
50
  def main():
@@ -59,9 +63,15 @@ def main():
59
  if st.button("Display To-Do List"):
60
  display_todo_list()
61
 
62
- # Save the list to a CSV file
63
- if st.button("Save to CSV"):
64
- save_to_csv('/app/daily_routine.csv') # Path within Hugging Face Spaces
 
 
 
 
 
 
65
 
66
  if __name__ == "__main__":
67
  main()
 
1
  import streamlit as st
2
  import csv
3
+ import io
4
 
5
  # Initialize an empty list for storing the daily routine
6
  daily_routine = []
 
34
  st.write(f"**Description**: {task['description']}")
35
  st.markdown("---")
36
 
37
+ # Function to save the daily routine to a CSV file in memory (using StringIO)
38
+ def save_to_csv():
39
+ # Create a StringIO object to hold the CSV data in memory
40
+ csv_buffer = io.StringIO()
41
+
42
+ # Write to the buffer
43
+ writer = csv.writer(csv_buffer)
44
+ writer.writerow(["Time", "Task", "Description"]) # Write headers
45
+ for task in daily_routine:
46
+ writer.writerow([task['time'], task['task'], task['description']])
47
+
48
+ # Move the buffer's cursor to the beginning of the file
49
+ csv_buffer.seek(0)
50
+
51
+ return csv_buffer.getvalue()
52
 
53
  # Streamlit UI
54
  def main():
 
63
  if st.button("Display To-Do List"):
64
  display_todo_list()
65
 
66
+ # Save the list to CSV and provide a download button
67
+ if len(daily_routine) > 0:
68
+ csv_data = save_to_csv()
69
+ st.download_button(
70
+ label="Download Daily Routine as CSV",
71
+ data=csv_data,
72
+ file_name="daily_routine.csv",
73
+ mime="text/csv"
74
+ )
75
 
76
  if __name__ == "__main__":
77
  main()