-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
71 lines (54 loc) · 2.35 KB
/
main.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
import random, os, concurrent.futures
from flask import Flask, render_template, url_for, request
import modeltrainer, predictions, webscrapping
#Sets up the flask app
app = Flask(
__name__,
template_folder='templates', #Name of folder containing html files
static_folder='static' #Name of folder containing static files
)
# Prevents cache from using the old css file, makes it use the updated one
@app.context_processor
def override_url_for():
return dict(url_for=dated_url_for)
def dated_url_for(endpoint, **values):
if endpoint == 'static':
filename = values.get('filename', None)
if filename:
file_path = os.path.join(app.root_path,endpoint, filename)
values['q'] = int(os.stat(file_path).st_mtime)
return url_for(endpoint, **values)
# ^ ^ ^
@app.route('/')
def index(): # Home page
return render_template('index.html', display_products="none", length=None, loading_status="none")
@app.route('/', methods=['POST'])
def form():
age = request.form['age']
gender = request.form['gender']
with concurrent.futures.ThreadPoolExecutor() as executor:
#Runs the predict function from "predictions.py" and set the return varible to "categories"
categories = executor.submit(predictions.predict, age, gender).result()
#categories = predictions.predict(age,gender)
# Removes spaces in categories
cleaned_categories = []
for category in categories:
cleaned_categories.append(category.strip(" "))
categories = cleaned_categories
budget = request.form['budget']
interests = request.form['interests']
if interests:
interests = interests.split(',')
with concurrent.futures.ThreadPoolExecutor() as executor:
products = executor.submit(webscrapping.find_products, categories, budget, interests).result()
#products = webscrapping.find_products(categories, budget, interests)
# Removes spaces in categories ^^^
random.shuffle(products) # Shuffles the products
return render_template('index.html', display_products="block", products=products, length=int(len(products)+1), loading_status="none")
if __name__ == "__main__":
app.run(
debug=True,
#Starts the website
host='0.0.0.0', #Sets the host, required for repl to detect the site
port=random.randint(2000, 9000) #Randomly select the port the machine hosts on.
)