-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmls_app.py
243 lines (197 loc) · 5.37 KB
/
mls_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import streamlit as st
import numpy as np
import matplotlib.style
import matplotlib as plt
import matplotlib.pyplot as plt
import pandas as pd
from bs4 import BeautifulSoup as bs
import requests
import lxml
import html5lib
import seaborn as sns
import plotly.express as px
import time
import colorcet as cc
st.image("mls_crest.png")
st.title("Current Table in the MLS")
st.write(
"This dashboard contains general stats from Major League Soccer. "
"The dashboard is broken up into four sections by conference:\n"
'1) The leading teams in the categories of "Goals in Favor", "Goals Against", and "XG"\n'
"2) A general stats table\n"
'3) A section created using seaborn library showcasing "Goal Difference", "Goals Scored by team", and "Wins by team"\n'
"4) A pairplot where you can combine different categories on a X-Y chart\n"
)
url_e = "https://fbref.com/en/comps/22/2023/2023-Major-League-Soccer-Stats"
r_e = requests.get(url_e)
soup_e = bs(r_e.content, features="lxml")
table_e = soup_e.find_all("table", id="results2023221Eastern-Conference_overall")
table_e = pd.read_html(str(table_e))
table_e = table_e[0]
table_e.index = table_e["Rk"]
table_e = table_e.drop(["Rk"], axis=True)
table_e = table_e.drop(["Notes"], axis=True)
url_w = "https://fbref.com/en/comps/22/2023/2023-Major-League-Soccer-Stats"
r_w = requests.get(url_w)
soup_w = bs(r_w.content, features="lxml")
table_w = soup_w.find_all("table", id="results2023221Western-Conference_overall")
table_w = pd.read_html(str(table_w))
table_w = table_w[0]
table_w.index = table_w["Rk"]
table_w = table_w.drop(["Rk"], axis=True)
table_w = table_w.drop(["Notes"], axis=True)
st.divider()
st.subheader("Eastern Conference")
col1, col2, col3 = st.columns(3)
a = table_e.query("GF == GF.max()")["Squad"]
b = table_e.query("GA == GA.max()")["Squad"]
c = table_e.query("xG == xG.max()")["Squad"]
col1.caption("Goals in Favor")
col1.write(a)
col2.caption("Goals Against")
col2.write(b)
col3.caption("XG")
col3.write(c)
st.divider()
st.subheader("Western Conference")
col4, col5, col6 = st.columns(3)
d = table_w.query("GF == GF.max()")["Squad"]
e = table_w.query("GA == GA.max()")["Squad"]
f = table_w.query("xG == xG.max()")["Squad"]
col4.caption("Goals in Favor")
col4.write(d)
col5.caption("Goals Against")
col5.write(e)
col6.caption("XG")
col6.write(f)
st.header("Easter Conference Standings")
st.write(table_e)
st.header("Western Conference Standings")
st.write(table_w)
st.divider()
st.write("*Scroll left or right to choose the different charts*")
@st.cache_data
def load_table(conf):
fig = px.bar(
conf,
x=conf.Squad,
y=conf.xG,
title="XG",
text=conf.xG,
color=conf.xG,
color_continuous_scale="ylgn",
height=600,
)
fig.update_xaxes(tickangle=270)
st.write(fig)
@st.cache_data
def load_gDF(conf):
stuff = conf
fig = px.bar(
stuff,
x=stuff.Squad,
y=stuff.GD,
title="Goal Difference : MLS 2023",
text=stuff.GD,
color=stuff.GD,
color_continuous_scale="ylgn",
height=600,
)
fig.update_xaxes(tickangle=270)
st.write(fig)
@st.cache_data
def load_pie(conf):
fig = px.pie(conf, values="GF", names="Squad", title="Share of total Goals scored")
st.write(fig)
(
tab1,
tab2,
tab3,
tab4,
tab5,
tab6,
) = st.tabs(
[
"Eastern Conference xG",
"Western Conference xG",
"Eastern Conference Goals Scored",
"Western Conference Goals Scored",
"Eastern Conference Goal Difference",
"Western Conference Goal Difference",
]
)
with tab1:
st.header("Eastern Conference")
load_table(table_e)
with tab2:
st.header("Western Conference")
load_table(table_w)
with tab3:
st.header("Eastern Conference")
load_pie(table_e)
with tab4:
st.header("Western Conference")
load_pie(table_w)
with tab5:
st.header("Eastern Conference")
load_gDF(table_e)
with tab6:
st.header("Western Conference")
load_gDF(table_w)
st.divider()
st.write(
"Here you can create your own plot combining\n"
"any of the categories below and see how they relate to each other.\n"
"The results my surprise you.\n"
)
conference = st.radio(
"Eastern or Western Conference:",
("Eastern", "Western"),
index=0,
horizontal=True,
)
if conference == "Eastern":
conf = table_e
if conference == "Western":
conf = table_w
x = str(
st.radio(
"X-Axis:",
("GF", "GA", "W", "D", "L", "MP", "xG", "Pts"),
index=0,
horizontal=True,
)
)
y = str(
st.radio(
"Y-Axis:",
("GF", "GA", "W", "D", "L", "MP", "xG", "Pts"),
index=0,
horizontal=True,
)
)
button3 = st.button("Submit selection")
st.divider()
@st.cache_data
def load_pair(conf, x, y):
stuff = conf
x = stuff[x]
y = stuff[y]
fig = px.scatter(
conf,
x=x,
y=y,
color="Squad",
color_discrete_sequence=px.colors.qualitative.Light24,
)
fig.update_traces(marker={"size": 15})
st.write(fig)
if button3:
with st.spinner("Who will win the MLS Cup....."):
time.sleep(5)
load_pair(conf, x, y)
st.write("*Data is provided by Sports-Reference.com*")
st.write("Jorge Mario Restrepo")
st.write("*Contact*")
st.write("Email: [email protected]\n")
st.write("Linkedin: www.linkedin.com/in/jorgemariorest\n")