-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
80 lines (66 loc) · 2.39 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from fastapi import FastAPI, HTTPException
from fastapi.responses import HTMLResponse
from typing import List, Dict
app = FastAPI()
app = FastAPI()
# Placeholder for rankings data (would typically be retrieved from a database or an API)
rankings_data = {
"current": [
{"nation": "Spain", "points": 1000},
{"nation": "Germany", "points": 950},
{"nation": "France", "points": 925},
{"nation": "England", "points": 900},
],
"previous": [
{"nation": "Spain", "points": 1000},
{"nation": "Germany", "points": 950},
{"nation": "France", "points": 920},
{"nation": "England", "points": 905},
]
}
@app.get("/rankings", response_model=List[Dict])
def get_current_rankings():
"""
Fetch current national rankings of European football nations.
"""
try:
return sorted(rankings_data["current"], key=lambda x: x['points'], reverse=True)
except Exception:
raise HTTPException(status_code=500, detail="Rankings data is unavailable.")
@app.get("/rankings/changes", response_model=List[Dict])
def get_ranking_changes():
"""
Compare and show changes in rankings after a round of games.
"""
try:
previous_rankings = rankings_data["previous"]
current_rankings = rankings_data["current"]
# Generate a list of rankings changes
changes = []
for i, current in enumerate(current_rankings):
previous = next((team for team in previous_rankings if team['nation'] == current['nation']), None)
if previous:
change = current['points'] - previous['points']
changes.append({
"nation": current['nation'],
"previous_points": previous['points'],
"current_points": current['points'],
"change": change,
"arrow": "up" if change > 0 else "down" if change < 0 else "no change"
})
return changes
except Exception:
raise HTTPException(status_code=500, detail="Error retrieving ranking changes.")
@app.get('/', response_class=HTMLResponse)
def hello_world():
html = (
"<html>\n"
"<body>\n"
" <b>Hello,World</b>\n"
"</body>\n"
"</html>\n"
)
return HTMLResponse(content=html, status_code=200)
if __name__ == '__main__':
import uvicorn
uvicorn.run(app)