hectorjelly commited on
Commit
abc4c8c
·
1 Parent(s): 75d0ed2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -136
app.py CHANGED
@@ -1,10 +1,7 @@
1
  import datetime
2
-
3
  import pandas as pd
4
  import streamlit as st
5
  import timeago
6
- import plotly.graph_objects as go
7
-
8
 
9
  st.set_page_config(layout="wide")
10
  st.markdown(
@@ -19,41 +16,41 @@ st.markdown(
19
  unsafe_allow_html=True
20
  )
21
 
22
- now = datetime.datetime.now()
 
 
 
 
23
  MATCH_RESULTS_URL = "https://huggingface.co/datasets/huggingface-projects/bot-fight-data/raw/main/soccer_history.csv"
24
 
25
 
26
  @st.cache_data(ttl=1800)
27
  def fetch_match_history():
28
  """
29
- Fetch match history.
30
  Cache the result for 30min to avoid unnecessary requests.
31
  Return a DataFrame.
32
  """
33
  df = pd.read_csv(MATCH_RESULTS_URL)
34
  df["timestamp"] = pd.to_datetime(df.timestamp, unit="s")
 
35
  df.columns = ["home", "away", "timestamp", "result"]
36
  return df
37
 
38
 
39
- def days_left():
40
- end_date = datetime.date(2023, 4, 30)
41
- today = datetime.date.today()
42
- time_until_date = end_date - today
43
- return time_until_date.days
44
-
45
 
 
46
  def num_matches_played():
47
  return match_df.shape[0]
48
 
49
-
50
- match_df = fetch_match_history()
51
  teams = sorted(
52
  list(pd.concat([match_df["home"], match_df["away"]]).unique()), key=str.casefold
53
  )
54
 
55
- st.title("🤗 SoccerTwos Challenge Analytics")
56
-
57
  team_results = {}
58
  for i, row in match_df.iterrows():
59
  home_team = row["home"]
@@ -76,7 +73,7 @@ for i, row in match_df.iterrows():
76
  team_results[home_team][1] += 1
77
  team_results[away_team][1] += 1
78
 
79
-
80
  df = pd.DataFrame.from_dict(
81
  team_results, orient="index", columns=["wins", "draws", "losses"]
82
  ).sort_index()
@@ -84,123 +81,10 @@ df[["owner", "team"]] = df.index.to_series().str.split("/", expand=True)
84
  df = df[["owner", "team", "wins", "draws", "losses"]]
85
  df["win_pct"] = (df["wins"] / (df["wins"] + df["draws"] + df["losses"])) * 100
86
 
87
- stats = df
88
-
89
- tab_team, tab_competition = st.tabs(["Results", "Competition stats"])
90
-
91
-
92
- def get_text_result(row, team_name):
93
- if row["home"] == team_name:
94
- if row["result"] == 1:
95
- return "Win"
96
- elif row["result"] == 0.5:
97
- return "Draw"
98
- else:
99
- return "Loss"
100
- elif row["away"] == team_name:
101
- if row["result"] == 0:
102
- return "Win"
103
- elif row["result"] == 0.5:
104
- return "Draw"
105
- else:
106
- return "Loss"
107
-
108
-
109
- with tab_team:
110
- team = st.selectbox("Team", teams)
111
-
112
- col1, col2 = st.columns(2)
113
-
114
- with col1:
115
- c1, c2, c3 = st.columns(3)
116
- with c1:
117
- st.metric("Wins", f"{stats.loc[[team]]['wins'][0]}")
118
- with c2:
119
- st.metric("Draws", f"{stats.loc[[team]]['draws'][0]}")
120
- with c3:
121
- st.metric("Losses", f"{stats.loc[[team]]['losses'][0]}")
122
-
123
- st.write("Results")
124
- res_df = match_df[(match_df["home"] == team) | (match_df["away"] == team)]
125
- res_df["result"] = res_df.apply(lambda row: get_text_result(row, team), axis=1)
126
- opponent_column = res_df.apply(
127
- lambda row: row["away"] if row["home"] == team else row["home"], axis=1
128
- )
129
- res_df["vs"] = opponent_column
130
- result_column = res_df["result"]
131
- new_df = pd.concat([opponent_column, result_column], axis=1)
132
- new_df.columns = ["vs", "result"]
133
- res_df[["owner", "team"]] = res_df["vs"].str.split("/", expand=True)
134
- res_df["played"] = res_df["timestamp"].apply(lambda x: timeago.format(x, now))
135
- res_df.sort_values(by=["timestamp"], ascending=True, inplace=True)
136
- disp_res_df = res_df.drop(["home", "away", "vs", "timestamp"], axis=1)
137
-
138
- def highlight_results(s):
139
- colour = {
140
- "Win": "LightGreen",
141
- "Draw": "LightYellow",
142
- "Loss": "LightSalmon",
143
- }
144
- return [f"background-color: {colour[s.result]}"] * len(s)
145
-
146
- # Create a friendly index.
147
- disp_res_df.reset_index(inplace=True, drop=True)
148
- disp_res_df.index += 1
149
- disp_res_df = disp_res_df.iloc[::-1]
150
-
151
- # Display the table.
152
- st.dataframe(disp_res_df.style.apply(highlight_results, axis=1))
153
-
154
- with col2:
155
- c1, c2 = st.columns(2)
156
- with c1:
157
- st.metric("Win rate", f"{stats.loc[[team]]['win_pct'][0]:.2f}%")
158
-
159
- joined = res_df["timestamp"].min()
160
- with c2:
161
- st.metric("Competing since", f"{timeago.format(joined, now)}")
162
-
163
- grouped = (
164
- res_df.groupby([res_df["timestamp"].dt.date, "result"])
165
- .size()
166
- .reset_index(name="count")
167
- )
168
-
169
- loss_trace = go.Bar(
170
- x=grouped.loc[grouped["result"] == "Loss", "timestamp"],
171
- y=grouped.loc[grouped["result"] == "Loss", "count"],
172
- name="Losses",
173
- marker=dict(color="red"),
174
- )
175
- draw_trace = go.Bar(
176
- x=grouped.loc[grouped["result"] == "Draw", "timestamp"],
177
- y=grouped.loc[grouped["result"] == "Draw", "count"],
178
- name="Draws",
179
- marker=dict(color="orange"),
180
- )
181
- win_trace = go.Bar(
182
- x=grouped.loc[grouped["result"] == "Win", "timestamp"],
183
- y=grouped.loc[grouped["result"] == "Win", "count"],
184
- name="Wins",
185
- marker=dict(color="green"),
186
- )
187
-
188
- fig = go.Figure(data=[loss_trace, draw_trace, win_trace])
189
- fig.update_layout(barmode="stack")
190
- st.plotly_chart(fig)
191
-
192
-
193
- with tab_competition:
194
- col1, col2, col3 = st.columns(3)
195
-
196
- col1.metric("Matches played", f"{num_matches_played():,d}")
197
- col2.metric("Live models", f"{len(teams)}")
198
- col3.metric("Season ends in", f"{days_left()} days")
199
-
200
- match_counts = (
201
- match_df.groupby(match_df["timestamp"].dt.date).size().reset_index(name="count")
202
- )
203
- match_counts["matches_played"] = match_counts["count"].cumsum()
204
-
205
- st.title("Matches played")
206
- st.area_chart(match_counts.set_index("timestamp")["matches_played"])
 
1
  import datetime
 
2
  import pandas as pd
3
  import streamlit as st
4
  import timeago
 
 
5
 
6
  st.set_page_config(layout="wide")
7
  st.markdown(
 
16
  unsafe_allow_html=True
17
  )
18
 
19
+ # Set title and create a new tab for league history
20
+ st.title("SoccerTwos Challenge Form Table! - Only last 24hours of games considered")
21
+ tab_team, tab_history = st.tabs(["Form Table", "League History Overtimer"])
22
+
23
+ # Fetch the match results from the last 24 hours
24
  MATCH_RESULTS_URL = "https://huggingface.co/datasets/huggingface-projects/bot-fight-data/raw/main/soccer_history.csv"
25
 
26
 
27
  @st.cache_data(ttl=1800)
28
  def fetch_match_history():
29
  """
30
+ Fetch the match results from the last 24 hours.
31
  Cache the result for 30min to avoid unnecessary requests.
32
  Return a DataFrame.
33
  """
34
  df = pd.read_csv(MATCH_RESULTS_URL)
35
  df["timestamp"] = pd.to_datetime(df.timestamp, unit="s")
36
+ df = df[df["timestamp"] >= pd.Timestamp.now() - pd.Timedelta(hours=24)]
37
  df.columns = ["home", "away", "timestamp", "result"]
38
  return df
39
 
40
 
41
+ match_df = fetch_match_history()
 
 
 
 
 
42
 
43
+ # Define a function to calculate the total number of matches played
44
  def num_matches_played():
45
  return match_df.shape[0]
46
 
47
+ # Get a list of all teams that have played in the last 24 hours
 
48
  teams = sorted(
49
  list(pd.concat([match_df["home"], match_df["away"]]).unique()), key=str.casefold
50
  )
51
 
52
+ # Create the form table, which shows the win percentage for each team
53
+ st.header("Form Table")
54
  team_results = {}
55
  for i, row in match_df.iterrows():
56
  home_team = row["home"]
 
73
  team_results[home_team][1] += 1
74
  team_results[away_team][1] += 1
75
 
76
+ # Create a DataFrame from the results dictionary and calculate the win percentage
77
  df = pd.DataFrame.from_dict(
78
  team_results, orient="index", columns=["wins", "draws", "losses"]
79
  ).sort_index()
 
81
  df = df[["owner", "team", "wins", "draws", "losses"]]
82
  df["win_pct"] = (df["wins"] / (df["wins"] + df["draws"] + df["losses"])) * 100
83
 
84
+ # Display the DataFrame as a table, sorted by win percentage
85
+ stats = df.sort_values(by="win_pct", ascending=False)
86
+ st.dataframe(stats)
87
+
88
+ # Create a new tab for league history over time
89
+ with tab_history:
90
+ st.write("Coming soon!")