ref: flask file;add: match and compare;fix: module import, sqlite db in flask

This commit is contained in:
liuyihui 2021-12-30 00:50:09 +08:00
parent 4c36ff8a90
commit a5712c9b17
6 changed files with 71 additions and 36 deletions

31
app.py
View File

@ -8,14 +8,37 @@
# here put the import lib # here put the import lib
import os import os
from flask import Flask, request, render_template import lib.elo as elo
import lib.utils as utils
from flask import Flask, request, redirect, render_template
app = Flask(__name__, template_folder='./public/', static_folder='./public/static/') P0 = None
P1 = None
ses = utils.get_session()
app = Flask(__name__, template_folder='./templates/', static_folder='./static/')
app.config['ROOT_FOLDER'] = os.path.abspath('.') app.config['ROOT_FOLDER'] = os.path.abspath('.')
@app.route('/') @app.route('/', methods=["GET"])
@app.route('/new', methods=["GET"])
def root(): def root():
return render_template('index.html') global P0, P1
P0, P1 = utils.match(ses)
return render_template(
'index.html',
p0src='./static/pic/{:d}.jpeg'.format(P0.id),
p0name=P0.name,
p0work='{:.2f}'.format(P0.rate),
p1src='./static/pic/{:d}.jpeg'.format(P1.id),
p1name=P1.name,
p1work='{:.2f}'.format(P1.rate)
)
@app.route('/elo', methods=["GET"])
def elo_res():
res = request.args.get('win')
elo.set_result(P0, P1, int(res))
ses.commit()
return redirect('/new')
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -8,7 +8,7 @@
# here put the import lib # here put the import lib
import math import math
from db_init import Person from lib.db_init import Person
def __compute_wre(A: Person, B: Person) -> float: def __compute_wre(A: Person, B: Person) -> float:
'''compute win rate expectation (wre for short) '''compute win rate expectation (wre for short)

View File

@ -7,17 +7,15 @@
''' '''
# here put the import lib # here put the import lib
import elo
import math import math
import random
from typing import List from typing import List
from db_init import Person from lib.db_init import Person
from sqlalchemy import create_engine from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm.session import Session from sqlalchemy.orm.session import Session
from sqlalchemy.sql.expression import desc, func from sqlalchemy.sql.expression import desc, func
engine = create_engine('sqlite:///data.db', echo=False) engine = create_engine('sqlite:///data.db?check_same_thread=False', echo=False)
session_maker = sessionmaker(bind=engine) session_maker = sessionmaker(bind=engine)
def get_session() -> Session: def get_session() -> Session:
@ -29,19 +27,18 @@ def get_session() -> Session:
''' '''
return session_maker() return session_maker()
def get_person(id: int, ses: Session) -> Person: def get_person(ses: Session) -> Person:
'''get person by id '''get random person
Parameters Parameters
---------- ----------
id : person id
ses : session ses : session
Returns Returns
------- -------
res : a person res : a person
''' '''
res = ses.query(Person).filter_by(id=id).first() res = ses.query(Person).order_by(func.random()).first()
return res return res
def get_enemy(P: Person, ses: Session, delta: float = 1.623354290020702) -> Person: def get_enemy(P: Person, ses: Session, delta: float = 1.623354290020702) -> Person:
@ -50,7 +47,7 @@ def get_enemy(P: Person, ses: Session, delta: float = 1.623354290020702) -> Pers
If rate of P (r for short) is within the interval [r1, r2] with 90% certainly: If rate of P (r for short) is within the interval [r1, r2] with 90% certainly:
Assuming that X obeys stand logistic distribution, so the possibility that X is in [-2.94443897916644, Assuming that X obeys stand logistic distribution, so the possibility that X is in [-2.94443897916644,
2.94443897916644] equals 0.9. The standard deviation of F is pi/sqrt(3)*gamma where gamma is shape 2.94443897916644] equals 0.9. The standard deviation of F is pi/sqrt(3)*gamma where gamma is shape
parameter. Assuming variance of r equals expectation mu, we have sqrt(mu) = pi/sqrt(3)*gamma. And x = parameter. Assuming variance of r equals expectation mu, we have sqrt(mu) = pi/sqrt(3)*gamma. And x =
(r - mu) / gamma, so r = x * gamma + mu. Instead x with ±2.94443897916644, gamma = sqrt(3*mu)/pi, we get (r - mu) / gamma, so r = x * gamma + mu. Instead x with ±2.94443897916644, gamma = sqrt(3*mu)/pi, we get
r = ±2.94443897916644 * sqrt(3*mu) / pi + mu = 1.623354290020702 * sqrt(mu) + mu. r = ±2.94443897916644 * sqrt(3*mu) / pi + mu = 1.623354290020702 * sqrt(mu) + mu.
@ -76,5 +73,32 @@ def get_enemy(P: Person, ses: Session, delta: float = 1.623354290020702) -> Pers
return res return res
def get_rank(ses: Session, b: int = 1, len: int = 10) -> List[Person]: def get_rank(ses: Session, b: int = 1, len: int = 10) -> List[Person]:
'''get rank
Parameters
----------
ses : session
b : offset subscript
len : length of list
Returns
-------
res : rank list between b and b+len-1
'''
res = ses.query(Person).order_by(desc('rate')).offset(b-1).limit(len).all() res = ses.query(Person).order_by(desc('rate')).offset(b-1).limit(len).all()
return res return res
def match(ses: Session) -> Person:
'''select two persons
Parameters
----------
ses : session
Returns
-------
: two persons
'''
A = get_person(ses)
B = get_enemy(A, ses)
return A, B

