56 lines
1.9 KiB
Python
56 lines
1.9 KiB
Python
|
|
import streamlit as st
|
||
|
|
import mysql.connector
|
||
|
|
from mysql.connector import errorcode
|
||
|
|
import pandas as pd
|
||
|
|
from datetime import datetime, date
|
||
|
|
|
||
|
|
workout_table = ['chest','back','legs']
|
||
|
|
selected_workout = st.selectbox('Select a Workout',options=workout_table)
|
||
|
|
edited_df=[]
|
||
|
|
|
||
|
|
try:
|
||
|
|
conn = mysql.connector.connect(user='root',password='Throwaway', host='localhost',port=3306,database='workouts')
|
||
|
|
except mysql.connector.Error as err:
|
||
|
|
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
|
||
|
|
print("Something is wrong with your user name or password")
|
||
|
|
elif err.errno == errorcode.ER_BAD_DB_ERROR:
|
||
|
|
print("Database does not exist")
|
||
|
|
else:
|
||
|
|
print(err)
|
||
|
|
|
||
|
|
else:
|
||
|
|
cursor = conn.cursor()
|
||
|
|
if selected_workout == 'chest':
|
||
|
|
query = ("SELECT * FROM `chest`")
|
||
|
|
working_table='chest'
|
||
|
|
elif selected_workout == 'back':
|
||
|
|
query = ("SELECT * FROM `back`")
|
||
|
|
working_table='back'
|
||
|
|
else:
|
||
|
|
query = ("SELECT * FROM `legs`")
|
||
|
|
working_table='legs'
|
||
|
|
cursor.execute(query)
|
||
|
|
data = cursor.fetchall()
|
||
|
|
conn.commit()
|
||
|
|
cursor.close()
|
||
|
|
conn.close()
|
||
|
|
|
||
|
|
df = pd.DataFrame(data,columns=cursor.column_names)
|
||
|
|
edited_df = st.data_editor(df)
|
||
|
|
|
||
|
|
# Button to save all changes back to the dataframe
|
||
|
|
if st.button('Save All Changes'):
|
||
|
|
st.write("All changes saved!")
|
||
|
|
conn = mysql.connector.connect(user='root',password='Throwaway', host='localhost',port=3306,database='workouts')
|
||
|
|
cursor = conn.cursor()
|
||
|
|
query = (f"DELETE FROM {working_table}")
|
||
|
|
cursor.execute(query)
|
||
|
|
for index, row in edited_df.iterrows():
|
||
|
|
query = (f"INSERT INTO `{working_table}`"
|
||
|
|
"(`date`,`group`,`exercise`,`set`,`weight`,`reps`,`notes`) "
|
||
|
|
"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]))
|
||
|
|
conn.commit()
|
||
|
|
cursor.close()
|
||
|
|
conn.close()
|