Spaces:
No application file
No application file
Commit
•
8039ca4
1
Parent(s):
e9c52a1
Update app.py
Browse files
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(
|
@@ -36,18 +33,11 @@ def fetch_match_history():
|
|
36 |
return df
|
37 |
|
38 |
|
39 |
-
|
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 |
)
|
@@ -61,147 +51,37 @@ for i, row in match_df.iterrows():
|
|
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 |
-
|
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 |
|
|
|
|
|
|
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(
|
|
|
33 |
return df
|
34 |
|
35 |
|
36 |
+
match_df = fetch_match_history()
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
def num_matches_played():
|
39 |
return match_df.shape[0]
|
40 |
|
|
|
|
|
41 |
teams = sorted(
|
42 |
list(pd.concat([match_df["home"], match_df["away"]]).unique()), key=str.casefold
|
43 |
)
|
|
|
51 |
result = row["result"]
|
52 |
|
53 |
if home_team not in team_results:
|
54 |
+
team_results[home_team] = [0, 0, 0, 0]
|
55 |
|
56 |
if away_team not in team_results:
|
57 |
+
team_results[away_team] = [0, 0, 0, 0]
|
58 |
|
59 |
if result == 0:
|
60 |
team_results[home_team][2] += 1
|
61 |
team_results[away_team][0] += 1
|
62 |
+
team_results[home_team][3] += 3
|
63 |
elif result == 1:
|
64 |
team_results[home_team][0] += 1
|
65 |
team_results[away_team][2] += 1
|
66 |
+
team_results[home_team][3] += 3
|
67 |
else:
|
68 |
team_results[home_team][1] += 1
|
69 |
team_results[away_team][1] += 1
|
70 |
+
team_results[home_team][3] += 1
|
71 |
+
team_results[away_team][3] += 1
|
72 |
|
73 |
|
74 |
df = pd.DataFrame.from_dict(
|
75 |
+
team_results, orient="index", columns=["wins", "draws", "losses", "points"]
|
76 |
).sort_index()
|
77 |
df[["owner", "team"]] = df.index.to_series().str.split("/", expand=True)
|
78 |
+
df = df[["owner", "team", "wins", "draws", "losses", "points"]]
|
79 |
df["win_pct"] = (df["wins"] / (df["wins"] + df["draws"] + df["losses"])) * 100
|
80 |
|
81 |
+
stats = df.sort_values(by='points', ascending=False)
|
82 |
+
|
83 |
+
st.header("League Table")
|
84 |
+
st.dataframe(stats)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
+
st.subheader("Match Results")
|
87 |
+
st.write("Note: Match results are not displayed in this version of the app.")
|