Workouts form rebuild for new UI and logic w Gemini
This commit is contained in:
parent
d19d495ae1
commit
2dd6c01cfe
1 changed files with 86 additions and 132 deletions
|
|
@ -13,148 +13,102 @@ host = os.getenv('host')
|
||||||
database = os.getenv('database')
|
database = os.getenv('database')
|
||||||
port = os.getenv('port')
|
port = os.getenv('port')
|
||||||
|
|
||||||
workout_table = ['chest','back','legs']
|
# --- Database Helper Functions ---
|
||||||
selected_workout = st.selectbox('Select a Workout',options=workout_table)
|
def get_db_connection():
|
||||||
|
|
||||||
try:
|
try:
|
||||||
conn = mysql.connector.connect(user=user,password=password, host=host,port=port,database=database)
|
return mysql.connector.connect(
|
||||||
|
user=user, password=password, host=host, port=port, database=database
|
||||||
|
)
|
||||||
except mysql.connector.Error as err:
|
except mysql.connector.Error as err:
|
||||||
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
|
st.error(f"Database Error: {err}")
|
||||||
print("Something is wrong with your user name or password")
|
return None
|
||||||
elif err.errno == errorcode.ER_BAD_DB_ERROR:
|
|
||||||
print("Database does not exist")
|
def load_template(workout_type):
|
||||||
else:
|
conn = get_db_connection()
|
||||||
print(err)
|
if not conn: return pd.DataFrame()
|
||||||
|
|
||||||
else:
|
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
if selected_workout == 'chest':
|
# Map selection to table name safely
|
||||||
query = ("SELECT * FROM `chest`")
|
table_map = {'chest': 'chest', 'back': 'back', 'legs': 'legs'}
|
||||||
elif selected_workout == 'back':
|
table = table_map.get(workout_type, 'chest')
|
||||||
query = ("SELECT * FROM `back`")
|
|
||||||
else:
|
query = f"SELECT * FROM `{table}`"
|
||||||
query = ("SELECT * FROM `legs`")
|
|
||||||
cursor.execute(query)
|
cursor.execute(query)
|
||||||
data = cursor.fetchall()
|
data = cursor.fetchall()
|
||||||
conn.commit()
|
columns = cursor.column_names
|
||||||
cursor.close()
|
cursor.close()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
# Set date to today for dataframe
|
df = pd.DataFrame(data, columns=columns)
|
||||||
today=date.today()
|
df['date'] = date.today()
|
||||||
df = pd.DataFrame(data,columns=cursor.column_names)
|
return df
|
||||||
df['date']=today
|
|
||||||
|
|
||||||
# function to display current row data
|
def save_workout(df):
|
||||||
def display_row(row):
|
conn = get_db_connection()
|
||||||
st.text_input("Date", value=row['date'], key='date')
|
if not conn: return
|
||||||
st.text_input("Group",value=row['group'], key='group')
|
|
||||||
st.text_input("Exercise",value=row['exercise'], key='exercise')
|
|
||||||
st.number_input("Set",value=row['set'], key='set',format="%i")
|
|
||||||
st.number_input("Weight",value=row['weight'], key='weight',format="%0.1f",step=0.5)
|
|
||||||
st.number_input("Reps",value=row['reps'], key='reps',format="%i")
|
|
||||||
st.text_input("Notes",value=row['notes'], key='notes')
|
|
||||||
|
|
||||||
# function to save current edited row data to edited dataframe
|
|
||||||
def save_edited_row(index):
|
|
||||||
st.session_state.edited_df.at[index,'group'] = st.session_state.group
|
|
||||||
st.session_state.edited_df.at[index,'exercise'] = st.session_state.exercise
|
|
||||||
st.session_state.edited_df.at[index,'set'] = st.session_state.set
|
|
||||||
st.session_state.edited_df.at[index,'weight'] = st.session_state.weight
|
|
||||||
st.session_state.edited_df.at[index,'reps'] = st.session_state.reps
|
|
||||||
st.session_state.edited_df.at[index,'notes'] = st.session_state.notes
|
|
||||||
|
|
||||||
# function to save current superset row to superset dataframe
|
|
||||||
def save_superset_row(index):
|
|
||||||
st.session_state.superset_df.at[index,'group'] = st.session_state.group
|
|
||||||
st.session_state.superset_df.at[index,'exercise'] = st.session_state.exercise
|
|
||||||
st.session_state.superset_df.at[index,'set'] = st.session_state.set
|
|
||||||
st.session_state.superset_df.at[index,'weight'] = st.session_state.weight
|
|
||||||
st.session_state.superset_df.at[index,'reps'] = st.session_state.reps
|
|
||||||
st.session_state.superset_df.at[index,'notes'] = st.session_state.notes
|
|
||||||
|
|
||||||
|
|
||||||
# Build superset selector
|
|
||||||
max_superset = int(df['superset'].max()) if not df.empty else 1
|
|
||||||
superset_count = list(range(1, max_superset + 1))
|
|
||||||
|
|
||||||
# initialize session state dataframe
|
|
||||||
if 'edited_df' not in st.session_state or st.session_state.get('current_workout') != selected_workout:
|
|
||||||
st.session_state.edited_df = df.copy()
|
|
||||||
st.session_state.current_workout = selected_workout
|
|
||||||
st.session_state.row = 0
|
|
||||||
if 'last_superset' in st.session_state:
|
|
||||||
del st.session_state['last_superset']
|
|
||||||
keys_to_clear = ['date', 'group', 'exercise', 'set', 'weight', 'reps', 'notes']
|
|
||||||
for key in keys_to_clear:
|
|
||||||
if key in st.session_state:
|
|
||||||
del st.session_state[key]
|
|
||||||
|
|
||||||
# Initialize session state to keep track of the current row
|
|
||||||
if 'row' not in st.session_state:
|
|
||||||
st.session_state.row = 0
|
|
||||||
|
|
||||||
# set superset selection
|
|
||||||
current_superset = st.selectbox('Select Superset', options=superset_count)
|
|
||||||
|
|
||||||
# Reset row if superset changes
|
|
||||||
if 'last_superset' not in st.session_state or st.session_state.last_superset != current_superset:
|
|
||||||
st.session_state.row = 0
|
|
||||||
st.session_state.last_superset = current_superset
|
|
||||||
keys_to_clear = ['date', 'group', 'exercise', 'set', 'weight', 'reps', 'notes']
|
|
||||||
for key in keys_to_clear:
|
|
||||||
if key in st.session_state:
|
|
||||||
del st.session_state[key]
|
|
||||||
|
|
||||||
# select superset dataframe subset
|
|
||||||
subset_df = st.session_state.edited_df[st.session_state.edited_df['superset'] == current_superset]
|
|
||||||
|
|
||||||
st.write(subset_df)
|
|
||||||
|
|
||||||
# Display the current row of data
|
|
||||||
if st.session_state.row < 12 and st.session_state.row < len(subset_df):
|
|
||||||
# Get the index from the subset to update the main df later
|
|
||||||
current_idx = subset_df.index[st.session_state.row]
|
|
||||||
row_data = subset_df.loc[current_idx]
|
|
||||||
|
|
||||||
display_row(row_data)
|
|
||||||
|
|
||||||
# Button to clear form and move to the next row
|
|
||||||
if st.button('Save and Next'):
|
|
||||||
save_edited_row(current_idx)
|
|
||||||
st.session_state.row += 1
|
|
||||||
|
|
||||||
# Clear widget states so next row loads correct values
|
|
||||||
keys_to_clear = ['date', 'group', 'exercise', 'set', 'weight', 'reps', 'notes']
|
|
||||||
for key in keys_to_clear:
|
|
||||||
if key in st.session_state:
|
|
||||||
del st.session_state[key]
|
|
||||||
|
|
||||||
if st.session_state.row >= 12:
|
|
||||||
st.session_state.row = 0
|
|
||||||
st.rerun()
|
|
||||||
elif st.session_state.row >= 12 or st.session_state.row >= len(subset_df):
|
|
||||||
st.write("Superset complete")
|
|
||||||
if st.button("Start Over"):
|
|
||||||
st.session_state.row = 0
|
|
||||||
st.rerun()
|
|
||||||
|
|
||||||
st.write("Select Next Superset")
|
|
||||||
|
|
||||||
st.write("")
|
|
||||||
|
|
||||||
# Button to save all changes back to the dataframe
|
|
||||||
if st.button('Save All Changes'):
|
|
||||||
df.update(st.session_state.edited_df)
|
|
||||||
st.write("All changes saved!")
|
|
||||||
st.write(df)
|
|
||||||
conn = mysql.connector.connect(user=user,password=password, host=host,port=port,database=database)
|
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
for index, row in df.iterrows():
|
# Note: Target table uses 'inputdate'
|
||||||
query = ("INSERT INTO `workouts` "
|
query = ("INSERT INTO `workouts` "
|
||||||
"(`inputdate`, `group`, `exercise`, `set`, `weight`, `reps`, `notes`) "
|
"(`inputdate`, `group`, `exercise`, `set`, `weight`, `reps`, `notes`) "
|
||||||
"VALUES (%s, %s, %s, %s, %s, %s, %s)")
|
"VALUES (%s, %s, %s, %s, %s, %s, %s)")
|
||||||
cursor.execute(query,(row.iloc[1],row.iloc[2],row.iloc[3],row.iloc[4],row.iloc[5],row.iloc[6],row.iloc[7]))
|
|
||||||
|
for _, row in df.iterrows():
|
||||||
|
cursor.execute(query, (
|
||||||
|
row['date'], row['group'], row['exercise'],
|
||||||
|
row['set'], row['weight'], row['reps'], row['notes']
|
||||||
|
))
|
||||||
|
|
||||||
conn.commit()
|
conn.commit()
|
||||||
cursor.close()
|
cursor.close()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
st.success("Workout saved successfully!")
|
||||||
|
|
||||||
|
# --- Main Application ---
|
||||||
|
st.title("Workout Logger")
|
||||||
|
|
||||||
|
workout_options = ['chest', 'back', 'legs']
|
||||||
|
selected_workout = st.selectbox('Select a Workout', options=workout_options)
|
||||||
|
|
||||||
|
if 'current_workout' not in st.session_state or st.session_state.current_workout != selected_workout:
|
||||||
|
st.session_state.current_workout = selected_workout
|
||||||
|
st.session_state.workout_df = load_template(selected_workout)
|
||||||
|
st.session_state.current_index = 0
|
||||||
|
|
||||||
|
df = st.session_state.workout_df
|
||||||
|
|
||||||
|
if not df.empty:
|
||||||
|
# Navigation Dropdown
|
||||||
|
step_labels = [f"{i+1}. {row['exercise']} (Set {row['set']})" for i, row in df.iterrows()]
|
||||||
|
selected_step = st.selectbox("Jump to Exercise", options=step_labels, index=st.session_state.current_index)
|
||||||
|
st.session_state.current_index = step_labels.index(selected_step)
|
||||||
|
|
||||||
|
# Current Row Data
|
||||||
|
idx = st.session_state.current_index
|
||||||
|
row = df.iloc[idx]
|
||||||
|
|
||||||
|
st.divider()
|
||||||
|
|
||||||
|
# Input Form - Using dynamic key to ensure fresh widgets for each row
|
||||||
|
with st.form(key=f"row_form_{idx}"):
|
||||||
|
st.subheader(f"{row['exercise']}")
|
||||||
|
st.caption(f"Set: {row['set']} | Group: {row['group']}")
|
||||||
|
|
||||||
|
c1, c2 = st.columns(2)
|
||||||
|
new_weight = c1.number_input("Weight", value=float(row['weight']), step=2.5)
|
||||||
|
new_reps = c2.number_input("Reps", value=int(row['reps']), step=1)
|
||||||
|
new_notes = st.text_input("Notes", value=str(row['notes']) if row['notes'] else "")
|
||||||
|
|
||||||
|
if st.form_submit_button("Save & Next"):
|
||||||
|
st.session_state.workout_df.at[idx, 'weight'] = new_weight
|
||||||
|
st.session_state.workout_df.at[idx, 'reps'] = new_reps
|
||||||
|
st.session_state.workout_df.at[idx, 'notes'] = new_notes
|
||||||
|
|
||||||
|
if idx < len(df) - 1:
|
||||||
|
st.session_state.current_index += 1
|
||||||
|
st.rerun()
|
||||||
|
|
||||||
|
st.divider()
|
||||||
|
st.dataframe(st.session_state.workout_df)
|
||||||
|
if st.button("Commit Workout to Database"):
|
||||||
|
save_workout(st.session_state.workout_df)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue