-
Notifications
You must be signed in to change notification settings - Fork 202
/
Copy pathapplication_lulu.py
74 lines (54 loc) · 2.47 KB
/
application_lulu.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
from flask import Flask,render_template,request,redirect
app_lulu = Flask(__name__)
app_lulu.vars={}
app_lulu.questions={}
app_lulu.questions['How many eyes do you have?']=('1','2','3')
app_lulu.questions['Which fruit do you like best?']=('banana','mango','pineapple')
app_lulu.questions['Do you like cupcakes?']=('yes','no','maybe')
app_lulu.nquestions=len(app_lulu.questions)
#should be 3
@app_lulu.route('/index_lulu',methods=['GET','POST'])
def index_lulu():
nquestions=app_lulu.nquestions
if request.method == 'GET':
return render_template('userinfo_lulu.html',num=nquestions)
else:
#request was a POST
app_lulu.vars['name'] = request.form['name_lulu']
app_lulu.vars['age'] = request.form['age_lulu']
f = open('%s_%s.txt'%(app_lulu.vars['name'],app_lulu.vars['age']),'w')
f.write('Name: %s\n'%(app_lulu.vars['name']))
f.write('Age: %s\n\n'%(app_lulu.vars['age']))
f.close()
return redirect('/main_lulu')
@app_lulu.route('/main_lulu')
def main_lulu2():
if len(app_lulu.questions)==0 : return render_template('end_lulu.html')
return redirect('/next_lulu')
#####################################
## IMPORTANT: I have separated /next_lulu INTO GET AND POST
## You can also do this in one function, with If and Else.
@app_lulu.route('/next_lulu',methods=['GET'])
def next_lulu(): #remember the function name does not need to match the URL
# for clarity (temp variables):
n=app_lulu.nquestions-len(app_lulu.questions)+1
q = list(app_lulu.questions.keys())[0] #python indexes at 0
a1=app_lulu.questions[q][0]
a2=app_lulu.questions[q][1]
a3=app_lulu.questions[q][2]
# save current question
app_lulu.currentq=q
return render_template('layout_lulu.html',num=n,question=q,ans1=a1,ans2=a2,ans3=a3)
@app_lulu.route('/next_lulu',methods=['POST'])
def next_lulu2(): #can't have two functions with the same name
# Here, we will collect data from the user.
# Then, we return to the main function, so it can tell us whether to
# display another question page, or to show the end page.
f=open('%s_%s.txt'%(app_lulu.vars['name'],app_lulu.vars['age']),'a') #a is for append
f.write('%s\n'%(app_lulu.currentq))
f.write('%s\n\n'%(request.form['answer_from_layout_lulu'])) #do you know where answer_lulu comes from?
f.close()
app_lulu.questions.pop(app_lulu.currentq)
return redirect('/main_lulu')
if __name__ == "__main__":
app_lulu.run(port=5001, debug=True)