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>
<meta charset="UTF-8">
<title>Fitness Tracker</title>
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
</head>
<body>
<h1>Welcome to the Fitness Tracker</h1>
<a href="/log">Log a Workout</a><br>
<a href="/history">View Workout History</a>
</body>
</html>
templates/log.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Log a Workout</title>
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
</head>
<body>
<h1>Log a Workout</h1>
<form method="post">
<label>Date:</label><br>
<input type="date" name="date" required><br>
<label>Type:</label><br>
<input type="text" name="type" required><br>
<label>Duration (in minutes):</label><br>
<input type="number" name="duration" required><br>
<label>Calories Burned:</label><br>
<input type="number" name="calories" required><br>
<button type="submit">Log Workout</button>
</form>
<a href="/">Back to Home</a>
</body>
</html>
templates/history.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Workout History</title>
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
</head>
<body>
<h1>Workout History</h1>
<table>
<tr>
<th>Date</th>
<th>Type</th>
<th>Duration (min)</th>
<th>Calories</th>
</tr>
{% for workout in workouts %}
<tr>
<td>{{ workout[1] }}</td>
<td>{{ workout[2] }}</td>
<td>{{ workout[3] }}</td>
<td>{{ workout[4] }}</td>
</tr>
{% endfor %}
</table>
<a href="/">Back to Home</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
- Navigate to your project directory in the terminal.
- Run the application:
python app.py
- Open your web browser and go to
http://127.0.0.1:5000
.
Leave a Reply