James McCool commited on
Commit
0c0ffea
·
1 Parent(s): 88c5476

Refactor stack analysis logic in Streamlit app for improved readability and maintainability. Consolidate repeated code blocks for handling different stack sizes and ensure consistent data processing before displaying results. Add functionality to export generated tables as CSV.

Browse files
Files changed (1) hide show
  1. app.py +144 -145
app.py CHANGED
@@ -128,151 +128,150 @@ with col1:
128
 
129
  with col2:
130
  stack_hold_container = st.empty()
131
- if st.button('Run stack analysis'):
132
- comb_list = []
133
- if pos_split2 == 'All Positions':
134
- raw_baselines = raw_baselines
135
- elif pos_split2 != 'All Positions':
136
- raw_baselines = raw_baselines[raw_baselines['Position'].str.contains('|'.join(pos_var2))]
137
-
138
- for cur_team in team_var2:
139
- working_baselines = raw_baselines
140
- working_baselines = working_baselines[working_baselines['Team'] == cur_team]
141
- working_baselines = working_baselines[working_baselines['Position'] != 'SP']
142
- working_baselines = working_baselines[working_baselines['Position'] != 'P']
143
- order_list = working_baselines['Player']
144
-
145
- comb = combinations(order_list, stack_size)
146
-
147
- for i in list(comb):
148
- comb_list.append(i)
149
-
150
- comb_DF = pd.DataFrame(comb_list)
151
-
152
- if stack_size == 3:
153
- comb_DF['Team'] = comb_DF[0].map(team_dict)
154
-
155
- comb_DF['Proj'] = sum([comb_DF[0].map(proj_dict),
156
- comb_DF[1].map(proj_dict),
157
- comb_DF[2].map(proj_dict)])
158
-
159
- comb_DF['Salary'] = sum([comb_DF[0].map(cost_dict),
160
- comb_DF[1].map(cost_dict),
161
- comb_DF[2].map(cost_dict)])
162
-
163
- comb_DF['Own%'] = sum([comb_DF[0].map(own_dict),
164
- comb_DF[1].map(own_dict),
165
- comb_DF[2].map(own_dict)])
166
- elif stack_size == 4:
167
- comb_DF['Team'] = comb_DF[0].map(team_dict)
168
-
169
- comb_DF['Proj'] = sum([comb_DF[0].map(proj_dict),
170
- comb_DF[1].map(proj_dict),
171
- comb_DF[2].map(proj_dict),
172
- comb_DF[3].map(proj_dict)])
173
-
174
- comb_DF['Salary'] = sum([comb_DF[0].map(cost_dict),
175
- comb_DF[1].map(cost_dict),
176
- comb_DF[2].map(cost_dict),
177
- comb_DF[3].map(cost_dict)])
178
-
179
- comb_DF['Own%'] = sum([comb_DF[0].map(own_dict),
180
- comb_DF[1].map(own_dict),
181
- comb_DF[2].map(own_dict),
182
- comb_DF[3].map(own_dict)])
183
- elif stack_size == 5:
184
- comb_DF['Team'] = comb_DF[0].map(team_dict)
185
-
186
- comb_DF['Proj'] = sum([comb_DF[0].map(proj_dict),
187
- comb_DF[1].map(proj_dict),
188
- comb_DF[2].map(proj_dict),
189
- comb_DF[3].map(proj_dict),
190
- comb_DF[4].map(proj_dict)])
191
-
192
- comb_DF['Salary'] = sum([comb_DF[0].map(cost_dict),
193
- comb_DF[1].map(cost_dict),
194
- comb_DF[2].map(cost_dict),
195
- comb_DF[3].map(cost_dict),
196
- comb_DF[4].map(cost_dict)])
197
-
198
- comb_DF['Own%'] = sum([comb_DF[0].map(own_dict),
199
- comb_DF[1].map(own_dict),
200
- comb_DF[2].map(own_dict),
201
- comb_DF[3].map(own_dict),
202
- comb_DF[4].map(own_dict)])
203
-
204
- comb_DF = comb_DF.sort_values(by='Proj', ascending=False)
205
- comb_DF = comb_DF.loc[comb_DF['Salary'] <= max_sal2]
206
-
207
- cut_var = 0
208
-
209
- if stack_size == 3:
210
- while cut_var <= int(len(comb_DF)):
211
- try:
212
- if int(cut_var) == 0:
213
- cur_proj = float(comb_DF.iat[cut_var,4])
 
 
 
 
 
 
 
 
 
214
  cur_own = float(comb_DF.iat[cut_var,6])
