Skip to content

Commit 658df29

Browse files
authored
Dev (#1)
Added basic functionality and demo flask app.
1 parent b602e2a commit 658df29

9 files changed

+158
-0
lines changed

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1+
venv/
2+
.env
3+
4+
*.pyc
5+
__pycache__/
6+
7+
instance/
8+
19
config.yml

Procfile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: gunicorn checkmymate:'create_app()'

clarence/__init__.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import os
2+
from flask import Flask
3+
4+
def create_app():
5+
6+
# create app
7+
app = Flask(__name__)
8+
app.config.from_mapping(
9+
SECRET_KEY = os.environ.get('SECRET_KEY') or 'dev_key')
10+
11+
# register app
12+
from . import checkmymate
13+
app.register_blueprint(checkmymate.bp)
14+
15+
# return
16+
return app

clarence/checkmymate.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from flask import Flask, render_template, request
2+
app = Flask(__name__)
3+
4+
@app.route("/", methods=['GET', 'POST'])
5+
def index():
6+
if request.method == 'POST':
7+
if request.form.get('action1') == 'VALUE1':
8+
pass # do something
9+
elif request.form.get('action2') == 'VALUE2':
10+
pass # do something else
11+
else:
12+
pass # unknown
13+
elif request.method == 'GET':
14+
return render_template('index.html', form=form)
15+
16+
return render_template("index.html")

clarence/templates/base.html

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<title>{% block title %}{% endblock %} - CheckMyMate</title>
2+
<!-- Fonts -->
3+
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css"
4+
rel='stylesheet' type='text/css' />
5+
<!-- Bootstrap CSS -->
6+
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
7+
rel="stylesheet" />
8+
9+
<section class="content">
10+
<div class="container">
11+
<header>
12+
{% block header %}{% endblock %}
13+
</header>
14+
{% for message in get_flashed_messages() %}
15+
<div class="alert alert-danger">
16+
<p class="lead">{{ message }}</p>
17+
</div>
18+
{% endfor %}
19+
{% block content %}{% endblock %}
20+
</div>
21+
</section>
22+
23+
<!-- Bootstrap related JavaScript -->
24+
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
25+
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
26+
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>

clarence/templates/index.html

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<h3>Our Flask Buttons</h3>
2+
<form method="post" action="/">
3+
<input type="submit" value="VALUE1" name="action1"/>
4+
<input type="submit" value="VALUE2" name="action2" />
5+
</form>

main.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# NOTES:
2+
# it might be easier to write my own parser. python-lichess does it themselves, using these lines of code:
3+
# https://github.com/cyanfish/python-lichess/blob/8deea5dda8965b90915c744d65f2a4bc3ee085e5/lichess/api.py#L293
4+
# https://github.com/cyanfish/python-lichess/blob/8deea5dda8965b90915c744d65f2a4bc3ee085e5/lichess/api.py#L89
5+
# there's something with specifying the object_type=lichess.format.GAME_STREAM_OBJECT and passing it to the parser
6+
7+
import pandas as pd
8+
import lichess.api
9+
import requests
10+
11+
# we want to create a table for user's games, that stores: color, points-earned, opening
12+
def process_game_dict(game_dict, username):
13+
"""
14+
Processes a single game dictionary, as it comes from the result of lichess.api.user_games().
15+
"""
16+
opening_name = game_dict["opening"]["name"]
17+
opening_name_simple = opening_name.split(':')[0]
18+
opening_code = game_dict["opening"]["eco"]
19+
color = "white" if game_dict["players"]["white"]["user"]["id"]==username else "black"
20+
if "winner" not in game_dict:
21+
points = .5
22+
elif game_dict["winner"]==color:
23+
points = 1
24+
else:
25+
points = 0
26+
return {"opening_name": opening_name,
27+
"opening_name_simple": opening_name_simple,
28+
"opening_code": opening_code,
29+
"color": color,
30+
"points": points}
31+
32+
# load the games
33+
user = 'madmaxmatze'
34+
query = {
35+
"opening": True,
36+
"moves": False,
37+
"sort": "dateDesc",
38+
# "max": 300
39+
}
40+
games = [process_game_dict(g, user) for g in lichess.api.user_games(user, **query)]
41+
42+
# test
43+
games = pd.DataFrame(games)
44+
print(games.head(5))
45+
for color in ["white", "black"]:
46+
display_games = \
47+
(games
48+
[games["color"]==color]
49+
.groupby("opening_name_simple")
50+
["points"]
51+
.agg(["count", "mean"])
52+
.sort_values("count", ascending=False)
53+
.rename({"opening_name_simple": "opening", "count": "num_games", "mean": "avg_points_per_game"})
54+
.head(10))
55+
print(display_games)

requirements.txt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
certifi==2021.10.8
2+
charset-normalizer==2.0.10
3+
click==8.0.3
4+
Flask==2.0.2
5+
gunicorn==20.1.0
6+
idna==3.3
7+
importlib-metadata==4.10.0
8+
itsdangerous==2.0.1
9+
Jinja2==3.0.3
10+
MarkupSafe==2.0.1
11+
numpy==1.21.5
12+
pandas==1.3.5
13+
python-dateutil==2.8.2
14+
python-dotenv==0.19.2
15+
python-lichess==0.10
16+
pytz==2021.3
17+
requests==2.27.1
18+
six==1.16.0
19+
typing-extensions==4.0.1
20+
urllib3==1.26.8
21+
Werkzeug==2.0.2
22+
zipp==3.7.0

server.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from flask import Flask
2+
app = Flask(__name__)
3+
4+
@app.route('/')
5+
def hello_world():
6+
return 'Hello World!'
7+
8+
if __name__ == '__main__':
9+
app.run()

0 commit comments

Comments
 (0)