Spaces:
Runtime error
Runtime error
| import pandas as pd | |
| import streamlit as st | |
| # Set page title and favicon | |
| st.set_page_config(page_icon=":soccer:",layout="wide") | |
| st.markdown( | |
| """ | |
| <style> | |
| .block-container { | |
| padding-top: 1rem; | |
| } | |
| #MainMenu {visibility: hidden;} | |
| </style> | |
| """, | |
| unsafe_allow_html=True | |
| ) | |
| # Set title and create a new tab for league history | |
| st.title("⚽ SoccerTwos Challenge Analytics Extra!⚽ ") | |
| tab_team, tab_owners = st.tabs(["Form Table", "Games by Author",]) | |
| # Match Results | |
| MATCH_RESULTS_URL = "https://huggingface.co/datasets/huggingface-projects/bot-fight-data/raw/main/soccer_history.csv" | |
| def fetch_match_history(): | |
| """ | |
| Fetch the match results from the last 24 hours. | |
| Cache the result for 30min to avoid unnecessary requests. | |
| Return a DataFrame. | |
| """ | |
| df = pd.read_csv(MATCH_RESULTS_URL) | |
| df["timestamp"] = pd.to_datetime(df.timestamp, unit="s") | |
| df = df[df["timestamp"] >= pd.Timestamp.now() - pd.Timedelta(hours=24)] | |
| df.columns = ["home", "away", "timestamp", "result"] | |
| return df | |
| match_df = fetch_match_history() | |
| # Define a function to calculate the total number of matches played | |
| def num_matches_played(): | |
| return match_df.shape[0] | |
| # Get a list of all teams that have played in the last 24 hours | |
| teams = sorted( | |
| list(pd.concat([match_df["home"], match_df["away"]]).unique()), key=str.casefold | |
| ) | |
| # Create the form table, which shows the win percentage for each team | |
| # st.header("Form Table") | |
| team_results = {} | |
| for i, row in match_df.iterrows(): | |
| home_team = row["home"] | |
| away_team = row["away"] | |
| result = row["result"] | |
| if home_team not in team_results: | |
| team_results[home_team] = [0, 0, 0] | |
| if away_team not in team_results: | |
| team_results[away_team] = [0, 0, 0] | |
| if result == 0: | |
| team_results[home_team][2] += 1 | |
| team_results[away_team][0] += 1 | |
| elif result == 1: | |
| team_results[home_team][0] += 1 | |
| team_results[away_team][2] += 1 | |
| else: | |
| team_results[home_team][1] += 1 | |
| team_results[away_team][1] += 1 | |
| # Create a DataFrame from the results dictionary and calculate the win percentage | |
| df = pd.DataFrame.from_dict( | |
| team_results, orient="index", columns=["wins", "draws", "losses"] | |
| ).sort_index() | |
| df[["owner", "team"]] = df.index.to_series().str.split("/", expand=True) | |
| df = df[["owner", "team", "wins", "draws", "losses"]] | |
| df["win_pct"] = (df["wins"] / (df["wins"] + df["draws"] + df["losses"])) * 100 | |
| # Get a list of all teams that have played in the last 24 hours | |
| def fetch_owners(): | |
| """ | |
| Fetch a list of all owners who have played in the matches, along with the number of teams they own | |
| and the number of unique teams they played with. | |
| """ | |
| # Extract the owner name and team name from each home and away team name in the DataFrame | |
| team_owners = match_df["home"].apply(lambda x: x.split('/')[0]).tolist() + match_df['away'].apply(lambda x: x.split('/')[0]).tolist() | |
| teams = match_df["home"].apply(lambda x: x.split('/')[1]).tolist() + match_df['away'].apply(lambda x: x.split('/')[1]).tolist() | |
| # Count the number of games played by each owner and the number of unique teams they played with | |
| owner_team_counts = {} | |
| owner_team_set = {} | |
| for i, team_owner in enumerate(team_owners): | |
| owner = team_owner.split(' ')[0] | |
| if owner not in owner_team_counts: | |
| owner_team_counts[owner] = 1 | |
| owner_team_set[owner] = {teams[i]} | |
| else: | |
| owner_team_counts[owner] += 1 | |
| owner_team_set[owner].add(teams[i]) | |
| # Create a DataFrame from the dictionary | |
| owners_df = pd.DataFrame.from_dict(owner_team_counts, orient="index", columns=["Games played by owner"]) | |
| owners_df["Unique teams by owner"] = owners_df.index.map(lambda x: len(owner_team_set[x])) | |
| # Return the DataFrame | |
| return owners_df | |
| # Display the DataFrame as a table, sorted by win percentage | |
| with tab_team: | |
| st.write("Form Table for previous 24 hours, ranked by win percentage") | |
| stats = df.sort_values(by="win_pct", ascending=False) | |
| styled_stats = stats.style.set_table_attributes("style='font-size: 20px'").set_table_styles([dict(selector='th', props=[('max-width', '200px')])]) | |
| styled_stats = styled_stats.set_table_attributes("style='max-height: 1200px; overflow: auto'") | |
| st.dataframe(styled_stats) | |
| # Create a DataFrame from the list of owners and their number of teams | |
| owners_df = fetch_owners() | |
| # Display the DataFrame as a table | |
| with tab_owners: | |
| st.dataframe(owners_df) | |