215
- elif int(cut_var) >= 1:
216
- check_own = float(comb_DF.iat[cut_var,6])
217
- if check_own > cur_own:
218
- comb_DF = comb_DF.drop([cut_var])
219
- cur_own = cur_own
220
- cut_var = cut_var - 1
221
- comb_DF = comb_DF.reset_index()
222
- comb_DF = comb_DF.drop(['index'], axis=1)
223
- elif check_own <= cur_own:
224
- cur_own = float(comb_DF.iat[cut_var,6])
225
- cut_var = cut_var
226
- cut_var += 1
227
- except:
228
- cut_var += 1
229
- elif stack_size == 4:
230
- while cut_var <= int(len(comb_DF)):
231
- try:
232
- if int(cut_var) == 0:
233
- cur_proj = float(comb_DF.iat[cut_var,5])
234
  cur_own = float(comb_DF.iat[cut_var,7])
235
- elif int(cut_var) >= 1:
236
- check_own = float(comb_DF.iat[cut_var,7])
237
- if check_own > cur_own:
238
- comb_DF = comb_DF.drop([cut_var])
239
- cur_own = cur_own
240
- cut_var = cut_var - 1
241
- comb_DF = comb_DF.reset_index()
242
- comb_DF = comb_DF.drop(['index'], axis=1)
243
- elif check_own <= cur_own:
244
- cur_own = float(comb_DF.iat[cut_var,7])
245
- cut_var = cut_var
246
- cut_var += 1
247
- except:
248
- cut_var += 1
249
- elif stack_size == 5:
250
- while cut_var <= int(len(comb_DF)):
251
- try:
252
- if int(cut_var) == 0:
253
- cur_proj = float(comb_DF.iat[cut_var,6])
254
  cur_own = float(comb_DF.iat[cut_var,8])
