James McCool commited on
Commit
e39607b
·
1 Parent(s): 0157a17

Refactor download options in app.py: comment out the previous download options logic for clarity, and implement a new selection mechanism for displaying either the portfolio or export base, enhancing user experience and functionality in data export processes.

Browse files
Files changed (1) hide show
  1. app.py +95 -80
app.py CHANGED
@@ -1002,100 +1002,115 @@ with tab2:
1002
  st.session_state['export_merge'] = st.session_state['working_frame'].copy()
1003
 
1004
  with col2:
1005
- with st.expander("Download options"):
1006
- if stack_dict is not None:
1007
- download_type = st.selectbox("Simple or Advanced Download?", options=['Simple', 'Advanced'], key='download_choice')
1008
- if download_type == 'Simple':
1009
- st.session_state['export_file'] = st.session_state['working_frame'].copy()
1010
- for col in st.session_state['export_file'].columns:
1011
- if col not in excluded_cols:
1012
- st.session_state['export_file'][col] = st.session_state['export_file'][col].map(st.session_state['export_dict'])
1013
- else:
1014
- with st.form(key='stack_form'):
1015
- st.subheader("Stack Count Adjustments")
1016
- st.info("This allows you to fine tune the stacks that you wish to export. If you want to make sure you don't export any of a specific stack you can 0 it out.")
1017
- # Create a container for stack value inputs
1018
- sort_container = st.container()
1019
- with sort_container:
1020
- sort_var = st.selectbox("Sort export portfolio by:", options=['median', 'Lineup Edge', 'Own'])
1021
 
1022
- # Get unique stack values
1023
- unique_stacks = sorted(list(set(stack_dict.values())))
1024
 
1025
- # Create a dictionary to store stack multipliers
1026
- if 'stack_multipliers' not in st.session_state:
1027
- st.session_state.stack_multipliers = {stack: 0.0 for stack in unique_stacks}
1028
 
1029
- # Create columns for the stack inputs
1030
- num_cols = 6 # Number of columns to display
1031
- for i in range(0, len(unique_stacks), num_cols):
1032
- cols = st.columns(num_cols)
1033
- for j, stack in enumerate(unique_stacks[i:i+num_cols]):
1034
- with cols[j]:
1035
- # Create a unique key for each number input
1036
- key = f"stack_count_{stack}"
1037
- # Get the current count of this stack in the portfolio
1038
- current_stack_count = len(st.session_state['working_frame'][st.session_state['working_frame']['Stack'] == stack])
1039
- # Create number input with current value and max value based on actual count
1040
- st.session_state.stack_multipliers[stack] = st.number_input(
1041
- f"{stack} count",
1042
- min_value=0.0,
1043
- max_value=float(current_stack_count),
1044
- value=0.0,
1045
- step=1.0,
1046
- key=key
1047
- )
1048
 
1049
- portfolio_copy = st.session_state['working_frame'].copy()
1050
 
1051
- submitted = st.form_submit_button("Submit")
1052
- if submitted:
1053
- # Create a list to store selected rows
1054
- selected_rows = []
1055
 
1056
- # For each stack, select the top N rows based on the count value
1057
- for stack in unique_stacks:
1058
- if stack in st.session_state.stack_multipliers:
1059
- count = int(st.session_state.stack_multipliers[stack])
1060
- # Get rows for this stack
1061
- stack_rows = portfolio_copy[portfolio_copy['Stack'] == stack]
1062
- # Sort by median and take top N rows
1063
- top_rows = stack_rows.nlargest(count, sort_var)
1064
- selected_rows.append(top_rows)
1065
-
1066
- # Combine all selected rows
1067
- portfolio_concat = pd.concat(selected_rows)
1068
 
