File size: 3,741 Bytes
90d30f6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | 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() |