services/main/Dockerfiles/app/App/app/app.py
2022-01-11 14:10:21 +01:00

108 lines
3.7 KiB
Python

# app/__init__.py
from flask import Flask, render_template , request, redirect, url_for, abort
from flask_bootstrap import Bootstrap
import requests
import shutil
import os
def create_app():
app = Flask(__name__)
Bootstrap(app)
NB_QUESTIONS = 8 # Number of questions (i.e attributes handle by yolo)
@app.route('/', methods=['POST', 'GET'])
def homepage():
if request.method == "POST":
# TO DO
# GENERATE GRID
r = requests.get('http://127.0.0.1:50001/')
filename = r.text
shutil.copy('/Datasets/grids/'+filename+'.png', './static/images/'+filename+'.png')
#r = requests.get('http://127.0.0.1:50011/create/TEST/')
#r = requests.get('http://127.0.0.1:50011/create/'+filename+'/')
return redirect(url_for('play',n=1,id=filename))
return render_template('homepage.html')
@app.route('/play/<string:id>/<int:n>/', methods=['POST', 'GET'])
def play(id="grille",n=1):
if (n==1):
# Call YOLO
r = requests.get('http://127.0.0.1:50015/create/'+id+'/')
r = requests.get('http://127.0.0.1:50011/create/'+id+'/')
if (n > NB_QUESTIONS or n < 1) :
abort(404)
# TO DO
# Get question n from database
r = requests.get("http://127.0.0.1:50011/question/"+str(n)).json()
q = r["intitule"]
attribute = r["attribut"]
answers=r["response"].split(";")
if request.method == "POST":
response=""
if 'YES' in request.form: # ON CLICK YES
response = "0"
elif 'NO' in request.form: # ON CLICK NO
response = "1"
elif 'STOP' in request.form: # ON CLICK STOP
# TO DO
# DELETE IMG
# parameters : id
return redirect(url_for('homepage'))
else:
for a in answers:
if a.upper() in request.form :
attribute=a
response="0"
# TO DO
# UPDATE GRID
# parameters : attribute , response, id
r = requests.get("http://127.0.0.1:50009/update/"+id+"/"+attribute+"/"+response)
nb_characters = int(r.text)
os.remove('./static/images/'+id+'.png')
shutil.copy('/Datasets/grids/'+id+'.png', './static/images/'+id+'.png')
if n == NB_QUESTIONS :
# EVERY QUESTIONS HAVE BEEN DONE
# Prendre un perso au pif ?
return redirect(url_for('end',id=id))
elif nb_characters == 1 :
# OR THE COMPUTER FOUND THE CHARACTER
return redirect(url_for('end',id=id))
elif nb_characters == 0 :
return render_template('lost.html')
else:
return redirect(url_for('play',n=n+1, id=id))
return render_template('play.html', numero = n, question = q, img = id,answers=answers)
@app.route('/end/<string:id>', methods=['POST', 'GET'])
def end(id = "grille") :
if request.method == "POST":
if 'YES' in request.form:
# TO DO
# GENERATE GRID
filename = "grille"
return redirect(url_for('play',n=1,id=filename))
elif 'NO' in request.form:
# TO DO
# DELETE IMG
# parameters : id
return redirect(url_for('homepage'))
return render_template('end.html',img=id)
if __name__ == "__main__":
app.run()