Workout / app.py
Json026's picture
Create app.py
90d30f6 verified
Raw History Blame Contribute Delete
3.74 kB
import gradio as gr
import pandas as pd
import datetime
import os
# Define the storage file
LOG_FILE = "workout_log.csv"
# Step 1: Initialize the storage engine
def initialize_csv():
if not os.path.exists(LOG_FILE):
columns = ["Date", "Workout Type", "Duration (mins)", "Intensity", "Notes"]
df = pd.DataFrame(columns=columns)
df.to_csv(LOG_FILE, index=False)
initialize_csv()
# Step 2: Core logging logic
def log_workout(workout_type, duration, intensity, notes):
try:
# Capture current date
today = datetime.date.today().strftime("%Y-%m-%d")
# Create a new row dataframe
new_entry = pd.DataFrame([{
"Date": today,
"Workout Type": workout_type,
"Duration (mins)": int(duration),
"Intensity": intensity,
"Notes": notes if notes.strip() else "None"
}])
# Append to the CSV
new_entry.to_csv(LOG_FILE, mode='a', header=False, index=False)
# Fetch updated data to refresh the UI view
updated_df = pd.read_csv(LOG_FILE).tail(10) # Show last 10 entries
status_msg = f"βœ… Workout logged successfully for {today}!"
return status_msg, updated_df
except Exception as e:
return f"❌ Error logging workout: {str(e)}", pd.read_csv(LOG_FILE).tail(10)
def load_initial_data():
return pd.read_csv(LOG_FILE).tail(10)
# Step 3: Build the Gradio Blocks Layout
with gr.Blocks(title="Fitness Dashboard") as demo:
gr.Markdown("# πŸ‹οΈβ€β™‚οΈ My Exercise & Consistency Tracker")
gr.Markdown("Consistency beats intensity. Log your daily workout below to stay on track!")
with gr.Row():
# Left Column - Form Inputs
with gr.Column(scale=1):
gr.Markdown("### πŸ“ Log Today's Effort")
workout_type = gr.Dropdown(
choices=["HIIT Circuit", "Core Focus", "Cardio/Walking", "Strength Training", "Active Recovery/Stretch"],
label="Workout Type",
value="HIIT Circuit"
)
duration = gr.Slider(
minimum=5,
maximum=120,
value=20,
step=5,
label="Duration (Minutes)"
)
intensity = gr.Radio(
choices=["🟒 Light (Easy pace)", "🟑 Moderate (Breathing hard)", "πŸ”΄ Intense (Max effort)"],
label="Intensity / Effort Level",
value="🟑 Moderate (Breathing hard)"
)
notes = gr.Textbox(
label="Quick Notes / How did it feel?",
placeholder="e.g., Finished all rounds, felt energized",
lines=2
)
submit_btn = gr.Button("Submit Log Entry", variant="primary")
status_output = gr.Markdown("")
# Right Column - Quick View Analytics
with gr.Column(scale=1.2):
gr.Markdown("### πŸ“Š Recent Activity Log")
# Dynamic data table displaying the last few rows of the CSV
history_table = gr.Dataframe(
value=load_initial_data,
headers=["Date", "Workout Type", "Duration (mins)", "Intensity", "Notes"],
datatype=["str", "str", "number", "str", "str"],
interactive=False
)
# Event binding: clicking submit logs data, updates status, and refreshes the table
submit_btn.click(
fn=log_workout,
inputs=[workout_type, duration, intensity, notes],
outputs=[status_output, history_table]
)
if __name__ == "__main__":
demo.launch()