Hi everyone,
First of all, I am new to this. I have only done Data Visualization until now, so please be patient with me.
In my App I want to add new events in a calendar from streamlit-calendar and I want the calendar to immediately update. I tried a few things and with different code, everytime, I only managed it to update every two times I submit a new event. The .csv gets updated everytime, but the calendar every two times. Sometimes it would update the calendar everytime, but only a certain amount of times, before again only updating every two times. Maybe someone sees what I am doing wrong. Again, I am new and probably it is super easy
See the snippet of the code below:
# File to store workout data
ATTENDANCE_DATA_FILE = r"C:\...\attendance_data.csv" #the directory used by me
# Load or create datasets
def load_attendance():
'''create or load data file that lets you track on which days you went to the gym'''
if os.path.exists(ATTENDANCE_DATA_FILE):
return pd.read_csv(ATTENDANCE_DATA_FILE)
else:
return pd.DataFrame(columns=["Date", "Type", "Color"])
#Save datasets
def save_attendance(df):
df.to_csv(ATTENDANCE_DATA_FILE, index=False)
#Calendar Set Up
#_____________________________________________
calendar_options = {
"editable": True, # Allows events to be edited
"selectable": True, # Allows selection of events
"show_events": True, # Ensures events are shown
"initialView": "dayGridMonth", # Set to dayGridMonth to show monthly calendar
"multiMonthMaxColumns": 2, # Number of months per row (if you want multi-month views)
"multiMonthMaxRows": 2, # Number of rows for multi-month view
"headerToolbar": {
"left": "prev,next today", # Buttons on the left side
"center": "title", # Title of the calendar in the center
"right": "dayGridMonth,timeGridWeek", # Display day grid month and week view buttons
},
}
# Convert CSV data to list of events for Streamlit Calendar
def get_calendar_events(df):
events = []
for _, row in df.iterrows():
events.append({
"title": row["Type"], # The type of workout
"start": row["Date"], # Date in ISO format
"end": row["Date"], # Single-day event
"backgroundColor": row["Color"] # color of event
})
return events
@st.fragment
def calendar_widget():
df_attendance = load_attendance()
calendar_events = get_calendar_events(df_attendance)
with st.form("workout_type"): #select event data
date = st.date_input("Date")
Type = st.text_input("Workout Type")
color = st.color_picker("Color")
submit = st.form_submit_button("Add Workout")
if submit and Type and color: # Only save if input is valid
new_entry = pd.DataFrame([[date.strftime("%Y-%m-%d"), Type, color]], columns=["Date", "Type", "Color"])
df_attendance = pd.concat([load_attendance(), new_entry], ignore_index=True)
save_attendance(df_attendance) #save newly added data in csv
#st.session_state.calendar_events = get_calendar_events(df_attendance)
df_attendance = load_attendance()
calendar_events = get_calendar_events(df_attendance)
st.success("Workout Added!")
st.rerun()
calendar_widget = calendar(
events=calendar_events,
options=calendar_options,
key='calendar', # Assign a widget key to prevent state loss
)
st.write(calendar_widget)
#if "calendar_events" not in st.session_state:
#df_attendance = load_attendance()
#st.session_state.calendar_events = get_calendar_events(df_attendance)
#_______________________________________
# FITNESS APP UI
#------------------------------------------------------------------
st.title("🏋️ Personal Fitness Tracker")
# Attendance Tracking
st.header("Gym History")
calendar_widget()
thank you for helping.
Adam