Spaces:
Running
Running
James McCool
commited on
Commit
·
9274561
1
Parent(s):
633710b
Implement lineup management in Handbuilder tab of app.py by introducing a session state for the lineup, allowing players to be added without duplicates. Update summary calculations to reflect the current lineup and add a "Clear Lineup" button for user convenience, enhancing overall functionality and user experience.
Browse files
app.py
CHANGED
@@ -802,9 +802,6 @@ with tab3:
|
|
802 |
with tab4:
|
803 |
st.header("Handbuilder")
|
804 |
|
805 |
-
if st.button("Clear Data", key='clear_handbuild'):
|
806 |
-
st.rerun()
|
807 |
-
|
808 |
# --- TEAM FILTER UI ---
|
809 |
all_teams = sorted(dk_roo['Team'].unique())
|
810 |
st.markdown("**Toggle teams to include:**")
|
@@ -827,34 +824,48 @@ with tab4:
|
|
827 |
else:
|
828 |
player_select_df = dk_roo[['Player', 'Position', 'Team', 'Salary', 'Median', 'Order', 'Hand', 'Own%']].copy()
|
829 |
|
|
|
|
|
|
|
|
|
830 |
col1, col2 = st.columns([1, 2])
|
831 |
with col2:
|
832 |
st.subheader("Player Select")
|
833 |
event = st.dataframe(
|
834 |
player_select_df,
|
835 |
on_select="rerun",
|
836 |
-
selection_mode=["
|
837 |
)
|
838 |
|
839 |
-
|
840 |
-
|
841 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
842 |
|
843 |
with col1:
|
844 |
st.subheader("Lineup")
|
845 |
st.dataframe(
|
846 |
-
|
847 |
use_container_width=True,
|
848 |
height=300,
|
849 |
hide_index=True
|
850 |
)
|
851 |
|
852 |
# --- SUMMARY ROW ---
|
853 |
-
|
854 |
-
|
855 |
-
|
856 |
-
|
857 |
-
|
|
|
858 |
|
859 |
summary_row = pd.DataFrame({
|
860 |
'Player': ['TOTAL'],
|
@@ -863,11 +874,16 @@ with tab4:
|
|
863 |
'Median': [total_median],
|
864 |
'Own%': [total_own]
|
865 |
})
|
866 |
-
summary_row = summary_row[
|
867 |
|
868 |
st.dataframe(
|
869 |
summary_row,
|
870 |
use_container_width=True,
|
871 |
height=50,
|
872 |
hide_index=True
|
873 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
802 |
with tab4:
|
803 |
st.header("Handbuilder")
|
804 |
|
|
|
|
|
|
|
805 |
# --- TEAM FILTER UI ---
|
806 |
all_teams = sorted(dk_roo['Team'].unique())
|
807 |
st.markdown("**Toggle teams to include:**")
|
|
|
824 |
else:
|
825 |
player_select_df = dk_roo[['Player', 'Position', 'Team', 'Salary', 'Median', 'Order', 'Hand', 'Own%']].copy()
|
826 |
|
827 |
+
# --- LINEUP STATE ---
|
828 |
+
if 'handbuilder_lineup' not in st.session_state:
|
829 |
+
st.session_state['handbuilder_lineup'] = pd.DataFrame(columns=['Player', 'Team', 'Salary', 'Median', 'Own%'])
|
830 |
+
|
831 |
col1, col2 = st.columns([1, 2])
|
832 |
with col2:
|
833 |
st.subheader("Player Select")
|
834 |
event = st.dataframe(
|
835 |
player_select_df,
|
836 |
on_select="rerun",
|
837 |
+
selection_mode=["single-row"]
|
838 |
)
|
839 |
|
840 |
+
# If a row is selected, add that player to the lineup
|
841 |
+
if event and "rows" in event.selection and len(event.selection["rows"]) > 0:
|
842 |
+
idx = event.selection["rows"][0]
|
843 |
+
player_row = player_select_df.iloc[[idx]][['Player', 'Team', 'Salary', 'Median', 'Own%']]
|
844 |
+
# Avoid duplicates
|
845 |
+
if not player_row['Player'].iloc[0] in st.session_state['handbuilder_lineup']['Player'].values:
|
846 |
+
st.session_state['handbuilder_lineup'] = pd.concat(
|
847 |
+
[st.session_state['handbuilder_lineup'], player_row],
|
848 |
+
ignore_index=True
|
849 |
+
)
|
850 |
+
# Rerun to reset selection
|
851 |
+
st.rerun()
|
852 |
|
853 |
with col1:
|
854 |
st.subheader("Lineup")
|
855 |
st.dataframe(
|
856 |
+
st.session_state['handbuilder_lineup'],
|
857 |
use_container_width=True,
|
858 |
height=300,
|
859 |
hide_index=True
|
860 |
)
|
861 |
|
862 |
# --- SUMMARY ROW ---
|
863 |
+
lineup = st.session_state['handbuilder_lineup']
|
864 |
+
if not lineup.empty:
|
865 |
+
total_salary = lineup['Salary'].sum()
|
866 |
+
total_median = lineup['Median'].sum()
|
867 |
+
total_own = lineup['Own%'].sum()
|
868 |
+
most_common_team = lineup['Team'].mode()[0] if not lineup['Team'].mode().empty else ""
|
869 |
|
870 |
summary_row = pd.DataFrame({
|
871 |
'Player': ['TOTAL'],
|
|
|
874 |
'Median': [total_median],
|
875 |
'Own%': [total_own]
|
876 |
})
|
877 |
+
summary_row = summary_row[lineup.columns]
|
878 |
|
879 |
st.dataframe(
|
880 |
summary_row,
|
881 |
use_container_width=True,
|
882 |
height=50,
|
883 |
hide_index=True
|
884 |
+
)
|
885 |
+
|
886 |
+
# Optionally, add a button to clear the lineup
|
887 |
+
if st.button("Clear Lineup", key='clear_lineup'):
|
888 |
+
st.session_state['handbuilder_lineup'] = pd.DataFrame(columns=['Player', 'Team', 'Salary', 'Median', 'Own%'])
|
889 |
+
st.rerun()
|