nesticot commited on
Commit
1606618
·
verified ·
1 Parent(s): 9e77e0f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -32
app.py CHANGED
@@ -46,6 +46,7 @@ markdown_text = """
46
  ##### By: Thomas Nestico ([@TJStats](https://x.com/TJStats))
47
  ##### Data: [MLB](https://baseballsavant.mlb.com/)
48
 
 
49
  This Streamlit app retrieves catch probability data for a selected fielder from [Baseball Savant](https://baseballsavant.mlb.com/leaderboard/catch_probability).
50
  The app displays the fielder's data in a table and allows the user to select a
51
  row to view the corresponding catch video.
@@ -126,55 +127,56 @@ df_merge['catch_rate'] = df_merge['catch_rate'].astype(float).apply(lambda x: f"
126
  df_merge['pos'] = df_merge['pos'].astype(int)
127
  df_merge['Position'] = df_merge['pos'].map(pos_dict)
128
 
 
129
  df_merge = df_merge[df_merge['batter_id'] != df_merge['player_id']]
130
 
131
 
132
  if df_merge.empty:
133
  st.write("No data available for the selected fielder.")
134
  st.stop()
135
- else:
136
- column_names = ['game_date','batter_name', 'pitcher_name', 'name_display_first_last', 'Position','event', 'out', 'wall', 'back', 'stars', 'distance', 'hang_time', 'catch_rate']
137
 
138
- column_names_display = ['Game Date','Batter Name', 'Pitcher Name', 'Fielder Name', 'Position','Event', 'Out', 'Wall', 'Back', 'Stars', 'Distance', 'Hang Time', 'Catch Rate']
 
 
139
 
140
 
141
 
142
 
143
 
144
- # Use a container to control the width of the AgGrid display
145
- with st.container():
146
- st.write("### Fielder Data")
147
- # Configure the AgGrid options
148
- gb = GridOptionsBuilder.from_dataframe(df_merge[column_names])
149
- # Set display names for columns
150
- for col, display_name in zip(column_names, column_names_display):
151
- gb.configure_column(col, headerName=display_name)
152
 
153
 
154
- gb.configure_selection('single', use_checkbox=True)
155
- grid_options = gb.build()
156
 
157
- # Display the dataframe using AgGrid
158
- grid_response = AgGrid(
159
- df_merge[column_names],
160
- gridOptions=grid_options,
161
- update_mode=GridUpdateMode.SELECTION_CHANGED,
162
- height=300,
163
- allow_unsafe_jscode=True,
164
- )
165
 
166
- # Get the selected row index
167
 
168
 
169
- try:
170
- # Update the video URL based on the selected row
171
- selected_row_index = int(grid_response['selected_rows'].index.values[0])
172
- a = requests.get(f'https://baseballsavant.mlb.com/sporty-videos?playId={df_merge["play_id"].values[selected_row_index]}')
173
- soup = BeautifulSoup(a.content, 'lxml')
174
- video_url = str(soup).split('<source src="')[1].split('" ')[0]
175
- # Share the video through Streamlit
176
- st.video(video_url)
177
- except AttributeError:
178
- st.write("Select Row to Display Video")
179
 
180
  st.markdown('</div>', unsafe_allow_html=True)
 
46
  ##### By: Thomas Nestico ([@TJStats](https://x.com/TJStats))
47
  ##### Data: [MLB](https://baseballsavant.mlb.com/)
48
 
49
+ #### About
50
  This Streamlit app retrieves catch probability data for a selected fielder from [Baseball Savant](https://baseballsavant.mlb.com/leaderboard/catch_probability).
51
  The app displays the fielder's data in a table and allows the user to select a
52
  row to view the corresponding catch video.
 
127
  df_merge['pos'] = df_merge['pos'].astype(int)
128
  df_merge['Position'] = df_merge['pos'].map(pos_dict)
129
 
130
+
131
  df_merge = df_merge[df_merge['batter_id'] != df_merge['player_id']]
132
 
133
 
134
  if df_merge.empty:
135
  st.write("No data available for the selected fielder.")
136
  st.stop()
 
 
137
 
138
+ df_merge.sort_values(by='game_date',inplace=True)
139
+ column_names = ['game_date','batter_name', 'pitcher_name', 'name_display_first_last', 'Position','event', 'out', 'wall', 'back', 'stars', 'distance', 'hang_time', 'catch_rate']
140
+ column_names_display = ['Game Date','Batter Name', 'Pitcher Name', 'Fielder Name', 'Position','Event', 'Out', 'Wall', 'Back', 'Stars', 'Distance', 'Hang Time', 'Catch Rate']
141
 
142
 
143
 
144
 
145
 
146
+ # Use a container to control the width of the AgGrid display
147
+ with st.container():
148
+ st.write("### Fielder Data")
149
+ # Configure the AgGrid options
150
+ gb = GridOptionsBuilder.from_dataframe(df_merge[column_names])
151
+ # Set display names for columns
152
+ for col, display_name in zip(column_names, column_names_display):
153
+ gb.configure_column(col, headerName=display_name)
154
 
155
 
156
+ gb.configure_selection('single', use_checkbox=True)
157
+ grid_options = gb.build()
158
 
159
+ # Display the dataframe using AgGrid
160
+ grid_response = AgGrid(
161
+ df_merge[column_names],
162
+ gridOptions=grid_options,
163
+ update_mode=GridUpdateMode.SELECTION_CHANGED,
164
+ height=300,
165
+ allow_unsafe_jscode=True,
166
+ )
167
 
168
+ # Get the selected row index
169
 
170
 
171
+ try:
172
+ # Update the video URL based on the selected row
173
+ selected_row_index = int(grid_response['selected_rows'].index.values[0])
174
+ a = requests.get(f'https://baseballsavant.mlb.com/sporty-videos?playId={df_merge["play_id"].values[selected_row_index]}')
175
+ soup = BeautifulSoup(a.content, 'lxml')
176
+ video_url = str(soup).split('<source src="')[1].split('" ')[0]
177
+ # Share the video through Streamlit
178
+ st.video(video_url)
179
+ except AttributeError:
180
+ st.write("Select Row to Display Video")
181
 
182
  st.markdown('</div>', unsafe_allow_html=True)