James McCool
commited on
Commit
·
76fbebc
1
Parent(s):
357bf18
testing the reoptimization stuff
Browse files
app.py
CHANGED
|
@@ -3042,6 +3042,10 @@ if selected_tab == 'Manage Portfolio':
|
|
| 3042 |
|
| 3043 |
if opt_reg_submitted:
|
| 3044 |
st.session_state['settings_base'] = False
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3045 |
# Run optimization on working_frame
|
| 3046 |
optimized_frame = optimize_lineup(
|
| 3047 |
working_frame=st.session_state['working_frame'],
|
|
@@ -3056,6 +3060,11 @@ if selected_tab == 'Manage Portfolio':
|
|
| 3056 |
optimize_by=optimize_by
|
| 3057 |
)
|
| 3058 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3059 |
# Recalculate lineup metrics
|
| 3060 |
optimized_frame = calculate_lineup_metrics(
|
| 3061 |
optimized_frame,
|
|
@@ -3103,6 +3112,10 @@ if selected_tab == 'Manage Portfolio':
|
|
| 3103 |
|
| 3104 |
elif opt_exp_submitted:
|
| 3105 |
st.session_state['settings_base'] = False
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3106 |
# Run optimization on export_base
|
| 3107 |
optimized_frame = optimize_lineup(
|
| 3108 |
working_frame=st.session_state['export_base'],
|
|
@@ -3117,6 +3130,11 @@ if selected_tab == 'Manage Portfolio':
|
|
| 3117 |
optimize_by=optimize_by
|
| 3118 |
)
|
| 3119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3120 |
# Recalculate lineup metrics for export
|
| 3121 |
optimized_frame = calculate_lineup_metrics(
|
| 3122 |
optimized_frame,
|
|
@@ -3161,6 +3179,11 @@ if selected_tab == 'Manage Portfolio':
|
|
| 3161 |
axis=1
|
| 3162 |
)
|
| 3163 |
st.session_state['export_merge'] = st.session_state['export_base'].copy()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3164 |
|
| 3165 |
with st.container():
|
| 3166 |
if 'export_base' not in st.session_state:
|
|
@@ -3207,15 +3230,15 @@ if selected_tab == 'Manage Portfolio':
|
|
| 3207 |
select_custom_index = st.multiselect("Select rows to add (based on first column):", options=st.session_state['display_frame'].index, default=[])
|
| 3208 |
if st.button("Add selected to Custom Export"):
|
| 3209 |
st.session_state['export_base'] = pd.concat([st.session_state['export_base'], st.session_state['display_frame'].loc[select_custom_index]])
|
| 3210 |
-
|
| 3211 |
-
|
| 3212 |
|
| 3213 |
with remove_rows_col:
|
| 3214 |
remove_custom_index = st.multiselect("Remove rows (based on first column):", options=st.session_state['display_frame'].index, default=[])
|
| 3215 |
if st.button("Remove selected from Display"):
|
| 3216 |
st.session_state['display_frame'] = st.session_state['display_frame'].drop(remove_custom_index)
|
| 3217 |
-
|
| 3218 |
-
|
| 3219 |
|
| 3220 |
|
| 3221 |
total_rows = len(st.session_state['display_frame'])
|
|
@@ -3246,10 +3269,24 @@ if selected_tab == 'Manage Portfolio':
|
|
| 3246 |
|
| 3247 |
# Get the subset of data for the current page
|
| 3248 |
current_page_data = st.session_state['display_frame'].iloc[start_idx:end_idx]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3249 |
# Display the paginated dataframe first
|
| 3250 |
st.dataframe(
|
| 3251 |
current_page_data.style
|
| 3252 |
-
.
|
| 3253 |
.background_gradient(cmap='RdYlGn')
|
| 3254 |
.background_gradient(cmap='RdYlGn_r', subset=['Finish_percentile', 'Own', 'Dupes'])
|
| 3255 |
.format(freq_format, precision=2),
|
|
|
|
| 3042 |
|
| 3043 |
if opt_reg_submitted:
|
| 3044 |
st.session_state['settings_base'] = False
|
| 3045 |
+
|
| 3046 |
+
# Store original for comparison (player columns only)
|
| 3047 |
+
original_frame = st.session_state['working_frame'][st.session_state['player_columns']].copy()
|
| 3048 |
+
|
| 3049 |
# Run optimization on working_frame
|
| 3050 |
optimized_frame = optimize_lineup(
|
| 3051 |
working_frame=st.session_state['working_frame'],
|
|
|
|
| 3060 |
optimize_by=optimize_by
|
| 3061 |
)
|
| 3062 |
|
| 3063 |
+
# Store changes mask in session state for highlighting (no columns added)
|
| 3064 |
+
st.session_state['optimization_changes_mask'] = (
|
| 3065 |
+
original_frame != optimized_frame[st.session_state['player_columns']]
|
| 3066 |
+
)
|
| 3067 |
+
|
| 3068 |
# Recalculate lineup metrics
|
| 3069 |
optimized_frame = calculate_lineup_metrics(
|
| 3070 |
optimized_frame,
|
|
|
|
| 3112 |
|
| 3113 |
elif opt_exp_submitted:
|
| 3114 |
st.session_state['settings_base'] = False
|
| 3115 |
+
|
| 3116 |
+
# Store original for comparison (player columns only)
|
| 3117 |
+
original_frame = st.session_state['export_base'][st.session_state['player_columns']].copy()
|
| 3118 |
+
|
| 3119 |
# Run optimization on export_base
|
| 3120 |
optimized_frame = optimize_lineup(
|
| 3121 |
working_frame=st.session_state['export_base'],
|
|
|
|
| 3130 |
optimize_by=optimize_by
|
| 3131 |
)
|
| 3132 |
|
| 3133 |
+
# Store changes mask in session state for highlighting (no columns added)
|
| 3134 |
+
st.session_state['optimization_changes_mask'] = (
|
| 3135 |
+
original_frame != optimized_frame[st.session_state['player_columns']]
|
| 3136 |
+
)
|
| 3137 |
+
|
| 3138 |
# Recalculate lineup metrics for export
|
| 3139 |
optimized_frame = calculate_lineup_metrics(
|
| 3140 |
optimized_frame,
|
|
|
|
| 3179 |
axis=1
|
| 3180 |
)
|
| 3181 |
st.session_state['export_merge'] = st.session_state['export_base'].copy()
|
| 3182 |
+
|
| 3183 |
+
# Clear highlighting button (outside the form)
|
| 3184 |
+
if st.button("Clear Optimization Highlighting", key='clear_opt_highlight'):
|
| 3185 |
+
if 'optimization_changes_mask' in st.session_state:
|
| 3186 |
+
del st.session_state['optimization_changes_mask']
|
| 3187 |
|
| 3188 |
with st.container():
|
| 3189 |
if 'export_base' not in st.session_state:
|
|
|
|
| 3230 |
select_custom_index = st.multiselect("Select rows to add (based on first column):", options=st.session_state['display_frame'].index, default=[])
|
| 3231 |
if st.button("Add selected to Custom Export"):
|
| 3232 |
st.session_state['export_base'] = pd.concat([st.session_state['export_base'], st.session_state['display_frame'].loc[select_custom_index]])
|
| 3233 |
+
st.session_state['export_base'] = st.session_state['export_base'].drop_duplicates()
|
| 3234 |
+
st.session_state['export_base'] = st.session_state['export_base'].reset_index(drop=True)
|
| 3235 |
|
| 3236 |
with remove_rows_col:
|
| 3237 |
remove_custom_index = st.multiselect("Remove rows (based on first column):", options=st.session_state['display_frame'].index, default=[])
|
| 3238 |
if st.button("Remove selected from Display"):
|
| 3239 |
st.session_state['display_frame'] = st.session_state['display_frame'].drop(remove_custom_index)
|
| 3240 |
+
st.session_state['display_frame'] = st.session_state['display_frame'].drop_duplicates()
|
| 3241 |
+
st.session_state['display_frame'] = st.session_state['display_frame'].reset_index(drop=True)
|
| 3242 |
|
| 3243 |
|
| 3244 |
total_rows = len(st.session_state['display_frame'])
|
|
|
|
| 3269 |
|
| 3270 |
# Get the subset of data for the current page
|
| 3271 |
current_page_data = st.session_state['display_frame'].iloc[start_idx:end_idx]
|
| 3272 |
+
|
| 3273 |
+
# Define highlight function for optimization changes
|
| 3274 |
+
def highlight_optimization_changes(df):
|
| 3275 |
+
styles = pd.DataFrame('', index=df.index, columns=df.columns)
|
| 3276 |
+
if 'optimization_changes_mask' in st.session_state:
|
| 3277 |
+
mask = st.session_state['optimization_changes_mask']
|
| 3278 |
+
for col in mask.columns:
|
| 3279 |
+
if col in styles.columns:
|
| 3280 |
+
common_idx = mask.index.intersection(df.index)
|
| 3281 |
+
for idx in common_idx:
|
| 3282 |
+
if mask.loc[idx, col]:
|
| 3283 |
+
styles.loc[idx, col] = 'background-color: #90EE90'
|
| 3284 |
+
return styles
|
| 3285 |
+
|
| 3286 |
# Display the paginated dataframe first
|
| 3287 |
st.dataframe(
|
| 3288 |
current_page_data.style
|
| 3289 |
+
.apply(highlight_optimization_changes, axis=None)
|
| 3290 |
.background_gradient(cmap='RdYlGn')
|
| 3291 |
.background_gradient(cmap='RdYlGn_r', subset=['Finish_percentile', 'Own', 'Dupes'])
|
| 3292 |
.format(freq_format, precision=2),
|