-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathepl_table_pred_app.py
More file actions
106 lines (93 loc) · 3.12 KB
/
epl_table_pred_app.py
File metadata and controls
106 lines (93 loc) · 3.12 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import streamlit as st
import requests
import json
import os
# Constants
API_URL = "https://api.football-data.org/v4/competitions/PL/standings"
API_TOKEN = "1ca7105fa42b4e6ba3d6a2e1b9c4d30f" # Replace with your football-data.org API token
PREDICTIONS_FILE = "predictions.json"
TEAMS = [
"Manchester City FC",
"Sunderland AFC",
"Tottenham Hotspur FC",
"Liverpool FC",
"Nottingham Forest FC",
"Arsenal FC",
"Leeds United FC",
"Brighton & Hove Albion FC",
"Fulham FC",
"Aston Villa FC",
"Chelsea FC",
"Crystal Palace FC",
"Newcastle United FC",
"Everton FC",
"Manchester United FC",
"AFC Bournemouth",
"Brentford FC",
"Burnley FC",
"West Ham United FC",
"Wolverhampton Wanderers FC"
]
# Helper functions
def load_predictions():
if os.path.exists(PREDICTIONS_FILE):
with open(PREDICTIONS_FILE, "r") as f:
return json.load(f)
return {}
def save_predictions(predictions):
with open(PREDICTIONS_FILE, "w") as f:
json.dump(predictions, f)
def fetch_current_table():
headers = {"X-Auth-Token": API_TOKEN}
response = requests.get(API_URL, headers=headers)
if response.status_code == 200:
data = response.json()
# Extract team names in order
standings = data["standings"][0]["table"]
return [team["team"]["name"] for team in standings]
else:
st.error("Failed to fetch EPL table. Check your API token.")
return []
def calculate_score(prediction, current_table):
score = 0
for i, team in enumerate(prediction):
if i < len(current_table) and team == current_table[i]:
score += 10
return score
# Streamlit UI
st.title("EPL Table Prediction Game 2025/2026")
st.write("Enter your predicted final EPL table. Scores update as the season progresses!")
# Player name
player = st.text_input("Enter your name:")
# Prediction form
st.write("Rank the teams from 1st to 20th by selecting them in order:")
prediction = st.multiselect(
"Select teams in your predicted order (top to bottom)",
options=TEAMS,
default=[],
key="prediction_multiselect"
)
if st.button("Submit Prediction"):
if player and prediction and len(prediction) == 20 and len(set(prediction)) == 20:
predictions = load_predictions()
predictions[player] = prediction
save_predictions(predictions)
st.success("Prediction saved!")
else:
st.error("Please enter your name and select all 20 teams in your predicted order.")
# Fetch current EPL table
# Calculate Scores button
if st.button("Calculate Scores"):
st.header("Current EPL Table")
current_table = fetch_current_table()
if current_table:
st.write(current_table)
st.header("Leaderboard")
predictions = load_predictions()
leaderboard = []
for player_name, pred in predictions.items():
score = calculate_score(pred, current_table)
leaderboard.append((player_name, score))
leaderboard.sort(key=lambda x: x[1], reverse=True)
st.table(leaderboard)
st.caption("10 points for each team in the correct position. Scores update as the table updates.")