James McCool
commited on
Commit
·
ffe9696
1
Parent(s):
22b5408
Enhance portfolio upload error handling and data integration in app.py
Browse files- Added error handling for portfolio file uploads to prevent crashes on invalid input.
- Integrated portfolio data with contest data for improved analysis and visibility.
- Adjusted logic to ensure the working dataframe is derived from the combined dataset when a portfolio is uploaded, enhancing user experience.
app.py
CHANGED
|
@@ -173,16 +173,20 @@ with tab1:
|
|
| 173 |
with portfolio_upload:
|
| 174 |
st.info("If you have a portfolio of lineups, you can upload them here to see how they would have performed against the field")
|
| 175 |
st.session_state['portfolio_file'] = st.file_uploader("Upload Portfolio File (CSV or Excel)", type=['csv', 'xlsx', 'xls'])
|
| 176 |
-
|
| 177 |
-
|
| 178 |
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
|
| 188 |
if 'Contest_file' in st.session_state:
|
|
@@ -208,6 +212,7 @@ with tab1:
|
|
| 208 |
if 'portfolio_df' in st.session_state:
|
| 209 |
st.write('Portfolio data:')
|
| 210 |
st.dataframe(st.session_state['portfolio_df'].head(100))
|
|
|
|
| 211 |
else:
|
| 212 |
pass
|
| 213 |
|
|
@@ -240,7 +245,10 @@ with tab2:
|
|
| 240 |
}
|
| 241 |
|
| 242 |
# Create a copy of the dataframe for calculations
|
| 243 |
-
|
|
|
|
|
|
|
|
|
|
| 244 |
|
| 245 |
# Pre-compute lookup arrays for vectorized operations
|
| 246 |
team_map = st.session_state['map_dict']['team_map']
|
|
|
|
| 173 |
with portfolio_upload:
|
| 174 |
st.info("If you have a portfolio of lineups, you can upload them here to see how they would have performed against the field")
|
| 175 |
st.session_state['portfolio_file'] = st.file_uploader("Upload Portfolio File (CSV or Excel)", type=['csv', 'xlsx', 'xls'])
|
| 176 |
+
try:
|
| 177 |
+
st.session_state['portfolio_df'] = pd.read_csv(st.session_state['portfolio_file'])
|
| 178 |
|
| 179 |
+
original_columns = st.session_state['portfolio_df'].columns.tolist()
|
| 180 |
+
|
| 181 |
+
for col in original_columns:
|
| 182 |
+
st.session_state['portfolio_df'][col] = st.session_state['portfolio_df'][col].astype(str).str.replace(r'\s*\([^)]*\)', '', regex=True).str.strip()
|
| 183 |
+
|
| 184 |
+
st.session_state['portfolio_df']['BaseName'] = 'Backtesting_upload'
|
| 185 |
+
st.session_state['portfolio_df']['EntryCount'] = len(st.session_state['portfolio_df'])
|
| 186 |
+
|
| 187 |
+
st.session_state['portfolio_df'] = st.session_state['portfolio_df'][['BaseName', 'EntryCount'] + original_columns]
|
| 188 |
+
except:
|
| 189 |
+
pass
|
| 190 |
|
| 191 |
|
| 192 |
if 'Contest_file' in st.session_state:
|
|
|
|
| 212 |
if 'portfolio_df' in st.session_state:
|
| 213 |
st.write('Portfolio data:')
|
| 214 |
st.dataframe(st.session_state['portfolio_df'].head(100))
|
| 215 |
+
st.session_state['combined_df'] = pd.concat([st.session_state['Contest'], st.session_state['portfolio_df']], ignore_index=True)
|
| 216 |
else:
|
| 217 |
pass
|
| 218 |
|
|
|
|
| 245 |
}
|
| 246 |
|
| 247 |
# Create a copy of the dataframe for calculations
|
| 248 |
+
if 'portfolio_df' in st.session_state:
|
| 249 |
+
working_df = st.session_state['combined_df'].copy()
|
| 250 |
+
else:
|
| 251 |
+
working_df = st.session_state['Contest'].copy()
|
| 252 |
|
| 253 |
# Pre-compute lookup arrays for vectorized operations
|
| 254 |
team_map = st.session_state['map_dict']['team_map']
|