James McCool
commited on
Commit
·
1854e4d
1
Parent(s):
7adbc9e
Refactor app.py to improve session state handling and user interaction
Browse files- Streamlined the management of session state variables in app.py, specifically focusing on the handling of 'Contest' and 'projections_df'.
- Enhanced the layout for user filters and data clearing options, ensuring a more intuitive user experience.
- Maintained the functionality of 'calc_toggle' to track calculation states effectively during user interactions.
app.py
CHANGED
@@ -86,46 +86,45 @@ with tab2:
|
|
86 |
st.write(st.session_state['calc_toggle'])
|
87 |
st.write(st.session_state['projections_df'])
|
88 |
st.write(st.session_state['Contest'])
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
# Create a copy of the dataframe for calculations
|
109 |
-
working_df = st.session_state['Contest'].copy()
|
110 |
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
|
|
|
129 |
if type_var == 'Classic':
|
130 |
working_df['stack'] = working_df.apply(
|
131 |
lambda row: Counter(
|
|
|
86 |
st.write(st.session_state['calc_toggle'])
|
87 |
st.write(st.session_state['projections_df'])
|
88 |
st.write(st.session_state['Contest'])
|
89 |
+
excluded_cols = ['BaseName', 'EntryCount']
|
90 |
+
player_columns = [col for col in st.session_state['Contest'].columns if col not in excluded_cols]
|
91 |
+
for col in player_columns:
|
92 |
+
st.session_state['Contest'][col] = st.session_state['Contest'][col].astype(str)
|
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'])),
|
97 |
+
'team_map': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['team'])),
|
98 |
+
'salary_map': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'])),
|
99 |
+
'proj_map': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'])),
|
100 |
+
'own_map': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'])),
|
101 |
+
'own_percent_rank': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'].rank(pct=True))),
|
102 |
+
'cpt_salary_map': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'])),
|
103 |
+
'cpt_proj_map': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'] * 1.5)),
|
104 |
+
'cpt_own_map': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['captain ownership']))
|
105 |
+
}
|
106 |
+
# Create a copy of the dataframe for calculations
|
107 |
+
working_df = st.session_state['Contest'].copy()
|
|
|
|
|
108 |
|
109 |
+
with st.expander("Info and filters"):
|
110 |
+
if st.button('Clear data', key='reset3'):
|
111 |
+
st.session_state.clear()
|
112 |
+
with st.form(key='filter_form'):
|
113 |
+
type_var = st.selectbox("Select Game Type", ['Classic', 'Showdown'])
|
114 |
+
entry_parse_var = st.selectbox("Do you want to view a specific player(s) or a group of players?", ['All', 'Specific'])
|
115 |
+
entry_names = st.multiselect("Select players", options=st.session_state['entry_list'], default=[])
|
116 |
+
submitted = st.form_submit_button("Submit")
|
117 |
+
if submitted:
|
118 |
+
if 'player_frame' in st.session_state:
|
119 |
+
del st.session_state['player_frame']
|
120 |
+
if 'stack_frame' in st.session_state:
|
121 |
+
del st.session_state['stack_frame']
|
122 |
+
# Apply entry name filter if specific entries are selected
|
123 |
+
if entry_parse_var == 'Specific' and entry_names:
|
124 |
+
working_df = working_df[working_df['BaseName'].isin(entry_names)]
|
125 |
+
st.session_state['calc_toggle'] = True
|
126 |
|
127 |
+
if 'Contest' in st.session_state and 'projections_df' in st.session_state and st.session_state['calc_toggle']:
|
128 |
if type_var == 'Classic':
|
129 |
working_df['stack'] = working_df.apply(
|
130 |
lambda row: Counter(
|