James McCool
commited on
Commit
·
2df0c40
1
Parent(s):
dc1c8da
Implement player portfolio filtering and display in `app.py`
Browse files- Added a new form for users to filter player data by game type and specific players, enhancing user interaction.
- Introduced logic to calculate salary, median projections, and ownership based on selected game type (Classic or Showdown).
- Updated the display of the portfolio dataframe with improved styling and pagination for better data visualization.
app.py
CHANGED
@@ -78,9 +78,70 @@ with tab1:
|
|
78 |
|
79 |
# Update projections_df with any new matches
|
80 |
st.session_state['contest_df'], st.session_state['projections_df'] = find_name_mismatches(st.session_state['Contest'], st.session_state['projections_df'])
|
81 |
-
st.dataframe(st.session_state['contest_df'].head(100))
|
82 |
-
st.dataframe(st.session_state['projections_df'].head(100))
|
83 |
|
84 |
with tab2:
|
85 |
if st.button('Clear data', key='reset3'):
|
86 |
st.session_state.clear()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
# Update projections_df with any new matches
|
80 |
st.session_state['contest_df'], st.session_state['projections_df'] = find_name_mismatches(st.session_state['Contest'], st.session_state['projections_df'])
|
|
|
|
|
81 |
|
82 |
with tab2:
|
83 |
if st.button('Clear data', key='reset3'):
|
84 |
st.session_state.clear()
|
85 |
+
if 'portfolio' in st.session_state and 'projections_df' in st.session_state:
|
86 |
+
col1, col2 = st.columns([1, 8])
|
87 |
+
excluded_cols = ['BaseName', 'EntryCount']
|
88 |
+
with col1:
|
89 |
+
with st.form(key='filter_form'):
|
90 |
+
type_var = st.selectbox("Select Game Type", ['Classic', 'Showdown'])
|
91 |
+
player_parse_var = st.selectbox("Do you want to view a specific player(s) or a group of players?", ['All', 'Specific'])
|
92 |
+
if player_parse_var == 'Specific':
|
93 |
+
player_names = st.multiselect("Select players", options=st.session_state['entry_list'])
|
94 |
+
else:
|
95 |
+
player_names = st.session_state['entry_list']
|
96 |
+
submitted = st.form_submit_button("Submit")
|
97 |
+
|
98 |
+
|
99 |
+
map_dict = {
|
100 |
+
'pos_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['position'])),
|
101 |
+
'team_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['team'])),
|
102 |
+
'salary_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'])),
|
103 |
+
'proj_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'])),
|
104 |
+
'own_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'])),
|
105 |
+
'own_percent_rank':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'].rank(pct=True))),
|
106 |
+
'cpt_salary_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'])),
|
107 |
+
'cpt_proj_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'] * 1.5)),
|
108 |
+
'cpt_own_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['captain ownership']))
|
109 |
+
}
|
110 |
+
|
111 |
+
if type_var == 'Classic':
|
112 |
+
st.session_state['portfolio']['salary'] = st.session_state['portfolio'].apply(lambda row: sum(map_dict['salary_map'].get(player, 0) for player in row), axis=1)
|
113 |
+
st.session_state['portfolio']['median'] = st.session_state['portfolio'].apply(lambda row: sum(map_dict['proj_map'].get(player, 0) for player in row), axis=1)
|
114 |
+
st.session_state['portfolio']['Own'] = st.session_state['portfolio'].apply(lambda row: sum(map_dict['own_map'].get(player, 0) for player in row), axis=1)
|
115 |
+
elif type_var == 'Showdown':
|
116 |
+
# Calculate salary (CPT uses cpt_salary_map, others use salary_map)
|
117 |
+
st.session_state['portfolio']['salary'] = st.session_state['portfolio'].apply(
|
118 |
+
lambda row: map_dict['cpt_salary_map'].get(row.iloc[0], 0) +
|
119 |
+
sum(map_dict['salary_map'].get(player, 0) for player in row.iloc[1:]),
|
120 |
+
axis=1
|
121 |
+
)
|
122 |
+
|
123 |
+
# Calculate median (CPT uses cpt_proj_map, others use proj_map)
|
124 |
+
st.session_state['portfolio']['median'] = st.session_state['portfolio'].apply(
|
125 |
+
lambda row: map_dict['cpt_proj_map'].get(row.iloc[0], 0) +
|
126 |
+
sum(map_dict['proj_map'].get(player, 0) for player in row.iloc[1:]),
|
127 |
+
axis=1
|
128 |
+
)
|
129 |
+
|
130 |
+
# Calculate ownership (CPT uses cpt_own_map, others use own_map)
|
131 |
+
st.session_state['portfolio']['Own'] = st.session_state['portfolio'].apply(
|
132 |
+
lambda row: map_dict['cpt_own_map'].get(row.iloc[0], 0) +
|
133 |
+
sum(map_dict['own_map'].get(player, 0) for player in row.iloc[1:]),
|
134 |
+
axis=1
|
135 |
+
)
|
136 |
+
|
137 |
+
with col2:
|
138 |
+
# Display the paginated dataframe first
|
139 |
+
st.dataframe(
|
140 |
+
st.session_state['portfolio'].style
|
141 |
+
.background_gradient(axis=0)
|
142 |
+
.background_gradient(cmap='RdYlGn')
|
143 |
+
.background_gradient(cmap='RdYlGn_r', subset=['Finish_percentile', 'Own', 'Dupes'])
|
144 |
+
.format(precision=2),
|
145 |
+
height=1000,
|
146 |
+
use_container_width=True
|
147 |
+
)
|