James McCool
commited on
Commit
·
2c57866
1
Parent(s):
e24862c
Refactor player filtering and calculation logic in app.py for improved functionality
Browse files- Reorganized the player selection form to ensure filters are applied only after submission, enhancing user experience.
- Updated the logic for applying filters and calculating metrics based on game type, ensuring accurate data processing.
- Reset pagination when new filters are applied to maintain consistency in data display.
app.py
CHANGED
|
@@ -84,13 +84,7 @@ with tab2:
|
|
| 84 |
if 'contest_df' in st.session_state and 'projections_df' in st.session_state:
|
| 85 |
col1, col2 = st.columns([1, 8])
|
| 86 |
excluded_cols = ['BaseName', 'EntryCount']
|
| 87 |
-
|
| 88 |
-
with st.form(key='filter_form'):
|
| 89 |
-
type_var = st.selectbox("Select Game Type", ['Classic', 'Showdown'])
|
| 90 |
-
entry_parse_var = st.selectbox("Do you want to view a specific player(s) or a group of players?", ['All', 'Specific'])
|
| 91 |
-
entry_names = st.multiselect("Select players", options=st.session_state['entry_list'], default=[])
|
| 92 |
-
submitted = st.form_submit_button("Submit")
|
| 93 |
-
|
| 94 |
# Create mapping dictionaries
|
| 95 |
map_dict = {
|
| 96 |
'pos_map': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['position'])),
|
|
@@ -104,34 +98,46 @@ with tab2:
|
|
| 104 |
'cpt_own_map': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['captain ownership']))
|
| 105 |
}
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
# Create a copy of the dataframe for calculations
|
| 108 |
working_df = st.session_state['contest_df'].copy()
|
| 109 |
|
| 110 |
-
# Apply filters
|
| 111 |
-
if submitted
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
# Initialize pagination in session state if not exists
|
| 137 |
if 'current_page' not in st.session_state:
|
|
|
|
| 84 |
if 'contest_df' in st.session_state and 'projections_df' in st.session_state:
|
| 85 |
col1, col2 = st.columns([1, 8])
|
| 86 |
excluded_cols = ['BaseName', 'EntryCount']
|
| 87 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
# Create mapping dictionaries
|
| 89 |
map_dict = {
|
| 90 |
'pos_map': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['position'])),
|
|
|
|
| 98 |
'cpt_own_map': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['captain ownership']))
|
| 99 |
}
|
| 100 |
|
| 101 |
+
with col1:
|
| 102 |
+
with st.form(key='filter_form'):
|
| 103 |
+
type_var = st.selectbox("Select Game Type", ['Classic', 'Showdown'])
|
| 104 |
+
entry_parse_var = st.selectbox("Do you want to view a specific player(s) or a group of players?", ['All', 'Specific'])
|
| 105 |
+
entry_names = st.multiselect("Select players", options=st.session_state['entry_list'], default=[])
|
| 106 |
+
submitted = st.form_submit_button("Submit")
|
| 107 |
+
|
| 108 |
# Create a copy of the dataframe for calculations
|
| 109 |
working_df = st.session_state['contest_df'].copy()
|
| 110 |
|
| 111 |
+
# Apply filters and calculations only after form submission
|
| 112 |
+
if submitted:
|
| 113 |
+
# Apply entry name filter if specific entries are selected
|
| 114 |
+
if entry_parse_var == 'Specific' and entry_names:
|
| 115 |
+
working_df = working_df[working_df['BaseName'].isin(entry_names)]
|
| 116 |
+
|
| 117 |
+
# Calculate metrics based on game type
|
| 118 |
+
if type_var == 'Classic':
|
| 119 |
+
working_df['salary'] = working_df.apply(lambda row: sum(map_dict['salary_map'].get(player, 0) for player in row), axis=1)
|
| 120 |
+
working_df['median'] = working_df.apply(lambda row: sum(map_dict['proj_map'].get(player, 0) for player in row), axis=1)
|
| 121 |
+
working_df['Own'] = working_df.apply(lambda row: sum(map_dict['own_map'].get(player, 0) for player in row), axis=1)
|
| 122 |
+
elif type_var == 'Showdown':
|
| 123 |
+
working_df['salary'] = working_df.apply(
|
| 124 |
+
lambda row: map_dict['cpt_salary_map'].get(row.iloc[0], 0) +
|
| 125 |
+
sum(map_dict['salary_map'].get(player, 0) for player in row.iloc[1:]),
|
| 126 |
+
axis=1
|
| 127 |
+
)
|
| 128 |
+
working_df['median'] = working_df.apply(
|
| 129 |
+
lambda row: map_dict['cpt_proj_map'].get(row.iloc[0], 0) +
|
| 130 |
+
sum(map_dict['proj_map'].get(player, 0) for player in row.iloc[1:]),
|
| 131 |
+
axis=1
|
| 132 |
+
)
|
| 133 |
+
working_df['Own'] = working_df.apply(
|
| 134 |
+
lambda row: map_dict['cpt_own_map'].get(row.iloc[0], 0) +
|
| 135 |
+
sum(map_dict['own_map'].get(player, 0) for player in row.iloc[1:]),
|
| 136 |
+
axis=1
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
# Reset pagination when new filters are applied
|
| 140 |
+
st.session_state.current_page = 0
|
| 141 |
|
| 142 |
# Initialize pagination in session state if not exists
|
| 143 |
if 'current_page' not in st.session_state:
|