2nzi commited on
Commit
bf697e6
·
verified ·
1 Parent(s): 382e54a

update app.py

Browse files
Files changed (1) hide show
  1. app.py +265 -265
app.py CHANGED
@@ -1,266 +1,266 @@
1
- import streamlit as st
2
- import pandas as pd
3
- from statsbombpy import sb
4
- import config_location_player as clp
5
- import functions
6
- import Pitch3D
7
- import json
8
- from stats_manager import StatsManager
9
-
10
- st.set_page_config(layout="wide")
11
-
12
- st.markdown(
13
- """
14
- <style>
15
- .centered-image {
16
- display: block;
17
- margin-left: auto;
18
- margin-right: auto;
19
- width: 20%; /* Adjust this percentage to resize the image */
20
- }
21
- .ban-image {
22
- display: block;
23
- margin-left: 0;
24
- margin-right: 0;
25
- width: 100%; /* Adjust this percentage to resize the image */
26
- height: 50%; /* Adjust this percentage to resize the image */
27
- }
28
- .banner-image {
29
- background-image: url('https://statsbomb.com/wp-content/uploads/2023/03/IconLockup_MediaPack-min.png');
30
- background-size: cover;
31
- background-position: center;
32
- height: 300px; /* Adjust the height to your preference */
33
- width: 100%;
34
- margin: 0 auto;
35
- margin-bottom: 100px;
36
- }
37
- </style>
38
- """,
39
- unsafe_allow_html=True
40
- )
41
-
42
- st.markdown('<div class="banner-image"></div>', unsafe_allow_html=True)
43
-
44
-
45
- # st.markdown(
46
- # '<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRsmWldT7_OE2kDhbehYcuTNHzItGFbeH5igw&s" class="centered-image"> <br> <br> <br>',
47
- # unsafe_allow_html=True
48
- # )
49
-
50
-
51
-
52
- #PARTIE 1 : MATCH CHOOSEN
53
- col1, col2 = st.columns([1, 3])
54
- with col1:
55
-
56
- competition = sb.competitions()
57
-
58
- #Gender Choice
59
- # on = st.toggle("Men" if st.session_state.get('competition_gender', 'women') == 'women' else "Women")
60
- # competition_gender = 'men' if on else 'women'
61
- # st.session_state.competition_gender = competition_gender
62
- competition_gender = "male"
63
- competition_gender_df = competition[competition['competition_gender']==competition_gender]
64
-
65
-
66
-
67
- #Competition Choice
68
- competitions = competition_gender_df['competition_name'].unique()
69
- selected_competition = st.selectbox(
70
- "Choose your competition",
71
- competitions,
72
- )
73
- selected_competition_df = competition_gender_df[competition_gender_df['competition_name']==selected_competition]
74
-
75
-
76
-
77
-
78
-
79
- #Season Choice
80
- seasons = selected_competition_df['season_name'].unique()
81
- selected_season = st.selectbox(
82
- "Choose your season",
83
- seasons,
84
- )
85
-
86
- competition_id = selected_competition_df[selected_competition_df['season_name']==selected_season]['competition_id'].iloc[0]
87
- season_id = selected_competition_df[selected_competition_df['season_name']==selected_season]['season_id'].iloc[0]
88
- matches = sb.matches(competition_id=competition_id, season_id=season_id)
89
-
90
-
91
- #Season Choice
92
- # teams = selected_competition_df[['home_team','away_team']].unique()
93
- teams = pd.concat([matches['away_team'], matches['home_team']]).unique()
94
- selected_team = st.selectbox(
95
- "Choose your team",
96
- teams,
97
- )
98
- one_team_matches = matches[(matches['home_team'] == selected_team) | (matches['away_team'] == selected_team)]
99
- # matches = one_team_matches['match_id']
100
- matches = one_team_matches['match_date']
101
-
102
- selected_match = st.selectbox(
103
- "Choose your match",
104
- matches,
105
- )
106
- selected_one_team_matches = one_team_matches[one_team_matches['match_date']==selected_match]
107
- match_id = selected_one_team_matches['match_id'].iloc[0]
108
-
109
- home_team = selected_one_team_matches['home_team'].iloc[0]
110
- home_score = selected_one_team_matches['home_score'].iloc[0]
111
- home_color = "#0B8494"
112
-
113
- away_team = selected_one_team_matches['away_team'].iloc[0]
114
- away_score = selected_one_team_matches['away_score'].iloc[0]
115
- away_color = "#F05A7E"
116
-
117
- home_lineups = sb.lineups(match_id=match_id)[home_team]
118
- away_lineups = sb.lineups(match_id=match_id)[away_team]
119
-
120
- events = sb.events(match_id=match_id)
121
- home_tactic_formation = events['tactics'].iloc[0]['formation']
122
- away_tactic_formation = events['tactics'].iloc[1]['formation']
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
- #PARTIE 2 : DASHBOARD
131
- with col2:
132
-
133
- # st.write(events.filter(regex='^bad_behaviour_card|^team$'))
134
-
135
- # Créer un gestionnaire de statistiques
136
- stats_manager = StatsManager(events)
137
-
138
- # Appel des fonctions via le gestionnaire
139
- home_possession, away_possession = stats_manager.get_possession()
140
- home_xg, away_xg = stats_manager.get_total_xg()
141
- home_shots, away_shots = stats_manager.get_total_shots()
142
- home_off_target, away_off_target = stats_manager.get_total_shots_off_target()
143
- home_on_target, away_on_target = stats_manager.get_total_shots_on_target()
144
- home_passes, away_passes = stats_manager.get_total_passes()
145
- home_successful_passes, away_successful_passes = stats_manager.get_successful_passes()
146
- home_corners, away_corners = stats_manager.get_total_corners()
147
- home_fouls, away_fouls = stats_manager.get_total_fouls()
148
- home_yellow_cards, away_yellow_cards = stats_manager.get_total_yellow_cards()
149
- home_red_cards, away_red_cards = stats_manager.get_total_red_cards()
150
-
151
- # Créer la liste des scores, en excluant ceux qui ne peuvent pas être calculés
152
- categories_scores = [
153
- {"catégorie": "Possession de balle (%)", "Home Team": home_possession, "Away Team": away_possession},
154
- {"catégorie": "xG (Buts attendus)", "Home Team": home_xg, "Away Team": away_xg},
155
- {"catégorie": "Tirs", "Home Team": home_shots, "Away Team": away_shots},
156
- {"catégorie": "Tirs cadrés", "Home Team": home_on_target, "Away Team": away_on_target},
157
- {"catégorie": "Tirs non cadrés", "Home Team": home_off_target, "Away Team": away_off_target},
158
- {"catégorie": "Passes", "Home Team": home_passes, "Away Team": away_passes},
159
- {"catégorie": "Passes réussies", "Home Team": home_successful_passes, "Away Team": away_successful_passes},
160
- {"catégorie": "Corners", "Home Team": home_corners, "Away Team": away_corners},
161
- {"catégorie": "Fautes", "Home Team": home_fouls, "Away Team": away_fouls},
162
- {"catégorie": "Cartons Jaunes", "Home Team": home_yellow_cards, "Away Team": away_yellow_cards},
163
- {"catégorie": "Cartons Rouges", "Home Team": home_red_cards, "Away Team": away_red_cards},
164
- ]
165
-
166
- # Filtrer les catégories valides
167
- categories_scores = [entry for entry in categories_scores if entry is not None]
168
-
169
- # Extraire les catégories et scores
170
- categories = [entry['catégorie'] for entry in categories_scores]
171
- home_scores = [entry['Home Team'] for entry in categories_scores]
172
- away_scores = [entry['Away Team'] for entry in categories_scores]
173
-
174
- # Afficher le graphique
175
- functions.display_normalized_scores(home_scores, away_scores, categories, home_color=home_color, away_color=away_color)
176
-
177
-
178
-
179
- # st.image('img/logo_stade.png', width=200)
180
- pitch_color='#d2d2d2',
181
- st.markdown(
182
- f"""
183
- <div style='text-align: center;'>
184
- <span style='color: {home_color}; margin-right: 30px;'>{home_team} {home_score}</span>
185
- <span style='margin-right: 30px;'>-</span>
186
- <span style='color: {away_color};'>{away_score} {away_team}</span>
187
- </div>
188
- <div style='text-align: center;'>
189
- <span style='color: {pitch_color}; margin-right: 30px;'>{home_tactic_formation}</span>
190
- <span style='margin-right: 30px;'> </span>
191
- <span style='color: {pitch_color};'>{away_tactic_formation}</span>
192
- </div>
193
- """,
194
- unsafe_allow_html=True
195
- )
196
-
197
-
198
- col21, col22 = st.columns([1,1])
199
-
200
- position_id_to_coordinates_home = clp.initial_player_position_allpitch_home
201
- position_id_to_coordinates_away = clp.initial_player_position_allpitch_away
202
- # functions.display_player_names_and_positions_twoTeam(home_lineups, away_lineups, position_id_to_coordinates_home, position_id_to_coordinates_away)
203
-
204
- with open('data/club.json', encoding='utf-8') as f:
205
- images_data = json.load(f)
206
-
207
- # Integrate club logo
208
- home_team_image = functions.get_best_match_image(home_team, images_data)
209
- away_team_image = functions.get_best_match_image(away_team, images_data)
210
-
211
-
212
- with col21:
213
-
214
- # if home_team_image:
215
- # st.image(home_team_image)
216
-
217
- functions.display_player_names_and_positions_oneTeam(home_lineups, position_id_to_coordinates_home, home_color)
218
-
219
- st.markdown(
220
- f"""
221
- <div style='text-align: center;'>
222
- <span style='color: {home_color}; margin-right: 30px;'>{selected_one_team_matches['home_managers'].iloc[0]}</span>
223
- </div>
224
- """,
225
- unsafe_allow_html=True
226
- )
227
-
228
- with col22:
229
-
230
- # if away_team_image:
231
- # st.image(away_team_image)
232
-
233
- functions.display_player_names_and_positions_oneTeam(away_lineups, position_id_to_coordinates_away,away_color)
234
-
235
- st.markdown(
236
- f"""
237
- <div style='text-align: center;'>
238
- <span style='color: {away_color}; margin-right: 30px;'>{selected_one_team_matches['away_managers'].iloc[0]}</span>
239
- </div>
240
- """,
241
- unsafe_allow_html=True
242
- )
243
-
244
-
245
-
246
-
247
-
248
-
249
-
250
- def ensure_3d_coordinates(coord):
251
- if len(coord) == 2:
252
- return coord + [0]
253
- return coord
254
-
255
- shot_events = events.filter(regex='^(shot|location|team)').dropna(how='all')
256
- shot_events_location = shot_events[shot_events['shot_end_location'].notna()]
257
-
258
- shot_events_location['location'] = shot_events_location['location'].apply(ensure_3d_coordinates)
259
- shot_events_location['shot_end_location'] = shot_events_location['shot_end_location'].apply(ensure_3d_coordinates)
260
-
261
- start_points = shot_events_location['location'].tolist()
262
- end_points = shot_events_location['shot_end_location'].tolist()
263
-
264
- st.write(shot_events_location)
265
-
266
  Pitch3D.main_3D_pitch(start_points,end_points)
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ from statsbombpy import sb
4
+ import config_location_player as clp
5
+ import functions
6
+ import Pitch3D
7
+ import json
8
+ from stats_manager import StatsManager
9
+
10
+ st.set_page_config(layout="wide")
11
+
12
+ st.markdown(
13
+ """
14
+ <style>
15
+ .centered-image {
16
+ display: block;
17
+ margin-left: auto;
18
+ margin-right: auto;
19
+ width: 20%; /* Adjust this percentage to resize the image */
20
+ }
21
+ .ban-image {
22
+ display: block;
23
+ margin-left: 0;
24
+ margin-right: 0;
25
+ width: 100%; /* Adjust this percentage to resize the image */
26
+ height: 50%; /* Adjust this percentage to resize the image */
27
+ }
28
+ .banner-image {
29
+ background-image: url('https://statsbomb.com/wp-content/uploads/2023/03/IconLockup_MediaPack-min.png');
30
+ background-size: cover;
31
+ background-position: center;
32
+ height: 300px; /* Adjust the height to your preference */
33
+ width: 100%;
34
+ margin: 0 auto;
35
+ margin-bottom: 100px;
36
+ }
37
+ </style>
38
+ """,
39
+ unsafe_allow_html=True
40
+ )
41
+
42
+ st.markdown('<div class="banner-image"></div>', unsafe_allow_html=True)
43
+
44
+
45
+ # st.markdown(
46
+ # '<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRsmWldT7_OE2kDhbehYcuTNHzItGFbeH5igw&s" class="centered-image"> <br> <br> <br>',
47
+ # unsafe_allow_html=True
48
+ # )
49
+
50
+
51
+
52
+ #PARTIE 1 : MATCH CHOOSEN
53
+ col1, col2 = st.columns([1, 3])
54
+ with col1:
55
+
56
+ competition = sb.competitions()
57
+
58
+ #Gender Choice
59
+ # on = st.toggle("Men" if st.session_state.get('competition_gender', 'women') == 'women' else "Women")
60
+ # competition_gender = 'men' if on else 'women'
61
+ # st.session_state.competition_gender = competition_gender
62
+ competition_gender = "male"
63
+ competition_gender_df = competition[competition['competition_gender']==competition_gender]
64
+
65
+
66
+
67
+ #Competition Choice
68
+ competitions = competition_gender_df['competition_name'].unique()
69
+ selected_competition = st.selectbox(
70
+ "Choose your competition",
71
+ competitions,
72
+ )
73
+ selected_competition_df = competition_gender_df[competition_gender_df['competition_name']==selected_competition]
74
+
75
+
76
+
77
+
78
+
79
+ #Season Choice
80
+ seasons = selected_competition_df['season_name'].unique()
81
+ selected_season = st.selectbox(
82
+ "Choose your season",
83
+ seasons,
84
+ )
85
+
86
+ competition_id = selected_competition_df[selected_competition_df['season_name']==selected_season]['competition_id'].iloc[0]
87
+ season_id = selected_competition_df[selected_competition_df['season_name']==selected_season]['season_id'].iloc[0]
88
+ matches = sb.matches(competition_id=competition_id, season_id=season_id)
89
+
90
+
91
+ #Season Choice
92
+ # teams = selected_competition_df[['home_team','away_team']].unique()
93
+ teams = pd.concat([matches['away_team'], matches['home_team']]).unique()
94
+ selected_team = st.selectbox(
95
+ "Choose your team",
96
+ teams,
97
+ )
98
+ one_team_matches = matches[(matches['home_team'] == selected_team) | (matches['away_team'] == selected_team)]
99
+ # matches = one_team_matches['match_id']
100
+ matches = one_team_matches['match_date']
101
+
102
+ selected_match = st.selectbox(
103
+ "Choose your match",
104
+ matches,
105
+ )
106
+ selected_one_team_matches = one_team_matches[one_team_matches['match_date']==selected_match]
107
+ match_id = selected_one_team_matches['match_id'].iloc[0]
108
+
109
+ home_team = selected_one_team_matches['home_team'].iloc[0]
110
+ home_score = selected_one_team_matches['home_score'].iloc[0]
111
+ home_color = "#0B8494"
112
+
113
+ away_team = selected_one_team_matches['away_team'].iloc[0]
114
+ away_score = selected_one_team_matches['away_score'].iloc[0]
115
+ away_color = "#F05A7E"
116
+
117
+ home_lineups = sb.lineups(match_id=match_id)[home_team]
118
+ away_lineups = sb.lineups(match_id=match_id)[away_team]
119
+
120
+ events = sb.events(match_id=match_id)
121
+ home_tactic_formation = events['tactics'].iloc[0]['formation']
122
+ away_tactic_formation = events['tactics'].iloc[1]['formation']
123
+
124
+
125
+
126
+
127
+
128
+
129
+
130
+ #PARTIE 2 : DASHBOARD
131
+ with col2:
132
+
133
+ # st.write(events.filter(regex='^bad_behaviour_card|^team$'))
134
+
135
+ # Créer un gestionnaire de statistiques
136
+ stats_manager = StatsManager(events)
137
+
138
+ # Appel des fonctions via le gestionnaire
139
+ home_possession, away_possession = stats_manager.get_possession()
140
+ home_xg, away_xg = stats_manager.get_total_xg()
141
+ home_shots, away_shots = stats_manager.get_total_shots()
142
+ home_off_target, away_off_target = stats_manager.get_total_shots_off_target()
143
+ home_on_target, away_on_target = stats_manager.get_total_shots_on_target()
144
+ home_passes, away_passes = stats_manager.get_total_passes()
145
+ home_successful_passes, away_successful_passes = stats_manager.get_successful_passes()
146
+ home_corners, away_corners = stats_manager.get_total_corners()
147
+ home_fouls, away_fouls = stats_manager.get_total_fouls()
148
+ home_yellow_cards, away_yellow_cards = stats_manager.get_total_yellow_cards()
149
+ home_red_cards, away_red_cards = stats_manager.get_total_red_cards()
150
+
151
+ # Créer la liste des scores, en excluant ceux qui ne peuvent pas être calculés
152
+ categories_scores = [
153
+ {"catégorie": "Possession de balle (%)", "Home Team": home_possession, "Away Team": away_possession},
154
+ {"catégorie": "xG (Buts attendus)", "Home Team": home_xg, "Away Team": away_xg},
155
+ {"catégorie": "Tirs", "Home Team": home_shots, "Away Team": away_shots},
156
+ {"catégorie": "Tirs cadrés", "Home Team": home_on_target, "Away Team": away_on_target},
157
+ {"catégorie": "Tirs non cadrés", "Home Team": home_off_target, "Away Team": away_off_target},
158
+ {"catégorie": "Passes", "Home Team": home_passes, "Away Team": away_passes},
159
+ {"catégorie": "Passes réussies", "Home Team": home_successful_passes, "Away Team": away_successful_passes},
160
+ {"catégorie": "Corners", "Home Team": home_corners, "Away Team": away_corners},
161
+ {"catégorie": "Fautes", "Home Team": home_fouls, "Away Team": away_fouls},
162
+ {"catégorie": "Cartons Jaunes", "Home Team": home_yellow_cards, "Away Team": away_yellow_cards},
163
+ {"catégorie": "Cartons Rouges", "Home Team": home_red_cards, "Away Team": away_red_cards},
164
+ ]
165
+
166
+ # Filtrer les catégories valides
167
+ categories_scores = [entry for entry in categories_scores if entry is not None]
168
+
169
+ # Extraire les catégories et scores
170
+ categories = [entry['catégorie'] for entry in categories_scores]
171
+ home_scores = [entry['Home Team'] for entry in categories_scores]
172
+ away_scores = [entry['Away Team'] for entry in categories_scores]
173
+
174
+ # Afficher le graphique
175
+ functions.display_normalized_scores(home_scores, away_scores, categories, home_color=home_color, away_color=away_color)
176
+
177
+
178
+
179
+ # st.image('img/logo_stade.png', width=200)
180
+ pitch_color='#d2d2d2',
181
+ st.markdown(
182
+ f"""
183
+ <div style='text-align: center;'>
184
+ <span style='color: {home_color}; margin-right: 30px;'>{home_team} {home_score}</span>
185
+ <span style='margin-right: 30px;'>-</span>
186
+ <span style='color: {away_color};'>{away_score} {away_team}</span>
187
+ </div>
188
+ <div style='text-align: center;'>
189
+ <span style='color: {pitch_color}; margin-right: 30px;'>{home_tactic_formation}</span>
190
+ <span style='margin-right: 30px;'> </span>
191
+ <span style='color: {pitch_color};'>{away_tactic_formation}</span>
192
+ </div>
193
+ """,
194
+ unsafe_allow_html=True
195
+ )
196
+
197
+
198
+ col21, col22 = st.columns([1,1])
199
+
200
+ position_id_to_coordinates_home = clp.initial_player_position_allpitch_home
201
+ position_id_to_coordinates_away = clp.initial_player_position_allpitch_away
202
+ # functions.display_player_names_and_positions_twoTeam(home_lineups, away_lineups, position_id_to_coordinates_home, position_id_to_coordinates_away)
203
+
204
+ with open('data/club.json', encoding='utf-8') as f:
205
+ images_data = json.load(f)
206
+
207
+ # Integrate club logo
208
+ home_team_image = functions.get_best_match_image(home_team, images_data)
209
+ away_team_image = functions.get_best_match_image(away_team, images_data)
210
+
211
+
212
+ with col21:
213
+
214
+ # if home_team_image:
215
+ # st.image(home_team_image)
216
+
217
+ functions.display_player_names_and_positions_oneTeam(home_lineups, position_id_to_coordinates_home, home_color)
218
+
219
+ st.markdown(
220
+ f"""
221
+ <div style='text-align: center;'>
222
+ <span style='color: {home_color}; margin-right: 30px;'>{selected_one_team_matches['home_managers'].iloc[0]}</span>
223
+ </div>
224
+ """,
225
+ unsafe_allow_html=True
226
+ )
227
+
228
+ with col22:
229
+
230
+ # if away_team_image:
231
+ # st.image(away_team_image)
232
+
233
+ functions.display_player_names_and_positions_oneTeam(away_lineups, position_id_to_coordinates_away,away_color)
234
+
235
+ st.markdown(
236
+ f"""
237
+ <div style='text-align: center;'>
238
+ <span style='color: {away_color}; margin-right: 30px;'>{selected_one_team_matches['away_managers'].iloc[0]}</span>
239
+ </div>
240
+ """,
241
+ unsafe_allow_html=True
242
+ )
243
+
244
+
245
+
246
+
247
+
248
+
249
+
250
+ def ensure_3d_coordinates(coord):
251
+ if len(coord) == 2:
252
+ return coord + [0]
253
+ return coord
254
+
255
+ shot_events = events.filter(regex='^(shot|location|team)').dropna(how='all')
256
+ shot_events_location = shot_events[shot_events['shot_end_location'].notna()]
257
+
258
+ shot_events_location['location'] = shot_events_location['location'].apply(ensure_3d_coordinates)
259
+ shot_events_location['shot_end_location'] = shot_events_location['shot_end_location'].apply(ensure_3d_coordinates)
260
+
261
+ start_points = shot_events_location['location'].tolist()
262
+ end_points = shot_events_location['shot_end_location'].tolist()
263
+
264
+ # st.write(shot_events_location)
265
+
266
  Pitch3D.main_3D_pitch(start_points,end_points)