hectorjelly commited on
Commit
5f0046c
1 Parent(s): f3b2737

first commit

Browse files
Files changed (2) hide show
  1. app.py +141 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import streamlit as st
3
+
4
+ # Set page title and favicon
5
+ st.set_page_config(page_icon=":soccer:",layout="wide")
6
+
7
+
8
+ st.markdown(
9
+ """
10
+ <style>
11
+ .block-container {
12
+ padding-top: 1rem;
13
+ }
14
+ #MainMenu {visibility: hidden;}
15
+ </style>
16
+ """,
17
+ unsafe_allow_html=True
18
+ )
19
+
20
+ # Set title and create a new tab for league history
21
+ st.title("⚽ SoccerTwos Challenge Analytics Extra!⚽ ")
22
+ tab_team, tab_owners = st.tabs(["Form Table", "Games by Author",])
23
+
24
+ # Match Results
25
+ MATCH_RESULTS_URL = "https://huggingface.co/datasets/huggingface-projects/bot-fight-data/raw/main/soccer_history.csv"
26
+
27
+
28
+ @st.cache(ttl=1800)
29
+ def fetch_match_history():
30
+ """
31
+ Fetch the match results from the last 24 hours.
32
+ Cache the result for 30min to avoid unnecessary requests.
33
+ Return a DataFrame.
34
+ """
35
+ df = pd.read_csv(MATCH_RESULTS_URL)
36
+ df["timestamp"] = pd.to_datetime(df.timestamp, unit="s")
37
+ df = df[df["timestamp"] >= pd.Timestamp.now() - pd.Timedelta(hours=24)]
38
+ df.columns = ["home", "away", "timestamp", "result"]
39
+ return df
40
+
41
+
42
+ match_df = fetch_match_history()
43
+
44
+ # Define a function to calculate the total number of matches played
45
+ def num_matches_played():
46
+ return match_df.shape[0]
47
+
48
+ # Get a list of all teams that have played in the last 24 hours
49
+ teams = sorted(
50
+ list(pd.concat([match_df["home"], match_df["away"]]).unique()), key=str.casefold
51
+ )
52
+
53
+ # Create the form table, which shows the win percentage for each team
54
+ # st.header("Form Table")
55
+ team_results = {}
56
+ for i, row in match_df.iterrows():
57
+ home_team = row["home"]
58
+ away_team = row["away"]
59
+ result = row["result"]
60
+
61
+ if home_team not in team_results:
62
+ team_results[home_team] = [0, 0, 0]
63
+
64
+ if away_team not in team_results:
65
+ team_results[away_team] = [0, 0, 0]
66
+
67
+ if result == 0:
68
+ team_results[home_team][2] += 1
69
+ team_results[away_team][0] += 1
70
+ elif result == 1:
71
+ team_results[home_team][0] += 1
72
+ team_results[away_team][2] += 1
73
+ else:
74
+ team_results[home_team][1] += 1
75
+ team_results[away_team][1] += 1
76
+
77
+ # Create a DataFrame from the results dictionary and calculate the win percentage
78
+ df = pd.DataFrame.from_dict(
79
+ team_results, orient="index", columns=["wins", "draws", "losses"]
80
+ ).sort_index()
81
+ df[["owner", "team"]] = df.index.to_series().str.split("/", expand=True)
82
+ df = df[["owner", "team", "wins", "draws", "losses"]]
83
+ df["win_pct"] = (df["wins"] / (df["wins"] + df["draws"] + df["losses"])) * 100
84
+
85
+
86
+ # Get a list of all teams that have played in the last 24 hours
87
+
88
+
89
+ @st.cache_data(ttl=1800)
90
+ def fetch_owners():
91
+ """
92
+ Fetch a list of all owners who have played in the matches, along with the number of teams they own
93
+ and the number of unique teams they played with.
94
+ """
95
+ # Extract the owner name and team name from each home and away team name in the DataFrame
96
+ team_owners = match_df["home"].apply(lambda x: x.split('/')[0]).tolist() + match_df['away'].apply(lambda x: x.split('/')[0]).tolist()
97
+ teams = match_df["home"].apply(lambda x: x.split('/')[1]).tolist() + match_df['away'].apply(lambda x: x.split('/')[1]).tolist()
98
+
99
+ # Count the number of games played by each owner and the number of unique teams they played with
100
+ owner_team_counts = {}
101
+ owner_team_set = {}
102
+ for i, team_owner in enumerate(team_owners):
103
+ owner = team_owner.split(' ')[0]
104
+ if owner not in owner_team_counts:
105
+ owner_team_counts[owner] = 1
106
+ owner_team_set[owner] = {teams[i]}
107
+ else:
108
+ owner_team_counts[owner] += 1
109
+ owner_team_set[owner].add(teams[i])
110
+
111
+ # Create a DataFrame from the dictionary
112
+ owners_df = pd.DataFrame.from_dict(owner_team_counts, orient="index", columns=["Games played by owner"])
113
+ owners_df["Unique teams by owner"] = owners_df.index.map(lambda x: len(owner_team_set[x]))
114
+
115
+ # Return the DataFrame
116
+ return owners_df
117
+
118
+
119
+
120
+
121
+
122
+
123
+ # Display the DataFrame as a table, sorted by win percentage
124
+ with tab_team:
125
+ st.write("Form Table for previous 24 hours, ranked by win percentage")
126
+ stats = df.sort_values(by="win_pct", ascending=False)
127
+ styled_stats = stats.style.set_table_attributes("style='font-size: 20px'").set_table_styles([dict(selector='th', props=[('max-width', '200px')])])
128
+ styled_stats = styled_stats.set_table_attributes("style='max-height: 1200px; overflow: auto'")
129
+ st.dataframe(styled_stats)
130
+
131
+
132
+ # Create a DataFrame from the list of owners and their number of teams
133
+ owners_df = fetch_owners()
134
+
135
+ # Display the DataFrame as a table
136
+ with tab_owners:
137
+
138
+ st.dataframe(owners_df)
139
+
140
+
141
+
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ streamlit
2
+ pandas