James McCool
commited on
Commit
·
6d04e58
1
Parent(s):
ada082f
Refactor contest data handling in app.py for improved clarity and functionality
Browse files- Updated references from 'contest_df' to 'Contest' in session state to maintain consistency with naming conventions.
- Enhanced the logic for applying filters and calculating metrics based on game type, ensuring accurate data processing.
- Streamlined the process of creating a working copy of the contest dataframe for calculations, improving data handling and user experience.
app.py
CHANGED
|
@@ -76,12 +76,12 @@ with tab1:
|
|
| 76 |
st.session_state['projections_df'] = projections.copy()
|
| 77 |
st.session_state['projections_df']['salary'] = (st.session_state['projections_df']['salary'].astype(str).str.replace(',', '').astype(float).astype(int))
|
| 78 |
# Run name matching only once when first loading the files
|
| 79 |
-
st.session_state['
|
| 80 |
|
| 81 |
with tab2:
|
| 82 |
if st.button('Clear data', key='reset3'):
|
| 83 |
st.session_state.clear()
|
| 84 |
-
if '
|
| 85 |
col1, col2 = st.columns([1, 8])
|
| 86 |
excluded_cols = ['BaseName', 'EntryCount']
|
| 87 |
|
|
@@ -97,6 +97,8 @@ with tab2:
|
|
| 97 |
'cpt_proj_map': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'] * 1.5)),
|
| 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'):
|
|
@@ -104,40 +106,32 @@ with tab2:
|
|
| 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 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 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:
|
|
|
|
| 76 |
st.session_state['projections_df'] = projections.copy()
|
| 77 |
st.session_state['projections_df']['salary'] = (st.session_state['projections_df']['salary'].astype(str).str.replace(',', '').astype(float).astype(int))
|
| 78 |
# Run name matching only once when first loading the files
|
| 79 |
+
st.session_state['Contest'], st.session_state['projections_df'] = find_name_mismatches(st.session_state['Contest'], st.session_state['projections_df'])
|
| 80 |
|
| 81 |
with tab2:
|
| 82 |
if st.button('Clear data', key='reset3'):
|
| 83 |
st.session_state.clear()
|
| 84 |
+
if 'Contest' in st.session_state and 'projections_df' in st.session_state:
|
| 85 |
col1, col2 = st.columns([1, 8])
|
| 86 |
excluded_cols = ['BaseName', 'EntryCount']
|
| 87 |
|
|
|
|
| 97 |
'cpt_proj_map': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'] * 1.5)),
|
| 98 |
'cpt_own_map': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['captain ownership']))
|
| 99 |
}
|
| 100 |
+
# Create a copy of the dataframe for calculations
|
| 101 |
+
working_df = st.session_state['Contest'].copy()
|
| 102 |
|
| 103 |
with col1:
|
| 104 |
with st.form(key='filter_form'):
|
|
|
|
| 106 |
entry_parse_var = st.selectbox("Do you want to view a specific player(s) or a group of players?", ['All', 'Specific'])
|
| 107 |
entry_names = st.multiselect("Select players", options=st.session_state['entry_list'], default=[])
|
| 108 |
submitted = st.form_submit_button("Submit")
|
| 109 |
+
if submitted:
|
| 110 |
+
# Apply entry name filter if specific entries are selected
|
| 111 |
+
if entry_parse_var == 'Specific' and entry_names:
|
| 112 |
+
working_df = working_df[working_df['BaseName'].isin(entry_names)]
|
| 113 |
+
|
| 114 |
+
# Calculate metrics based on game type
|
| 115 |
+
if type_var == 'Classic':
|
| 116 |
+
working_df['salary'] = working_df.apply(lambda row: sum(map_dict['salary_map'].get(player, 0) for player in row), axis=1)
|
| 117 |
+
working_df['median'] = working_df.apply(lambda row: sum(map_dict['proj_map'].get(player, 0) for player in row), axis=1)
|
| 118 |
+
working_df['Own'] = working_df.apply(lambda row: sum(map_dict['own_map'].get(player, 0) for player in row), axis=1)
|
| 119 |
+
elif type_var == 'Showdown':
|
| 120 |
+
working_df['salary'] = working_df.apply(
|
| 121 |
+
lambda row: map_dict['cpt_salary_map'].get(row.iloc[0], 0) +
|
| 122 |
+
sum(map_dict['salary_map'].get(player, 0) for player in row.iloc[1:]),
|
| 123 |
+
axis=1
|
| 124 |
+
)
|
| 125 |
+
working_df['median'] = working_df.apply(
|
| 126 |
+
lambda row: map_dict['cpt_proj_map'].get(row.iloc[0], 0) +
|
| 127 |
+
sum(map_dict['proj_map'].get(player, 0) for player in row.iloc[1:]),
|
| 128 |
+
axis=1
|
| 129 |
+
)
|
| 130 |
+
working_df['Own'] = working_df.apply(
|
| 131 |
+
lambda row: map_dict['cpt_own_map'].get(row.iloc[0], 0) +
|
| 132 |
+
sum(map_dict['own_map'].get(player, 0) for player in row.iloc[1:]),
|
| 133 |
+
axis=1
|
| 134 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
# Initialize pagination in session state if not exists
|
| 137 |
if 'current_page' not in st.session_state:
|