-
Notifications
You must be signed in to change notification settings - Fork 0
/
jeopardy.py
184 lines (129 loc) · 5.55 KB
/
jeopardy.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
from flask import Flask, render_template, url_for, flash, redirect, request
from forms import RegistrationForm, LoginForm, ApiForm, SearchCategoryForm, QuestionForm
from tables import ClueTable, Clue, CategoryTable, Category, JeopardyBoard, JeopardyTile
from test_api import display_clue, get_categories
import json
import requests
app = Flask(__name__)
#api = FlaskAPI(__name__)
app.config['SECRET_KEY'] = '5791628bb0b13ce0c676dfde280ba245'
#-------------------------------------------------------
#
# STUFF FOR JEOPARDY BOARD
#
#-------------------------------------------------------
cat_ids = [21, 42, 136, 306, 780]
data_big = [[0]]*5 # initializing 5x5 array for boards
with open('data_for_now.json', 'r') as f:
l = json.load(f)
data_big = l
f.close()
#-------------------------------------------------------
@app.route("/", methods=['GET', 'POST'])
@app.route("/start", methods=['GET', 'POST'])
def start(): #After searching, redirect to list of options
form = ApiForm()
if request.method == 'POST':
cat_name = form.category.data
return redirect(url_for('searchCategory', board=True)) #?
return render_template('start.html', form=form)
#-------------------------------------------------------
@app.route("/searchCategory", methods=['GET', 'POST'])
def searchCategory(): #After searching, redirect to list of options
form = SearchCategoryForm()
board = request.args.get('board') #this is a string!!
if form.validate_on_submit():
category_name = form.category.data
if board == 'True':
return redirect(url_for('chooseCategoryBoard', cat_name = category_name))
elif board == 'False':
return redirect(url_for('chooseCategory', cat_name = category_name))
return render_template('searchCategories.html', form=form)
#-------------------------------------------------------
@app.route("/chooseCategory",methods=['GET', 'POST'])
def chooseCategory():
cat_name = request.args.get('cat_name')
form = ApiForm()
cat_list = []
#make a table for the category entered
with open('categories.json', 'r') as f:
all_categories = json.load(f)
for item in all_categories:
if item['title'] and cat_name in item['title']:
cat_list.append(item)
return render_template('chooseCategory.html', form=form, cat_list=cat_list)
#-------------------------------------------------------
@app.route("/chooseCategoryBoard",methods=['GET', 'POST'])
def chooseCategoryBoard():
cat_name = request.args.get('cat_name')
form = ApiForm()
cat_list = []
allCatTableItems = []
tableCat = []
#make a table for the category entered
with open('categories.json', 'r') as f:
all_categories = json.load(f)
for item in all_categories:
if item['title'] and cat_name in item['title']:
cat_list.append(item)
if form.validate_on_submit:
cat_id = 123 #number obatined from clicking the table
for item in cat_list: # find the dictionary for the category inputed
if item['id'] and id == item['id']:
tableCat.append(item)
break
for item in cat_list:
allCatTableItems.append(Category(item['title'],item['clues_count']))
table = CategoryTable(allCatTableItems)
return render_template('chooseCategoryBoard.html', table=table, form=form, cat_list=cat_list)
#-------------------------------------------------------
@app.route("/question",methods=['GET', 'POST'])
def question():
cat_id = request.args.get('cat_id')
form = QuestionForm()
clues = display_clue(cat_id)
filtered_clues = clues
if form.validate_on_submit():
value = form.value_dropdown.data
filtered_clues = []
for clue in clues:
print(type(clue['value']),type(value))
if clue['value'] == int(value):
filtered_clues.append(clue)
if len(filtered_clues) == 0:
flash("No clues of value: " + value,'danger')
return render_template('question.html',clues=filtered_clues, form=form)
#-------------------------------------------------------
@app.route("/jeopardy", methods=['GET', 'POST'])
def jeopardy():
form = ApiForm()
question = ""
answer = ""
jeopardy_data = data_big
if request.method == 'POST':
keys = list(request.form.keys()) # which button was pressed
if(len(keys) > 0):
which_button = keys[len(keys) - 1]
row = int(which_button[7])
col = int(which_button[9])
print(which_button, jeopardy_data[row][col])
question = jeopardy_data[row][col]['question']
answer = jeopardy_data[row][col]['answer']
flash("Question: " + question,"danger")
flash("Answer: " + answer,"success")
return render_template("jeopardy.html", form=form, question=question, answer=answer) #table=table
#-------------------------------------------------------
@app.route("/lookup", methods=['GET', 'POST'])
def lookup():
form = ApiForm()
if form.validate_on_submit:
num_cat = form.count.data
data = get_categories(num_cat) #list of dicts
tableItems = []
for cat in data:
tableItems.append(Category(cat['title'],cat['clues_count']))
table = CategoryTable(tableItems)
return render_template('lookup.html', title='Lookup ', table=table, form=form)
#-------------------------------------------------------
if __name__ == '__main__':
app.run(debug=True)