1069
- # Update export_file with filtered data
1070
- st.session_state['export_file'] = portfolio_concat.copy()
1071
- for col in st.session_state['export_file'].columns:
1072
- if col not in excluded_cols:
1073
- st.session_state['export_file'][col] = st.session_state['export_file'][col].map(st.session_state['export_dict'])
1074
- st.write('Export portfolio updated!')
1075
- else:
1076
- st.session_state['export_file'] = st.session_state['working_frame'].copy()
1077
- if 'export_base' not in st.session_state:
1078
- st.session_state['export_base'] = pd.DataFrame(columns=st.session_state['working_frame'].columns)
1079
- for col in st.session_state['export_file'].columns:
1080
- if col not in excluded_cols:
1081
- st.session_state['export_file'][col] = st.session_state['export_file'][col].map(st.session_state['export_dict'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1082
  if 'export_file' in st.session_state:
1083
- download_port, merge_port, blank_export_col = st.columns([1, 1, 8])
1084
  with download_port:
1085
  st.download_button(label="Download Portfolio", data=st.session_state['export_file'].to_csv(index=False), file_name="portfolio.csv", mime="text/csv")
1086
  with merge_port:
1087
- if st.button("Add to export"):
1088
  st.session_state['export_base'] = pd.concat([st.session_state['export_base'], st.session_state['export_merge']])
1089
  st.session_state['export_base'] = st.session_state['export_base'].drop_duplicates()
1090
  st.session_state['export_base'] = st.session_state['export_base'].reset_index(drop=True)
1091
- else:
1092
- st.error("No portfolio to download")
1093
-
1094
- display_frame_source = st.selectbox("Display:", options=['Portfolio', 'Export Base'], key='display_frame_source')
1095
- if display_frame_source == 'Portfolio':
1096
- display_frame = st.session_state['working_frame']
1097
- elif display_frame_source == 'Export Base':
1098
- display_frame = st.session_state['export_base']
1099
  total_rows = len(display_frame)
1100
  rows_per_page = 500
1101
  total_pages = (total_rows + rows_per_page - 1) // rows_per_page # Ceiling division
 
1002
  st.session_state['export_merge'] = st.session_state['working_frame'].copy()
1003
 
1004
  with col2:
1005
+ # with st.expander("Download options"):
1006
+ # if stack_dict is not None:
1007
+ # download_type = st.selectbox("Simple or Advanced Download?", options=['Simple', 'Advanced'], key='download_choice')
1008
+ # if download_type == 'Simple':
1009
+ # st.session_state['export_file'] = st.session_state['working_frame'].copy()
1010
+ # for col in st.session_state['export_file'].columns:
1011
+ # if col not in excluded_cols:
1012
+ # st.session_state['export_file'][col] = st.session_state['export_file'][col].map(st.session_state['export_dict'])
1013
+ # else:
1014
+ # with st.form(key='stack_form'):
1015
+ # st.subheader("Stack Count Adjustments")
1016
+ # st.info("This allows you to fine tune the stacks that you wish to export. If you want to make sure you don't export any of a specific stack you can 0 it out.")
1017
+ # # Create a container for stack value inputs
1018
+ # sort_container = st.container()
1019
+ # with sort_container:
1020
+ # sort_var = st.selectbox("Sort export portfolio by:", options=['median', 'Lineup Edge', 'Own'])
1021
 
1022
+ # # Get unique stack values
1023
+ # unique_stacks = sorted(list(set(stack_dict.values())))
1024
 
1025
+ # # Create a dictionary to store stack multipliers
1026
+ # if 'stack_multipliers' not in st.session_state:
1027
+ # st.session_state.stack_multipliers = {stack: 0.0 for stack in unique_stacks}
1028
 
1029
+ # # Create columns for the stack inputs
1030
+ # num_cols = 6 # Number of columns to display
1031
+ # for i in range(0, len(unique_stacks), num_cols):
1032
+ # cols = st.columns(num_cols)
1033
+ # for j, stack in enumerate(unique_stacks[i:i+num_cols]):
1034
+ # with cols[j]:
1035
+ # # Create a unique key for each number input
1036
+ # key = f"stack_count_{stack}"
1037
+ # # Get the current count of this stack in the portfolio
1038
+ # current_stack_count = len(st.session_state['working_frame'][st.session_state['working_frame']['Stack'] == stack])
1039
+ # # Create number input with current value and max value based on actual count
1040
+ # st.session_state.stack_multipliers[stack] = st.number_input(
1041
+ # f"{stack} count",
1042
+ # min_value=0.0,
1043
+ # max_value=float(current_stack_count),
1044
+ # value=0.0,
1045
+ # step=1.0,
1046
+ # key=key
1047
+ # )
1048
 
1049
+ # portfolio_copy = st.session_state['working_frame'].copy()
1050
 
1051
+ # submitted = st.form_submit_button("Submit")
1052
+ # if submitted:
1053
+ # # Create a list to store selected rows
1054
+ # selected_rows = []
1055
 
1056
+ # # For each stack, select the top N rows based on the count value
1057
+ # for stack in unique_stacks:
1058
+ # if stack in st.session_state.stack_multipliers:
1059
+ # count = int(st.session_state.stack_multipliers[stack])
1060
+ # # Get rows for this stack
1061
+ # stack_rows = portfolio_copy[portfolio_copy['Stack'] == stack]
1062
+ # # Sort by median and take top N rows
1063
+ # top_rows = stack_rows.nlargest(count, sort_var)
1064
+ # selected_rows.append(top_rows)
1065
+
1066
+ # # Combine all selected rows
1067
+ # portfolio_concat = pd.concat(selected_rows)
1068
 
1069
+ # # Update export_file with filtered data
1070
+ # st.session_state['export_file'] = portfolio_concat.copy()
1071
+ # for col in st.session_state['export_file'].columns:
1072
+ # if col not in excluded_cols:
1073
+ # st.session_state['export_file'][col] = st.session_state['export_file'][col].map(st.session_state['export_dict'])
1074
+ # st.write('Export portfolio updated!')
1075
+ # else:
1076
+ # st.session_state['export_file'] = st.session_state['working_frame'].copy()
1077
+ # if 'export_base' not in st.session_state:
1078
+ # st.session_state['export_base'] = pd.DataFrame(columns=st.session_state['working_frame'].columns)
1079
+ # for col in st.session_state['export_file'].columns:
1080
+ # if col not in excluded_cols:
1081
+ # st.session_state['export_file'][col] = st.session_state['export_file'][col].map(st.session_state['export_dict'])
1082
+ if 'export_base' not in st.session_state:
1083
+ st.session_state['export_base'] = pd.DataFrame(columns=st.session_state['working_frame'].columns)
1084
+
1085
+ display_frame_source = st.selectbox("Display:", options=['Portfolio', 'Export Base'], key='display_frame_source')
1086
+ if display_frame_source == 'Portfolio':
1087
+ display_frame = st.session_state['working_frame']
1088
+ st.session_state['export_file'] = st.session_state['working_frame'].copy()
1089
+
1090
+ for col in st.session_state['export_file'].columns:
1091
+ if col not in excluded_cols:
1092
+ st.session_state['export_file'][col] = st.session_state['export_file'][col].map(st.session_state['export_dict'])
1093
+ elif display_frame_source == 'Export Base':
1094
+ display_frame = st.session_state['export_base']
1095
+ st.session_state['export_file'] = st.session_state['working_frame'].copy()
1096
+
1097
+ for col in st.session_state['export_file'].columns:
1098
+ if col not in excluded_cols:
1099
+ st.session_state['export_file'][col] = st.session_state['export_file'][col].map(st.session_state['export_dict'])
1100
+
1101
  if 'export_file' in st.session_state:
1102
+ download_port, merge_port, clear_export, blank_export_col = st.columns([1, 1, 1, 8])
1103
  with download_port:
1104
  st.download_button(label="Download Portfolio", data=st.session_state['export_file'].to_csv(index=False), file_name="portfolio.csv", mime="text/csv")
1105
  with merge_port:
1106
+ if st.button("Add to Custom Export"):
1107
  st.session_state['export_base'] = pd.concat([st.session_state['export_base'], st.session_state['export_merge']])
1108
  st.session_state['export_base'] = st.session_state['export_base'].drop_duplicates()
1109
  st.session_state['export_base'] = st.session_state['export_base'].reset_index(drop=True)
1110
+ with clear_export:
1111
+ if st.button("Clear Custom Export"):
1112
+ st.session_state['export_base'] = pd.DataFrame(columns=st.session_state['working_frame'].columns)
1113
+
 
 
 
 
1114
  total_rows = len(display_frame)
1115
  rows_per_page = 500
1116
  total_pages = (total_rows + rows_per_page - 1) // rows_per_page # Ceiling division