hectorjelly commited on
Commit
cba8171
1 Parent(s): bde45ea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +206 -26
app.py CHANGED
@@ -1,27 +1,207 @@
1
- import streamlit as st
 
2
  import pandas as pd
3
- import matplotlib.pyplot as plt
4
- ata = pd.read_csv('my_data.csv')
5
-
6
- # Add a title to the web application
7
- st.title('My Streamlit App')
8
-
9
- # Add a subtitle
10
- st.markdown('This is a simple app that displays a scatter plot.')
11
-
12
- # Add a scatter plot using Matplotlib
13
- fig, ax = plt.subplots()
14
- ax.scatter(data['x'], data['y'])
15
- ax.set_xlabel('X')
16
- ax.set_ylabel('Y')
17
- st.pyplot(fig)
18
-
19
- # Add a slider to control the size of the scatter plot
20
- size = st.slider('Marker size', 1, 10, 5)
21
-
22
- # Create a scatter plot with the selected marker size
23
- fig, ax = plt.subplots()
24
- ax.scatter(data['x'], data['y'], s=size*10)
25
- ax.set_xlabel('X')
26
- ax.set_ylabel('Y')
27
- st.pyplot(fig)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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(
11
+ """
12
+ <style>
13
+ .block-container {
14
+ padding-top: 1rem;
15
+ }
16
+ #MainMenu {visibility: hidden;}
17
+ </style>
18
+ """,
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"]
60
+ away_team = row["away"]
61
+ result = row["result"]
62
+
63
+ if home_team not in team_results:
64
+ team_results[home_team] = [0, 0, 0]
65
+
66
+ if away_team not in team_results:
67
+ team_results[away_team] = [0, 0, 0]
68
+
69
+ if result == 0:
70
+ team_results[home_team][2] += 1
71
+ team_results[away_team][0] += 1
72
+ elif result == 1:
73
+ team_results[home_team][0] += 1
74
+ team_results[away_team][2] += 1
75
+ else:
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()
83
+ 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"])
207
+