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
app.py
CHANGED
@@ -128,151 +128,150 @@ with col1:
|
|
128 |
|
129 |
with col2:
|
130 |
stack_hold_container = st.empty()
|
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 |
cur_own = float(comb_DF.iat[cut_var,6])
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
cur_own = float(comb_DF.iat[cut_var,7])
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
cur_own = float(comb_DF.iat[cut_var,8])
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
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 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|