255
- elif int(cut_var) >= 1:
256
- check_own = float(comb_DF.iat[cut_var,8])
257
- if check_own > cur_own:
258
- comb_DF = comb_DF.drop([cut_var])
259
- cur_own = cur_own
260
- cut_var = cut_var - 1
261
- comb_DF = comb_DF.reset_index()
262
- comb_DF = comb_DF.drop(['index'], axis=1)
263
- elif check_own <= cur_own:
264
- cur_own = float(comb_DF.iat[cut_var,8])
265
- cut_var = cut_var
266
- cut_var += 1
267
- except:
268
- cut_var += 1
269
-
270
- with stack_hold_container:
271
- stack_hold_container = st.empty()
272
- st.dataframe(comb_DF.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
273
- st.download_button(
274
- label="Export Tables",
275
- data=convert_df_to_csv(comb_DF),
276
- file_name='MLB_Stack_Options_export.csv',
277
- mime='text/csv',
278
- )
 
128
 
129
  with col2:
130
  stack_hold_container = st.empty()
131
+ comb_list = []
132
+ if pos_split2 == 'All Positions':
133
+ raw_baselines = raw_baselines
134
+ elif pos_split2 != 'All Positions':
135
+ raw_baselines = raw_baselines[raw_baselines['Position'].str.contains('|'.join(pos_var2))]
136
+
137
+ for cur_team in team_var2:
138
+ working_baselines = raw_baselines
139
+ working_baselines = working_baselines[working_baselines['Team'] == cur_team]
140
+ working_baselines = working_baselines[working_baselines['Position'] != 'SP']
141
+ working_baselines = working_baselines[working_baselines['Position'] != 'P']
142
+ order_list = working_baselines['Player']
143
+
144
+ comb = combinations(order_list, stack_size)
145
+
146
+ for i in list(comb):
147
+ comb_list.append(i)
148
+
149
+ comb_DF = pd.DataFrame(comb_list)
150
+
151
+ if stack_size == 3:
152
+ comb_DF['Team'] = comb_DF[0].map(team_dict)
153
+
154
+ comb_DF['Proj'] = sum([comb_DF[0].map(proj_dict),
155
+ comb_DF[1].map(proj_dict),
156
+ comb_DF[2].map(proj_dict)])
157
+
158
+ comb_DF['Salary'] = sum([comb_DF[0].map(cost_dict),
159
+ comb_DF[1].map(cost_dict),
160
+ comb_DF[2].map(cost_dict)])
161
+
162
+ comb_DF['Own%'] = sum([comb_DF[0].map(own_dict),
163
+ comb_DF[1].map(own_dict),
164
+ comb_DF[2].map(own_dict)])
165
+ elif stack_size == 4:
166
+ comb_DF['Team'] = comb_DF[0].map(team_dict)
167
+
168
+ comb_DF['Proj'] = sum([comb_DF[0].map(proj_dict),
169
+ comb_DF[1].map(proj_dict),
170
+ comb_DF[2].map(proj_dict),
171
+ comb_DF[3].map(proj_dict)])
172
+
173
+ comb_DF['Salary'] = sum([comb_DF[0].map(cost_dict),
174
+ comb_DF[1].map(cost_dict),
175
+ comb_DF[2].map(cost_dict),
176
+ comb_DF[3].map(cost_dict)])
177
+
178
+ comb_DF['Own%'] = sum([comb_DF[0].map(own_dict),
179
+ comb_DF[1].map(own_dict),
180
+ comb_DF[2].map(own_dict),
181
+ comb_DF[3].map(own_dict)])
182
+ elif stack_size == 5:
183
+ comb_DF['Team'] = comb_DF[0].map(team_dict)
184
+
185
+ comb_DF['Proj'] = sum([comb_DF[0].map(proj_dict),
186
+ comb_DF[1].map(proj_dict),
187
+ comb_DF[2].map(proj_dict),
188
+ comb_DF[3].map(proj_dict),
189
+ comb_DF[4].map(proj_dict)])
190
+
191
+ comb_DF['Salary'] = sum([comb_DF[0].map(cost_dict),
192
+ comb_DF[1].map(cost_dict),
193
+ comb_DF[2].map(cost_dict),
194
+ comb_DF[3].map(cost_dict),
195
+ comb_DF[4].map(cost_dict)])
196
+
197
+ comb_DF['Own%'] = sum([comb_DF[0].map(own_dict),
198
+ comb_DF[1].map(own_dict),
199
+ comb_DF[2].map(own_dict),
200
+ comb_DF[3].map(own_dict),
201
+ comb_DF[4].map(own_dict)])
202
+
203
+ comb_DF = comb_DF.sort_values(by='Proj', ascending=False)
204
+ comb_DF = comb_DF.loc[comb_DF['Salary'] <= max_sal2]
205
+
206
+ cut_var = 0
207
+
208
+ if stack_size == 3:
209
+ while cut_var <= int(len(comb_DF)):
210
+ try:
211
+ if int(cut_var) == 0:
212
+ cur_proj = float(comb_DF.iat[cut_var,4])
213
+ cur_own = float(comb_DF.iat[cut_var,6])
214
+ elif int(cut_var) >= 1:
215
+ check_own = float(comb_DF.iat[cut_var,6])
216
+ if check_own > cur_own:
217
+ comb_DF = comb_DF.drop([cut_var])
218
+ cur_own = cur_own
219
+ cut_var = cut_var - 1
220
+ comb_DF = comb_DF.reset_index()
221
+ comb_DF = comb_DF.drop(['index'], axis=1)
222
+ elif check_own <= cur_own:
223
  cur_own = float(comb_DF.iat[cut_var,6])
224
+ cut_var = cut_var
225
+ cut_var += 1
226
+ except:
227
+ cut_var += 1
228
+ elif stack_size == 4:
229
+ while cut_var <= int(len(comb_DF)):
230
+ try:
231
+ if int(cut_var) == 0:
232
+ cur_proj = float(comb_DF.iat[cut_var,5])
233
+ cur_own = float(comb_DF.iat[cut_var,7])
234
+ elif int(cut_var) >= 1:
235
+ check_own = float(comb_DF.iat[cut_var,7])
236
+ if check_own > cur_own:
237
+ comb_DF = comb_DF.drop([cut_var])
238
+ cur_own = cur_own
239
+ cut_var = cut_var - 1
240
+ comb_DF = comb_DF.reset_index()
241
+ comb_DF = comb_DF.drop(['index'], axis=1)
242
+ elif check_own <= cur_own:
243
  cur_own = float(comb_DF.iat[cut_var,7])
244
+ cut_var = cut_var
245
+ cut_var += 1
246
+ except:
247
+ cut_var += 1
248
+ elif stack_size == 5:
249
+ while cut_var <= int(len(comb_DF)):
250
+ try:
251
+ if int(cut_var) == 0:
252
+ cur_proj = float(comb_DF.iat[cut_var,6])
253
+ cur_own = float(comb_DF.iat[cut_var,8])
254
+ elif int(cut_var) >= 1:
255
+ check_own = float(comb_DF.iat[cut_var,8])
256
+ if check_own > cur_own:
257
+ comb_DF = comb_DF.drop([cut_var])
258
+ cur_own = cur_own
259
+ cut_var = cut_var - 1
260
+ comb_DF = comb_DF.reset_index()
261
+ comb_DF = comb_DF.drop(['index'], axis=1)
262
+ elif check_own <= cur_own:
263
  cur_own = float(comb_DF.iat[cut_var,8])
264
+ cut_var = cut_var
265
+ cut_var += 1
266
+ except:
267
+ cut_var += 1
268
+
269
+ with stack_hold_container:
270
+ stack_hold_container = st.empty()
271
+ st.dataframe(comb_DF.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
272
+ st.download_button(
273
+ label="Export Tables",
274
+ data=convert_df_to_csv(comb_DF),
275
+ file_name='MLB_Stack_Options_export.csv',
276
+ mime='text/csv',
277
+ )