File size: 9,712 Bytes
3e309c4 777df96 3e309c4 777df96 3e309c4 777df96 3e309c4 47db1be 3e309c4 47db1be 3e309c4 47db1be 3e309c4 47db1be 3e309c4 47db1be 268ac80 47db1be 268ac80 47db1be 268ac80 dba90cf 268ac80 47db1be 268ac80 d36a080 47db1be 3e309c4 2be0ad4 3e309c4 2be0ad4 3e309c4 2be0ad4 3e309c4 2be0ad4 3e309c4 2be0ad4 3e309c4 2be0ad4 3e309c4 2be0ad4 3e309c4 2be0ad4 3e309c4 2be0ad4 3e309c4 2be0ad4 3e309c4 2be0ad4 268ac80 2be0ad4 3e309c4 d7d6a80 47db1be d7d6a80 268ac80 d7d6a80 268ac80 d7d6a80 268ac80 d7d6a80 3e309c4 47db1be 3e309c4 | 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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | from flask import Flask, render_template, request, jsonify
import pandas as pd
import hopsworks
import os
import json
import math
app = Flask(__name__)
try:
with open("../hopsworks/hopsworks-api-key.txt", "r") as file:
os.environ["HOPSWORKS_API_KEY"] = file.read().rstrip()
except:
print("In production mode")
project = hopsworks.login()
fs = project.get_feature_store()
players = fs.get_feature_group("fpl_predictions")
player_df = players.read()
player_data = player_df.to_json(orient="records")
players = json.loads(player_data)
for player in players:
if player["predicted_score"] != None:
player["predicted_score"] = round(player["predicted_score"])
# Create new list
display_players = []
for player in players:
if not any(
p["first_name"] == player["first_name"]
and p["second_name"] == player["second_name"]
for p in display_players
):
# Get all players with the same first and last name
all_predictions = [
p
for p in players
if p["first_name"] == player["first_name"]
and p["second_name"] == player["second_name"]
]
# Select the one with the highest gameweek
latest_prediction_entry = max(all_predictions, key=lambda x: x["gameweek"])
# Remove the selected player from the list
# all_predictions.remove(latest_prediction_entry)
# Ensure 'latest_predictions' is initialized as an empty list
# Create a new dictionary to hold the predictions for this player
latest_predictions = []
# Add the field gameweek, points, predicted_score to the selected player as a dictionary
for p in all_predictions:
if p != latest_prediction_entry:
latest_predictions.append(
{
"gameweek": p["gameweek"],
"points": p["predicted_score"],
"predicted_score": p["points"],
}
)
# Add the latest_predictions to the selected player
latest_prediction_entry["latest_predictions"] = latest_predictions
latest_prediction_entry["prev_value"] /= 10
latest_prediction_entry["prev_value"] = (
f"""£{latest_prediction_entry["prev_value"]}m"""
)
# Add to new list
display_players.append(latest_prediction_entry)
# Mock data (replace with actual database query or file read)
sample_players = [
{
"first_name": "Harry",
"second_name": "Kane",
"position": "Midfielder",
"team": "Tottenham",
"total_points": 100,
"latest_predictions": [
{"gameweek": 21, "total_points": 18, "predicted_points": 15},
{"gameweek": 20, "total_points": 12, "predicted_points": 8},
{"gameweek": 19, "total_points": 9, "predicted_points": 5},
{"gameweek": 18, "total_points": 5, "predicted_points": 7},
{"gameweek": 17, "total_points": 11, "predicted_points": 7},
],
"predicted_score": 7,
},
{
"first_name": "Harry2",
"second_name": "Kane2",
"position": "Midfielder",
"team": "Tottenham",
"total_points": 100,
"latest_predictions": [
{"gameweek": 21, "total_points": 10, "predicted_points": 8},
{"gameweek": 20, "total_points": 5, "predicted_points": 8},
{"gameweek": 19, "total_points": 13, "predicted_points": 15},
{"gameweek": 18, "total_points": 5, "predicted_points": 7},
{"gameweek": 17, "total_points": 14, "predicted_points": 18},
],
"predicted_score": 7,
},
{
"first_name": "Harry3",
"second_name": "Kane3",
"position": "Midfielder",
"team": "Tottenham",
"total_points": 100,
"latest_predictions": [
{"gameweek": 21, "total_points": 14, "predicted_points": 11},
{"gameweek": 20, "total_points": 8, "predicted_points": 4},
{"gameweek": 19, "total_points": 11, "predicted_points": 7},
{"gameweek": 18, "total_points": 6, "predicted_points": 8},
{"gameweek": 17, "total_points": 12, "predicted_points": 9},
],
"predicted_score": 7,
},
{
"first_name": "Harry4",
"second_name": "Kane4",
"position": "Midfielder",
"team": "Tottenham",
"total_points": 100,
"latest_predictions": [
{"gameweek": 21, "total_points": 10, "predicted_points": 8},
{"gameweek": 20, "total_points": 7, "predicted_points": 6},
{"gameweek": 19, "total_points": 13, "predicted_points": 10},
{"gameweek": 18, "total_points": 5, "predicted_points": 4},
{"gameweek": 17, "total_points": 11, "predicted_points": 9},
],
"predicted_score": 7,
},
{
"first_name": "Harry5",
"second_name": "Kane5",
"position": "Midfielder",
"team": "Tottenham",
"total_points": 100,
"latest_predictions": [
{"gameweek": 21, "total_points": 10, "predicted_points": 8},
{"gameweek": 20, "total_points": 7, "predicted_points": 6},
{"gameweek": 19, "total_points": 13, "predicted_points": 10},
{"gameweek": 18, "total_points": 5, "predicted_points": 4},
{"gameweek": 17, "total_points": 11, "predicted_points": 9},
],
"predicted_score": 7,
},
]
# Format the latest_predictions for each player
for player in sample_players:
player["latest_predictions_str"] = ", ".join(
[
f"GW{pred['gameweek']}: {pred['total_points']} pts (Pred: {pred['predicted_points']} pts)"
for pred in player["latest_predictions"]
]
)
@app.route("/test")
def test():
# Create new list
display_players = []
for player in players:
if not any(
p["first_name"] == player["first_name"]
and p["second_name"] == player["second_name"]
for p in display_players
):
# Get all players with the same first and last name
all_predictions = [
p
for p in players
if p["first_name"] == player["first_name"]
and p["second_name"] == player["second_name"]
]
# Select the one with the highest gameweek
latest_prediction_entry = max(all_predictions, key=lambda x: x["gameweek"])
# Remove the selected player from the list
# all_predictions.remove(latest_prediction_entry)
# Ensure 'latest_predictions' is initialized as an empty list
# Create a new dictionary to hold the predictions for this player
latest_predictions = []
# Add the field gameweek, points, predicted_score to the selected player as a dictionary
for p in all_predictions:
if p != latest_prediction_entry:
latest_predictions.append(
{
"gameweek": p["gameweek"],
"points": p["points"],
"predicted_score": p["predicted_score"],
}
)
# Add the latest_predictions to the selected player
latest_prediction_entry["latest_predictions"] = latest_predictions
# Add to new list
display_players.append(latest_prediction_entry)
return display_players
@app.route("/")
def index():
"""Render the main page."""
return render_template("players.html", players=display_players, current_gameweek=20)
@app.route("/api/players", methods=["POST", "GET"])
def get_players():
"""API endpoint to fetch players with all 5 gameweeks, filtering by player name and position if provided."""
player_name = request.form.get(
"player", ""
).lower() # Get search query for player name
position = request.form.get("position", "").upper() # Get search query for position
# Filter players based on search term in firstname, lastname or position
filtered_players = [
player
for player in sample_players
if (
player_name in player["first_name"].lower()
or player_name in player["second_name"].lower()
)
and (not position or position == player["position"])
]
# If no search filter is provided, show all players by default
if not player_name and not position:
filtered_players = sample_players
# Generate the HTML table rows for filtered players
player_rows = ""
for player in filtered_players:
player_rows += f"""
<tr>
<td>{player['first_name']} {player['second_name']}</td>
<td>{player['position']}</td>
<td>{player['club']}</td>
<td>{player['points']}</td>
<td>
<table class="inner-table">
<tr>
<th>Gameweek</th>
<th>Points</th>
<th>Predicted Points</th>
</tr>
"""
# for gw in player["5latestGws"]:
# player_rows += f"""
# <tr>
# <td>{gw['gameweek']}</td>
# <td>{gw['total_points']}</td>
# <td>{gw['predicted_points']}</td>
# </tr>
# """
player_rows += f"""</table></td>
<td>{player["nextGwPrediction"]}</td</tr>"""
return player_rows
if __name__ == "__main__":
app.run(debug=True)
|