My Blog

My WordPress Blog

My Blog

My WordPress Blog

Fitness Tracker Application

Step 1: Create the Flask Application

app.py

from flask import Flask, render_template, request, redirect, url_for
import sqlite3

app = Flask(__name__)

def init_db():
with sqlite3.connect("fitness_tracker.db") as conn:
    conn.execute('''
        CREATE TABLE IF NOT EXISTS workouts (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            date TEXT NOT NULL,
            type TEXT NOT NULL,
            duration INTEGER NOT NULL,
            calories INTEGER NOT NULL
        )
    ''')
conn.close()
@app.route('/') def index():
return render_template('index.html')
@app.route('/log', methods=['GET', 'POST']) def log():
if request.method == 'POST':
    date = request.form['date']
    type_ = request.form['type']
    duration = request.form['duration']
    calories = request.form['calories']
    
    with sqlite3.connect("fitness_tracker.db") as conn:
        conn.execute('''
            INSERT INTO workouts (date, type, duration, calories)
            VALUES (?, ?, ?, ?)
        ''', (date, type_, duration, calories))
    
    return redirect(url_for('history'))
return render_template('log.html')
@app.route('/history') def history():
with sqlite3.connect("fitness_tracker.db") as conn:
    cursor = conn.execute('SELECT * FROM workouts ORDER BY date DESC')
    workouts = cursor.fetchall()
return render_template('history.html', workouts=workouts)
if __name__ == '__main__':
init_db()
app.run(debug=True)

Step 2: Create the HTML Templates

templates/index.html

<!DOCTYPE html>
<html lang="en">
<head>
&lt;meta charset="UTF-8">
&lt;title>Fitness Tracker&lt;/title>
&lt;link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
</head> <body>
&lt;h1>Welcome to the Fitness Tracker&lt;/h1>
&lt;a href="/log">Log a Workout&lt;/a>&lt;br>
&lt;a href="/history">View Workout History&lt;/a>
</body> </html>

templates/log.html

<!DOCTYPE html>
<html lang="en">
<head>
&lt;meta charset="UTF-8">
&lt;title>Log a Workout&lt;/title>
&lt;link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
</head> <body>
&lt;h1>Log a Workout&lt;/h1>
&lt;form method="post">
    &lt;label>Date:&lt;/label>&lt;br>
    &lt;input type="date" name="date" required>&lt;br>
    &lt;label>Type:&lt;/label>&lt;br>
    &lt;input type="text" name="type" required>&lt;br>
    &lt;label>Duration (in minutes):&lt;/label>&lt;br>
    &lt;input type="number" name="duration" required>&lt;br>
    &lt;label>Calories Burned:&lt;/label>&lt;br>
    &lt;input type="number" name="calories" required>&lt;br>
    &lt;button type="submit">Log Workout&lt;/button>
&lt;/form>
&lt;a href="/">Back to Home&lt;/a>
</body> </html>

templates/history.html

<!DOCTYPE html>
<html lang="en">
<head>
&lt;meta charset="UTF-8">
&lt;title>Workout History&lt;/title>
&lt;link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
</head> <body>
&lt;h1>Workout History&lt;/h1>
&lt;table>
    &lt;tr>
        &lt;th>Date&lt;/th>
        &lt;th>Type&lt;/th>
        &lt;th>Duration (min)&lt;/th>
        &lt;th>Calories&lt;/th>
    &lt;/tr>
    {% for workout in workouts %}
    &lt;tr>
        &lt;td>{{ workout&#91;1] }}&lt;/td>
        &lt;td>{{ workout&#91;2] }}&lt;/td>
        &lt;td>{{ workout&#91;3] }}&lt;/td>
        &lt;td>{{ workout&#91;4] }}&lt;/td>
    &lt;/tr>
    {% endfor %}
&lt;/table>
&lt;a href="/">Back to Home&lt;/a>
</body> </html>

Step 3: Add Basic Styles

static/styles.css

body {
font-family: Arial, sans-serif;
margin: 20px;
} h1 {
color: #333;
} table {
width: 100%;
border-collapse: collapse;
} table, th, td {
border: 1px solid #ddd;
} th, td {
padding: 8px;
text-align: left;
}

Step 4: Run the Application

  1. Navigate to your project directory in the terminal.
  2. Run the application:
python app.py
  1. Open your web browser and go to http://127.0.0.1:5000.
Fitness Tracker Application

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top