Skip to content

πŸ‹οΈβ€β™‚οΈ A Flask-based web app for tracking fitness goals, workouts, and exercise plans using SQLite, SQLAlchemy, and the Wger API. Docker-ready.

Notifications You must be signed in to change notification settings

xiaotuzi7877/Backend-Fitness-API-Flask

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Fitness and Activity Tracker

Overview

Fitness and Activity Tracker is a web application that helps users set and track their fitness goals. The app integrates with the wger Workout Manager API to provide exercise recommendations, log daily workouts, and visualize progress through charts.

This application is developed using Flask for the backend, SQLite for the database, and SQLAlchemy as the ORM. It includes account management features for secure user authentication and leverages Docker for containerization.

Features

  • Account Management

    • Securely store passwords using hashing with salts
    • Allow users to register, log in, and update their passwords
  • Workout Tracking

    • Log daily workouts and activities
    • Set and track fitness goals
    • View progress using charts
  • Exercise Recommendations

    • Fetch exercise recommendations from the wger Workout Manager API
    • Save favorite exercises or routines
  • Health Check

    • Verify the app's status through a health check route

Technologies Used

  • Backend Framework: Flask
  • Database: SQLite
  • ORM: SQLAlchemy
  • External API: wger Workout Manager API
  • Containerization: Docker

How to Run

Prerequisites

  • Python 3.10+
  • Docker (if containerizing the app)

Setup Instructions

  1. Clone the repository:
git clone <[email protected]:arnavSingh23/CS411-FinalProject.git>
cd fitness-tracker
  1. Create a virtual environment and install dependencies:
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
pip install -r requirements.txt
  1. Set up your environment variables:
    • Create a .env file in the project root and add the following variables:
FLASK_APP=app.py
FLASK_ENV=development
SECRET_KEY=<your-secret-key>
API_KEY=<wger-api-key>
  1. Initialize the database:
flask db init
flask db migrate
flask db upgrade
  1. Start the Flask application:
flask run
  1. Open the app in your browser at http://127.0.0.1:5000

Using Docker

  1. Build the Docker image:
docker build -t fitness-tracker .
  1. Run the Docker container:
docker run -p 5000:5000 --env-file .env fitness-tracker

API Endpoints

Authentication Routes

  1. Login
    • Route: /login
    • Method: POST
    • Purpose: Authenticate users by verifying their password against stored hashes
    • Request Format:
{
    "username": "string",
    "password": "string"
}
  • Response Format:
{
    "message": "Login successful",
    "token": "jwt-token-string"
}
  • Error Response:

  • Status: 401 Unauthorized

{
  "message": "Invalid username or password"
}
  • Status: 400 Bad Request
{
  "message": "Missing required fields"
}
  • Status 500 Interal Server Error
{
   "message": "An unexpected error occurred"
}
  1. Create Account
    • Route: /create-account
    • Method: POST
    • Purpose: Allow users to register
    • Request Format:
{
    "username": "string",
    "password": "string"
}
  • Response Format:
{
    "message": "Account created successfully"
}
  • Error Response:

  • Status: 400 Bad Request

{
  "message": "Username already exists"
}
  • Status: 400 Bad Request
{
  "message": "Missing required fields"
}
  • Status 500 Interal Server Error
{
   "message": "An unexpected error occurred"
}
  1. Update Password
    • Route: /update-password
    • Method: POST
    • Purpose: Allow users to update their password
    • Request Format:
{
    "username": "string",
    "current_password": "string",
    "new_password": "string"
}
  • Response Format:
{
    "message": "Password updated successfully"
}
  • Error Response:

  • Status: 401 Unauthorized

{
  "message": "Invalid username or current password"
}
  • Status: 400 Bad Request
{
  "message": "Missing required fields"
}
  • Status 500 Interal Server Error
{
   "message": "An unexpected error occurred"
}

Health Check Route

  • Route: /health

    • Method: GET
    • Purpose: Health check route to verify the app is running
  • Response Format:

{
    "status": "OK"
}

API Interaction Routes

  1. Log Workouts
    • Route: /log-workout
    • Method: POST
    • Purpose: Log user workouts
    • Request Format:
    {
        "user_id": "integer",
        "exercise_id": "integer",
        "repetitions": "integer",
        "weight": "float",
        "date": "YYYY-MM-DD",
        "comment": "string"
    }
    • Response Format:
    {
    "message": "Workout logged successfully"
    }
    
  2. View Workouts
    • Route: /view-workouts
    • Method: GET
    • Purpose: Retrieve all logged workouts for a user
    • Request Format:
    {
        "user_id": "integer"
    }
    • Response Format:
     {
         "exercise_id": "integer",
         "repetitions": "integer",
         "weight": "float",
         "date": "YYYY-MM-DD",
         "comment": "string"
     }
    
    
  3. Health Check
    • Route: /health
    • Method: GET
    • Purpose: Verify the app is running
    • Response Format:
{
    "status": "OK"
}
  1. Excercise Recommendandations

Route: /recommendations
Method: GET
Purpose:
Retrieve exercise recommendations from the Wger Workout Manager API based on specified filters for category and equipment.

Query Parameters:

Parameter Type Required Description
category str No Filter exercises by category ID.
equipment str No Filter exercises by equipment ID.

Request Format:

curl "http://127.0.0.1:5000/recommendations?category=4&equipment=7"

β‰ˆ


Expected Behavior:

  • The API fetches exercise data from the Wger Workout Manager API based on the provided category and equipment filters.
  • If the API call is successful, a list of exercises is returned in JSON format.
  • If the API call fails, an error message with status code 500 is returned.
  1. Save Exercise Route: /save-exercise
    Method: POST
    Purpose:
    Allow users to save favorite exercises into an in-memory dictionary for future access.

Query Parameters:

Parameter Type Required Description
user_id int Yes The ID of the user saving the exercise.
exercise_id int Yes The ID of the exercise being saved.
name str Yes The name of the exercise.
description str No A description of the exercise.
  • Request Format:
{
 "user_id": 1,
 "exercise_id": 101,
 "name": "Push-ups",
 "description": "An exercise for chest and triceps."
}
  • Response Format:
   
    {
        "exercise_id": "integer",
        "repetitions": "integer",
        "weight": "float",
        "date": "YYYY-MM-DD",
        "comment": "string"
    }

Expected Behavior:

  • This route allows users to save specific exercises as favorites.
  • Users must provide their user_id and details of the exercise to be saved.
  • If successful, a confirmation message is returned.
  • If an error occurs, a status code with a corresponding error message is returned.
  • Error Response:

  • Status: Status Code 400

{
  "message": "Missing required fields in the request."
}
  • Status: Status Code 500
{
  "message": "An unexpected error occurred."
}

About

πŸ‹οΈβ€β™‚οΈ A Flask-based web app for tracking fitness goals, workouts, and exercise plans using SQLite, SQLAlchemy, and the Wger API. Docker-ready.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published