ОК, так что я понял это сам. И поделиться ответом для тех, кто хочет решить эту проблему.
Я сейчас читаю данные анкеты из файла JSON. У меня есть два маршрута сейчас
- Чтобы получить первый вопрос.
- Чтобы получить следующий вопрос на основе предыдущего выбора.
data.json
{
"questionnaire_0" :
[
{
"question": "Are you Hungry?",
"options": ["Yes", "No"]
},
{
"Yes": {
"question": "What would you like to eat?",
"options": ["Hamburger", "Pizza", "Pop Corn", "Chicken"]
},
"No": {
"message": "OK, call me when you are hungry."
}
},
{
"Hamburger": {
"message": "Nice, I will order a hamburger for you."
},
"Pizza": {
"question": "Would you like pizza with mushroom?",
"options": ["Yes", "No"]
},
"Pop Corn": {
"question": "Would you like pop corn with cheese?",
"options": ["Yes", "No"]
},
"Chicken": {
"question": "Would you like chicken with cheese?",
"options": ["Yes", "No"]
}
},
{
"Pizza": {
"Yes": {
"message": "Ok, i will order the best pizza in town for you."
},
"No": {
"message": "No? Well... stay hungry then."
}
},
"Pop Corn": {
"Yes": {
"message": "Ok, i will order the best pop corn in town for you."
},
"No": {
"message": "No? Well... stay hungry then."
}
},
"Chicken": {
"Yes": {
"message": "Ok, i will order the best chicken in town for you."
},
"No": {
"message": "No? Well... stay hungry then."
}
}
}
],
"questionnaire_1":
[
{
"question": "Are you bored?",
"options": ["Yes", "No"]
},
{
"Yes": {
"question": "What would you like me to play?",
"options": ["Song", "Movie", "Music", "Ted Talk"]
},
"No": {
"message": "OK, call me when you are bored."
}
},
{
"Song": {
"message": "Nice, I will play your favorite song."
},
"Movie": {
"question": "Would you like to watch action movie?",
"options": ["Yes", "No"]
},
"Music": {
"question": "Would you like relaxing music?",
"options": ["Yes", "No"]
},
"Ted Talk": {
"question": "Would you like me to play simon sinek talk?",
"options": ["Yes", "No"]
}
},
{
"Movie": {
"Yes": {
"message": "Ok, i am playing Avengers."
},
"No": {
"message": "No? Well... stay bored then."
}
},
"Music": {
"Yes": {
"message": "Ok, i will play the most relaxing music."
},
"No": {
"message": "No? Well... stay bored then."
}
},
"Ted Talk": {
"Yes": {
"message": "Ok, get ready to feel inspired."
},
"No": {
"message": "No? Well... stay bored then."
}
}
}
]
}
app.py
from flask import Flask, jsonify
import json
import sys
# global variables
num = 0
last_choice = 'empty'
questionnaire_key = ''
user_choice = []
data = {}
app = Flask(__name__)
with open('static/data.json') as f:
data = json.load(f)
print(data, file=sys.stdout)
@app.route('/<int:index>/Start')
def StartQuestionnaire(index):
global num, last_choice, questionnaire_key, user_choice
num = 0
last_choice = 'empty'
user_choice.clear()
questionnaire_key = 'questionnaire_' + str(index)
user_choice.append(data[questionnaire_key][0]['question'])
print(user_choice, file=sys.stdout)
return jsonify(data[questionnaire_key][0])
# last selected option will be passed as keyword argument
@app.route('/<int:index>/<string:option>')
def GetQuestion(index, option):
global num, last_choice, questionnaire_key
num = num + 1
response = {}
user_choice.append(option)
if last_choice != 'empty':
response = data[questionnaire_key][num][last_choice][option]
else:
if option != 'Yes' and option != 'No':
last_choice = option
response = data[questionnaire_key][num][option]
if option == 'No' or num == len(data[questionnaire_key]) - 1:
for elem in user_choice:
print(elem, file=sys.stdout)
return jsonify(response)
if __name__ == '__main__':
app.run(debug=False, use_reloader=False)