def team_stats(name): # Remove dash in team name for display purposes team_name = name.replace('-', ' ') # Get the TeamList and TotalStats sheets as dataframes team_list = get_sheet_df("TeamList") total_stats = get_sheet_df("TotalStats") # Get the team owner team_owner = team_to_owner(team_name, team_list) # Generate the team roster table team_roster = generate_team_roster_table(team_name, team_list) # Generate the weekly points bar graph for the team breakdown_df = team_players_breakdown_df(team_name, team_list) bar_graph = team_points_stacked_bar_graph(team_name, breakdown_df) # Generate the weekly points cumulative line graph for the team line_graph = team_points_stacked_line_graph(team_name, breakdown_df) # Generate the team roster radar graph radar_graph = team_roster_radar_graph(team_name, team_list, total_stats) return render_template("team-stats.html", team_name=team_name, team_owner=team_owner, team_roster=team_roster, bar_graph=bar_graph, line_graph=line_graph, radar_graph=radar_graph)
def dream_teams(): # Generate current dream team table total_stats = get_sheet_df("TotalStats") current_team = generate_dream_team_table(total_stats) # Get a list of the weeks that we will generate the dream team tables for week_names = [] week_nums = [] for i in range(0, 10): # Get sheet for the week sheet_name = f'Week{i+1}' week_df = get_sheet_df(sheet_name) # Check degeneracy if generate_dream_team_table(week_df) == "": continue else: week_names.append(f"Week{i+1}") week_nums.append(i+1) # Generate tables and store in a list weekly_teams = [] for week_name in week_names: weekly_teams.append(generate_dream_team_table(get_sheet_df(week_name))) return render_template("dream-teams.html", current_team=current_team, week_nums=week_nums, weekly_teams=weekly_teams)
def player_stats(name): # Remove dash in player name for display purposes player_name = name.replace('-', ' ') # Get the TeamList sheet as a dataframe team_list = get_sheet_df('TeamList') # Get the weekly sheets and TotalStats sheet as dataframes weekly_dfs = [get_sheet_df('Week1'), get_sheet_df('Week2'), get_sheet_df('Week3'), get_sheet_df('Week4'), get_sheet_df('Week5'), get_sheet_df('Week6'), get_sheet_df('Week7'), get_sheet_df('Week8'), get_sheet_df('Week9'), get_sheet_df('Week10'), get_sheet_df('TotalStats')] # Get the player points dataframes player_points = player_points_df(player_name, weekly_dfs) # Generate the player points bar graph bar_graph = player_points_bar_graph(player_name, player_points) # Generate the player points cumulative line graph line_graph = player_points_line_graph(player_name, player_points) # Generate the player points radar graph radar_graph = player_points_radar_graph(player_name, player_points) # Generate the picks table and get the total picks picks_table = generate_picks_table(player_name, team_list) total_picks = name_to_picks(player_name, team_list)[1] return render_template("player-stats.html", player_name=player_name, bar_graph=bar_graph, line_graph=line_graph, total_picks=total_picks, radar_graph=radar_graph, picks_table=picks_table)
def team_players_breakdown_df(team_name, team_list_df): """Returns a dataframe giving the weekly breakdown of points for a specific team by player :param team_name The team name to get the dataframe of :param team_list_df The dataframe containing the data needed (in this case we will have team_list_df = get_sheet_df('TeamList')""" # Get list of roles roles = [ 'Batsman 1', 'Batsman 2', 'Batsman 3', 'Batsman 4', 'All-Rounder 1', 'All-Rounder 2', 'All-Rounder 3', 'Wicket-keeper', 'Bowler 1', 'Bowler 2', 'Bowler 3' ] # Get team points breakdown by week team_names = list(team_list_df['Team Name']) row_index = team_names.index(team_name) team_points_breakdown = team_list_df.iloc[[row_index]] # Get player names in the team player_numbers = [int(team_points_breakdown[role]) for role in roles] player_names = numbers_to_names(player_numbers) # Generate dataframe df = pd.DataFrame(zip(roles, player_names), columns=["Role", "Name"]) weeks = [f"Week{i}" for i in range(1, 11)] for week in weeks: week_df = get_sheet_df(week)[["Player Number", "TOTAL"]] points = week_df.set_index( 'Player Number').loc[player_numbers].reset_index() df[week] = points["TOTAL"] return df
def top_n_league_graph(n, league_df): """Returns a line graph with n lines. The graph gives the cumulative weekly points breakdown of the current top n teams in the league table :param n The number of lines the graph will have, corresponding to the current top n teams :param league_df The current league table as a dataframe (obtained from table_data_manager.generate_league_table_df)""" # Get top n teams from league table top_n = list(league_df.head(n)["Team Name"]) # Get the top n teams' weekly points breakdown as a dataframe team_list = get_sheet_df("TeamList") # Remove unecessary trailing zeros and transform data to cumulative data data = [list(team_points_df(y, team_list).iloc[0]) for y in top_n] min_zeros = min([count_trailing_zeros(x) for x in data]) data = [cumulative(x)[:-min_zeros] for x in data] # Style the graph graph = pygal.Line(style=style, margin=35, stroke_style={'width': 3}) graph.title = f"Current Top {n} Tracker" graph.y_title = "Total Points" graph.x_labels = weeks # Add the data to the graph for team, values in zip(top_n, data): graph.add(team, values) # Render the graph graph_data = graph.render_data_uri() return graph_data
def teams(): # Get TeamList dataframe team_list = get_sheet_df('TeamList') # Generate table of all the teams teams_table = generate_teams_table(team_list) return render_template("teams.html", teams_table=teams_table)
def players(): # Get PlayerList Sheet player_list = get_sheet_df('PlayerList') # Generate player list table players_table = generate_players_table(player_list) return render_template("players.html", players_table=players_table)
def home(): # Dataframes table_df = generate_league_table_df() total_stats_df = get_sheet_df('TotalStats') # Generate tables league_table = generate_table(table_df, link_columns=[("Team Name", "teams")]) dream_team = generate_dream_team_table(total_stats_df) # Generate graphs tracker_graph = top_n_league_graph(5, table_df) pie_chart = role_pie_chart(total_stats_df) radar_graph = mvp_radar_graph(total_stats_df) return render_template("index.html", league_table=league_table, dream_team=dream_team, tracker_graph=tracker_graph, role_pie_chart=pie_chart, mvp_radar_graph=radar_graph)
graph_data = graph.render_data_uri() return graph_data def player_points_radar_graph(player_name, player_points_df): """Returns a radar graph giving the breakdown of the given player's points by category :param player_name The player to generate the graph of :param player_points_df The dataframe containing the necessary data (in this case we will obtain in from the player_points_df() function""" # Calculate the total points in each category and store them in a list (which will be of length 4) total_points = [] for i in range(0, 4): category_points = sum(list(player_points_df.iloc[:, i])) total_points.append(category_points) # Generate the radar graph graph = pygal.Radar(style=style, show_legend=False) graph.title = f"{player_name} Points By Category" graph.x_labels = ['Batting', 'Bowling', 'Fielding', 'Bonus'] graph.add(player_name, total_points) graph_data = graph.render_data_uri() return graph_data if __name__ == "__main__": team_name = "The Stoin CC" team_list = get_sheet_df("TeamList") df = team_players_breakdown_df(team_name, team_list) print(df)