Download app.py from ID2223JR/fpl_app: direct link, hf CLI and curl.
- Browser
- Download file 9.71 kB
-
https://huggingface.co/spaces/ID2223JR/fpl_app/resolve/main/app.py
- Command line
-
hf download hf://spaces/ID2223JR/fpl_app/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ID2223JR/fpl_app/resolve/main/app.py
9.71 kB
| 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"] | |
| ] | |
| ) | |
| 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 | |
| def index(): | |
| """Render the main page.""" | |
| return render_template("players.html", players=display_players, current_gameweek=20) | |
| 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) | |