View File

@ -19,7 +19,7 @@ a {
color : #ddd; color : #ddd;
text-decoration: none; text-decoration: none;
} }
button { .bto {
width : 30%; width : 30%;
height : 100%; height : 100%;
border : none; border : none;

View File

@ -39,20 +39,20 @@
<div class="row"> <div class="row">
<div class="col-sm"> <div class="col-sm">
<div style="width: 36%;height: 60%;position: relative;"> <div style="width: 36%;height: 60%;position: relative;">
<img id="P1" alt="Person 1"> <img id="P0" alt="Person 0" src={{ p0src }}>
</div> </div>
<div style="margin: 2% auto;"> <div style="margin: 2% auto;">
姓名<p id="N1"></p><br /> 姓名<br /><p id="N0">{{ p0name }}</p><br />
代表作<p id="M1"></p><br /> 代表作<br /><p id="W0">{{ p0work }}</p><br />
</div> </div>
</div> </div>
<div class="col-sm"> <div class="col-sm">
<div style="width: 36%;height: 60%;position: relative;"> <div style="width: 36%;height: 60%;position: relative;">
<img id="P2" alt="Person 2"> <img id="P1" alt="Person 1" src={{ p1src }}>
</div> </div>
<div style="margin: 2% auto;"> <div style="margin: 2% auto;">
姓名<p id="N2"></p><br /> 姓名<br /><p id="N1">{{ p1name }}</p><br />
代表作<p id="M2"></p><br /> 代表作<br /><p id="W1">{{ p1work }}</p><br />
</div> </div>
</div> </div>
</div> </div>
@ -60,13 +60,13 @@
<div style="height: 7%;width: 75%;margin-top: 2%;"> <div style="height: 7%;width: 75%;margin-top: 2%;">
<div class="row"> <div class="row">
<div class="col-sm"> <div class="col-sm">
<button>Ta</button> <input class="bto" type="button" value="Ta" onclick="window.location.href='/elo?win=0'">
</div> </div>
<div class="col-sm"> <div class="col-sm">
<button>下一个</button> <input class="bto" type="button" value="下一个" onclick="window.location.href='/new'">
</div> </div>
<div class="col-sm"> <div class="col-sm">
<button>Ta</button> <input class="bto" type="button" value="Ta" onclick="window.location.href='/elo?win=1'">
</div> </div>
</div> </div>
</div> </div>
@ -84,18 +84,6 @@
</div> </div>
</div> </div>
</div> </div>
<!-- <div class="footerRow2">
<div class="container">
<div class="row">
<div class="col-sm">
© 2021 ELO Star
</div>
<div class="col-sm">
<p>Made By <a href="https://foolishfox.cn">Fox</a></p>
</div>
</div>
</div>
</div> -->
</footer> </footer>
</body> </body>
</html> </html>