James McCool
commited on
Commit
·
1b35a81
1
Parent(s):
2b8ea88
Refactor portfolio loading logic to remove redundant checks and optimize memory management. The code now clears session state objects only when not sourced from the database, ensuring necessary data persistence while improving overall efficiency.
Browse files
app.py
CHANGED
|
@@ -716,24 +716,6 @@ if selected_tab == 'Data Load':
|
|
| 716 |
st.dataframe(projections.head(10))
|
| 717 |
|
| 718 |
if st.session_state['portfolio_loaded'] and projections_file:
|
| 719 |
-
# Ensure portfolio exists, reload from database if needed
|
| 720 |
-
if 'portfolio' not in st.session_state and upload_toggle == 'Paydirt DB' and 'db_portfolio_file' in st.session_state:
|
| 721 |
-
st.session_state['export_portfolio'], st.session_state['portfolio'] = load_file(st.session_state['db_portfolio_file'], site_var, type_var, sport_var, 'portfolio')
|
| 722 |
-
st.session_state['export_portfolio'] = st.session_state['export_portfolio'].dropna(how='all').reset_index(drop=True)
|
| 723 |
-
st.session_state['portfolio'] = st.session_state['portfolio'].dropna(how='all').reset_index(drop=True)
|
| 724 |
-
|
| 725 |
-
# Optimize data types early for memory efficiency
|
| 726 |
-
st.session_state['portfolio'] = optimize_dataframe_dtypes(st.session_state['portfolio'])
|
| 727 |
-
|
| 728 |
-
# Clean player names
|
| 729 |
-
for col in st.session_state['portfolio'].select_dtypes(include=['object', 'category']).columns:
|
| 730 |
-
if st.session_state['portfolio'][col].dtype == 'category':
|
| 731 |
-
st.session_state['portfolio'][col] = st.session_state['portfolio'][col].cat.rename_categories(
|
| 732 |
-
lambda x: player_right_names_mlb.get(x, x) if x in player_wrong_names_mlb else x
|
| 733 |
-
)
|
| 734 |
-
else:
|
| 735 |
-
st.session_state['portfolio'][col] = st.session_state['portfolio'][col].replace(player_wrong_names_mlb)
|
| 736 |
-
|
| 737 |
if st.session_state['portfolio'] is not None and projections is not None:
|
| 738 |
|
| 739 |
st.subheader("Name Matching Analysis")
|
|
@@ -811,11 +793,10 @@ if selected_tab == 'Data Load':
|
|
| 811 |
st.session_state['portfolio'].to_parquet(buffer, compression='snappy')
|
| 812 |
st.session_state['origin_portfolio'] = buffer.getvalue()
|
| 813 |
|
| 814 |
-
# Clear large objects from session state to free memory
|
| 815 |
-
|
| 816 |
-
|
| 817 |
-
|
| 818 |
-
del st.session_state['export_portfolio']
|
| 819 |
|
| 820 |
# with tab2:
|
| 821 |
# if st.button('Clear data', key='reset2'):
|
|
|
|
| 716 |
st.dataframe(projections.head(10))
|
| 717 |
|
| 718 |
if st.session_state['portfolio_loaded'] and projections_file:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 719 |
if st.session_state['portfolio'] is not None and projections is not None:
|
| 720 |
|
| 721 |
st.subheader("Name Matching Analysis")
|
|
|
|
| 793 |
st.session_state['portfolio'].to_parquet(buffer, compression='snappy')
|
| 794 |
st.session_state['origin_portfolio'] = buffer.getvalue()
|
| 795 |
|
| 796 |
+
# Clear large objects from session state to free memory
|
| 797 |
+
# Don't delete portfolio if it came from database (needs to persist)
|
| 798 |
+
if upload_toggle != 'Paydirt DB':
|
| 799 |
+
del st.session_state['portfolio'], st.session_state['export_portfolio']
|
|
|
|
| 800 |
|
| 801 |
# with tab2:
|
| 802 |
# if st.button('Clear data', key='reset2'):
|