Example #1
0
from flask import Flask, render_template, request, redirect
testapp = Flask(__name__)

testapp.vars={}

testapp.questions={}
testapp.questions['question']=('moo','poo','goo')
testapp.questions['Which fruit do you like best?']=('banana','mango','pineapple')
testapp.questions['Do you like cupcakes?']=('yes','no','maybe')
testapp.nquestions=len(testapp.questions)

@testapp.route('/index', methods=['GET', 'POST'])
def index():
	nquestions=testapp.nquestions
	if request.method == 'GET':
		return render_template('userinfo.html', num=nquestions)
	else:
		#request is likely to be a post
		testapp.vars['name1'] = request.form['name']
		testapp.vars['age'] = request.form['age']
		testapp.vars['gender'] = request.form['gender']

		#write file containing user information
		f = open('%s_%s.txt'%(testapp.vars['name1'], testapp.vars['age']),'w')
		f.write('Name: %s\n'%(testapp.vars['name1']))
		f.write('Age: %s\n\n'%(testapp.vars['age']))
		f.write('Gender: %s\n'%(testapp.vars['gender']))
		f.close()
		
		return redirect('/main')
Example #2
0
from flask import Flask, render_template, request, redirect
app = Flask(__name__)
#test
app.vars = {}

app.questions = {}
app.questions['How many eyes do you have?'] = ('1','2','3')
app.questions['Which fruit do you like best?'] = ('banana','mango','pineapple')
app.questions['Do you like cupcakes?'] = ('yes','no','maybe')

app.nquestions = len(app.questions)

@app.route('/index', methods=['GET','POST'])
def index():
	nquestions = app.nquestions
	if request.method == 'GET':
		return render_template('userinfo_lulu.html',num=nquestions)
		
	else:
		#request was a POST
		app.vars['name'] = request.form['name']
		app.vars['age'] = request.form['age']
		
		f = open('%s_%s.txt' % (app.vars['name'], app.vars['age']),'w')
		f.write('Name: %s\n' % (app.vars['name']))
		f.write('Age: %s\n\n' % (app.vars['age']))
		f.close()
		
		return redirect('/main')
	
@app.route('/main')
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()
# The object Flask() allows setting dictionaries to collect information from the user
# We set an empty dictonary that will collect name and age of user.
app_simpleApp.vars={}

# We set another dictionary for the questions asked in the form that will be displayed 
# in the web application
app_simpleApp.questions={}

# We populate the dictionary with questions and options
app_simpleApp.questions['How many eyes do you have?']=('1','2','3')
app_simpleApp.questions['Which fruit do you like best?']=('banana','mango','pineapple')
app_simpleApp.questions['Do you like cupcakes?']=('yes','no','maybe')

# We get the number of questions from the length of the dictionary
app_simpleApp.nquestions=len(app_simpleApp.questions)


@app_simpleApp.route('/index_simpleApp',methods=['GET','POST'])
# The app will be launched at 127.0.0.1:5000/index_simpleApp
def index_simpleApp():

	"""
	This function displays the corresponding HTML page that presents
	the survey and its questions. It also stores the answers provided
	by the user into a txt file
	"""

	# The number questions, nquestions, is gotten from the previous
	# app_simpleApp.nquestions
	nquestions = app_simpleApp.nquestions