-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
55 lines (40 loc) · 2.21 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
# current_app stores a reference to the global application object.
from flask import Flask, render_template, current_app, request, redirect
from flask_bootstrap import Bootstrap
import pandas as pd
import copy
import os
import random
from random_circuit_app import app
from random_circuit_app.forms import WorkoutsForm
from random_circuit_app.src.workout_info import BodyPart, Equipment, Difficulty
app.exercises_df = pd.read_csv(os.path.join('random_circuit_app', 'static', 'data', 'exercises_expanded.csv')) # load data once, bind to app
bootstrap = Bootstrap(app)
@app.route('/', methods=['GET','POST'])
def index():
form = WorkoutsForm()
form.set_choices()
if request.method == 'POST' and form.validate_on_submit():
body_part_choice = form.body_part_options.data
equipment_choice = form.equipment_options.data
difficulty_chocie = form.difficulty_options.data
number_of_workouts = form.number_of_workouts.data
# Filter workouts
filtered_df = pd.DataFrame()
for body_part in body_part_choice:
filtered_df = filtered_df.append(current_app.exercises_df[current_app.exercises_df['body_part__' + body_part] == True])
for equipment in equipment_choice:
filtered_df = filtered_df[filtered_df['equipment__' + equipment] == True]
filtered_df = filtered_df[filtered_df['difficulty'].isin(difficulty_chocie)]
# Choose randomly from filtered workouts
random_indicies = random.sample(range(filtered_df.shape[0]), number_of_workouts)
chosen_exercises = filtered_df.iloc[random_indicies][['title', 'body_part', 'difficulty', 'equipment', 'image1', 'image2']]
form.set_choices()
return render_template('index.html',
form=form,
chosen_exercises=chosen_exercises.values.tolist(),
table_exercises=chosen_exercises[['title', 'body_part', 'difficulty', 'equipment']].to_html(classes=["table-bordered", "table-striped"], header=True, index=False)
)
return render_template('index.html', form=form)
if __name__ == '__main__':
app.run()