Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import datetime
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Define the storage file
|
| 7 |
+
LOG_FILE = "workout_log.csv"
|
| 8 |
+
|
| 9 |
+
# Step 1: Initialize the storage engine
|
| 10 |
+
def initialize_csv():
|
| 11 |
+
if not os.path.exists(LOG_FILE):
|
| 12 |
+
columns = ["Date", "Workout Type", "Duration (mins)", "Intensity", "Notes"]
|
| 13 |
+
df = pd.DataFrame(columns=columns)
|
| 14 |
+
df.to_csv(LOG_FILE, index=False)
|
| 15 |
+
|
| 16 |
+
initialize_csv()
|
| 17 |
+
|
| 18 |
+
# Step 2: Core logging logic
|
| 19 |
+
def log_workout(workout_type, duration, intensity, notes):
|
| 20 |
+
try:
|
| 21 |
+
# Capture current date
|
| 22 |
+
today = datetime.date.today().strftime("%Y-%m-%d")
|
| 23 |
+
|
| 24 |
+
# Create a new row dataframe
|
| 25 |
+
new_entry = pd.DataFrame([{
|
| 26 |
+
"Date": today,
|
| 27 |
+
"Workout Type": workout_type,
|
| 28 |
+
"Duration (mins)": int(duration),
|
| 29 |
+
"Intensity": intensity,
|
| 30 |
+
"Notes": notes if notes.strip() else "None"
|
| 31 |
+
}])
|
| 32 |
+
|
| 33 |
+
# Append to the CSV
|
| 34 |
+
new_entry.to_csv(LOG_FILE, mode='a', header=False, index=False)
|
| 35 |
+
|
| 36 |
+
# Fetch updated data to refresh the UI view
|
| 37 |
+
updated_df = pd.read_csv(LOG_FILE).tail(10) # Show last 10 entries
|
| 38 |
+
|
| 39 |
+
status_msg = f"β
Workout logged successfully for {today}!"
|
| 40 |
+
return status_msg, updated_df
|
| 41 |
+
except Exception as e:
|
| 42 |
+
return f"β Error logging workout: {str(e)}", pd.read_csv(LOG_FILE).tail(10)
|
| 43 |
+
|
| 44 |
+
def load_initial_data():
|
| 45 |
+
return pd.read_csv(LOG_FILE).tail(10)
|
| 46 |
+
|
| 47 |
+
# Step 3: Build the Gradio Blocks Layout
|
| 48 |
+
with gr.Blocks(title="Fitness Dashboard") as demo:
|
| 49 |
+
gr.Markdown("# ποΈββοΈ My Exercise & Consistency Tracker")
|
| 50 |
+
gr.Markdown("Consistency beats intensity. Log your daily workout below to stay on track!")
|
| 51 |
+
|
| 52 |
+
with gr.Row():
|
| 53 |
+
# Left Column - Form Inputs
|
| 54 |
+
with gr.Column(scale=1):
|
| 55 |
+
gr.Markdown("### π Log Today's Effort")
|
| 56 |
+
|
| 57 |
+
workout_type = gr.Dropdown(
|
| 58 |
+
choices=["HIIT Circuit", "Core Focus", "Cardio/Walking", "Strength Training", "Active Recovery/Stretch"],
|
| 59 |
+
label="Workout Type",
|
| 60 |
+
value="HIIT Circuit"
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
duration = gr.Slider(
|
| 64 |
+
minimum=5,
|
| 65 |
+
maximum=120,
|
| 66 |
+
value=20,
|
| 67 |
+
step=5,
|
| 68 |
+
label="Duration (Minutes)"
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
intensity = gr.Radio(
|
| 72 |
+
choices=["π’ Light (Easy pace)", "π‘ Moderate (Breathing hard)", "π΄ Intense (Max effort)"],
|
| 73 |
+
label="Intensity / Effort Level",
|
| 74 |
+
value="π‘ Moderate (Breathing hard)"
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
notes = gr.Textbox(
|
| 78 |
+
label="Quick Notes / How did it feel?",
|
| 79 |
+
placeholder="e.g., Finished all rounds, felt energized",
|
| 80 |
+
lines=2
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
submit_btn = gr.Button("Submit Log Entry", variant="primary")
|
| 84 |
+
status_output = gr.Markdown("")
|
| 85 |
+
|
| 86 |
+
# Right Column - Quick View Analytics
|
| 87 |
+
with gr.Column(scale=1.2):
|
| 88 |
+
gr.Markdown("### π Recent Activity Log")
|
| 89 |
+
# Dynamic data table displaying the last few rows of the CSV
|
| 90 |
+
history_table = gr.Dataframe(
|
| 91 |
+
value=load_initial_data,
|
| 92 |
+
headers=["Date", "Workout Type", "Duration (mins)", "Intensity", "Notes"],
|
| 93 |
+
datatype=["str", "str", "number", "str", "str"],
|
| 94 |
+
interactive=False
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# Event binding: clicking submit logs data, updates status, and refreshes the table
|
| 98 |
+
submit_btn.click(
|
| 99 |
+
fn=log_workout,
|
| 100 |
+
inputs=[workout_type, duration, intensity, notes],
|
| 101 |
+
outputs=[status_output, history_table]
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
if __name__ == "__main__":
|
| 105 |
+
demo.launch()
|