-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
258 lines (217 loc) · 9.14 KB
/
app.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
from flask import Flask, render_template, request, redirect, url_for, session, jsonify, flash
import sqlite3
from datetime import datetime
from fuzzywuzzy import process
import utils
app = Flask(__name__)
app.secret_key = 'supersecretkey'
exercises_list = utils.load_exercises('exercises.txt')
# Initialize SQLite database
def init_db():
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS workouts
(id INTEGER PRIMARY KEY, exercise TEXT, sets INTEGER, reps INTEGER, weights TEXT, date TEXT)''')
c.execute('''CREATE TABLE IF NOT EXISTS workout_templates
(id INTEGER PRIMARY KEY, name TEXT, exercises TEXT)''')
conn.commit()
conn.close()
@app.route('/')
def home():
if 'loggedin' in session:
return render_template('index.html')
return redirect(url_for('login'))
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
password = request.form['password']
if password == 'yourpassword': # Replace with a more secure method
session['loggedin'] = True
return redirect(url_for('home'))
return render_template('login.html')
@app.route('/logout')
def logout():
session.pop('loggedin', None)
return redirect(url_for('login'))
@app.route('/dashboard')
def dashboard():
if 'loggedin' in session:
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute('SELECT * FROM workouts')
workouts = c.fetchall()
conn.close()
return render_template('dashboard.html', workouts=workouts)
return redirect(url_for('login'))
@app.route('/add_workout', methods=['POST'])
def add_workout():
if 'loggedin' in session:
exercise = request.form['exercise']
sets = int(request.form['sets'])
reps = request.form['reps']
weights = ','.join(request.form.getlist('weights'))
date = datetime.now().strftime('%Y-%m-%d')
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute("INSERT INTO workouts (exercise, sets, reps, weights, date) VALUES (?, ?, ?, ?, ?)",
(exercise, sets, reps, weights, date))
conn.commit()
conn.close()
return redirect(url_for('home'))
return redirect(url_for('login'))
@app.route('/history')
def history():
if 'loggedin' in session:
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute('SELECT * FROM workouts')
workouts = c.fetchall()
conn.close()
return render_template('history.html', workouts=workouts)
return redirect(url_for('login'))
@app.route('/delete_workout/<int:workout_id>', methods=['POST'])
def delete_workout(workout_id):
if 'loggedin' in session:
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute('DELETE FROM workouts WHERE id = ?', (workout_id,))
conn.commit()
conn.close()
return redirect(url_for('history'))
return redirect(url_for('login'))
@app.route('/search', methods=['GET'])
def search():
query = request.args.get('query', '')
if query:
results = process.extract(query, exercises_list, limit=10)
results = [result[0] for result in results] # Extract the matched exercise names
else:
results = []
return jsonify(results)
#Workouts from below here
@app.route('/workout_templates', methods=['GET', 'POST'])
def workout_templates():
if 'loggedin' in session:
if request.method == 'POST':
name = request.form['name']
exercises = []
i = 1
while f'exercise_name_{i}' in request.form:
exercise_name = request.form[f'exercise_name_{i}']
sets = request.form[f'sets_{i}']
reps = request.form[f'reps_{i}']
exercises.append({'name': exercise_name, 'sets': int(sets), 'reps': int(reps)})
i += 1
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute("INSERT INTO workout_templates (name, exercises) VALUES (?, ?)", (name, str(exercises)))
conn.commit()
conn.close()
return redirect(url_for('workout_templates'))
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute('SELECT * FROM workout_templates')
templates = c.fetchall()
conn.close()
return render_template('workout_templates.html', templates=templates)
return redirect(url_for('login'))
@app.route('/existing_templates', methods=['GET', 'POST'])
def existing_templates():
if 'loggedin' in session:
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute('SELECT * FROM workout_templates')
templates = c.fetchall()
conn.close()
return render_template('existing_templates.html', templates=templates)
return redirect(url_for('login'))
@app.route('/create_template', methods=['POST'])
def create_template():
if 'loggedin' in session:
name = request.form['name']
exercises = ','.join(request.form.getlist('exercises'))
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute("INSERT INTO workout_templates (name, exercises) VALUES (?, ?)", (name, exercises))
conn.commit()
conn.close()
return redirect(url_for('workout_templates'))
return redirect(url_for('login'))
@app.route('/delete_template/<int:template_id>', methods=['POST'])
def delete_template(template_id):
if 'loggedin' in session:
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute('DELETE FROM workout_templates WHERE id = ?', (template_id,))
conn.commit()
conn.close()
return redirect(url_for('workout_templates'))
return redirect(url_for('login'))
@app.route('/edit_template/<int:template_id>', methods=['GET', 'POST'])
def edit_template(template_id):
if 'loggedin' in session:
conn = sqlite3.connect('database.db')
c = conn.cursor()
if request.method == 'POST':
name = request.form['name']
exercises = []
i = 1
while f'exercise_name_{i}' in request.form:
exercise_name = request.form[f'exercise_name_{i}']
sets = request.form[f'sets_{i}']
reps = request.form[f'reps_{i}']
exercises.append({'name': exercise_name, 'sets': int(sets), 'reps': int(reps)})
i += 1
c.execute("UPDATE workout_templates SET name = ?, exercises = ? WHERE id = ?", (name, str(exercises), template_id))
conn.commit()
conn.close()
return redirect(url_for('workout_templates'))
c.execute('SELECT * FROM workout_templates WHERE id = ?', (template_id,))
template = c.fetchone()
conn.close()
exercises = eval(template[2]) # Convert string back to list of dictionaries
return render_template('edit_template.html', template=template, exercises=exercises)
return redirect(url_for('login'))
@app.route('/run_workout/<int:template_id>', methods=['GET', 'POST'])
def run_workout(template_id):
if 'loggedin' in session:
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute('SELECT * FROM workout_templates WHERE id = ?', (template_id,))
template = c.fetchone()
conn.close()
# Fetch the last recorded weights for each exercise
last_weights = {}
conn = sqlite3.connect('database.db')
c = conn.cursor()
for exercise in eval(template[2]):
c.execute('''SELECT weights FROM workouts WHERE exercise = ? ORDER BY date DESC LIMIT 1''', (exercise['name'],))
last_record = c.fetchone()
if last_record:
last_weights[exercise['name']] = last_record[0].split(',')
else:
last_weights[exercise['name']] = []
conn.close()
if request.method == 'POST':
for exercise_data in request.form.getlist('exercise_data'):
exercise_name, sets, reps = exercise_data.split(';')
weights = []
for i in range(1, int(sets) + 1):
weight = request.form.get(f'weight_{exercise_name}_{i}')
if weight:
weights.append(weight)
date = datetime.now().strftime('%Y-%m-%d')
weights_str = ','.join(weights)
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute("INSERT INTO workouts (exercise, sets, reps, weights, date) VALUES (?, ?, ?, ?, ?)",
(exercise_name, sets, reps, weights_str, date))
conn.commit()
conn.close()
return redirect(url_for('history'))
exercises = eval(template[2]) # Convert string back to list of dictionaries
return render_template('run_workout.html', template=template, exercises=exercises, last_weights=last_weights)
return redirect(url_for('login'))
if __name__ == '__main__':
init_db()
app.run(debug=True)