James McCool commited on
Commit
a707b59
·
1 Parent(s): 2841628

Enhance caching and data handling in app.py for improved performance

Browse files

- Added caching decorators to several functions to optimize data retrieval and processing, enhancing application performance.
- Introduced a new function to highlight specific rows in the dataframe based on conditions, improving data visualization for users.
- These changes aim to streamline data management and enhance user experience by ensuring efficient data handling and clearer presentation of information.

Files changed (1) hide show
  1. app.py +14 -1
app.py CHANGED
@@ -10,6 +10,7 @@ from datetime import datetime
10
 
11
  # Just setting a note here to say that I should attempt to do some memory allocation savings and swap to numpy soon
12
 
 
13
  def init_conn():
14
 
15
  uri = st.secrets['mongo_uri']
@@ -17,7 +18,8 @@ def init_conn():
17
  db = client['Contest_Information']
18
 
19
  return db
20
-
 
21
  def grab_contest_names(db, sport, type):
22
  if type == 'Classic':
23
  db_type = 'reg'
@@ -33,6 +35,7 @@ def grab_contest_names(db, sport, type):
33
 
34
  return contest_names, curr_info
35
 
 
36
  def grab_contest_player_info(db, sport, type, contest_date, contest_name, contest_id_map):
37
  if type == 'Classic':
38
  db_type = 'reg'
@@ -56,6 +59,7 @@ def grab_contest_player_info(db, sport, type, contest_date, contest_name, contes
56
 
57
  return player_info, info_maps
58
 
 
59
  def export_contest_file(db, sport, type, contest_date, contest_id, contest_data):
60
  if type == 'Classic':
61
  db_type = 'reg'
@@ -103,12 +107,20 @@ from global_func.create_stack_comparison import create_stack_comparison
103
  from global_func.create_size_comparison import create_size_comparison
104
  from global_func.create_general_comparison import create_general_comparison
105
 
 
106
  def is_valid_input(file):
107
  if isinstance(file, pd.DataFrame):
108
  return not file.empty
109
  else:
110
  return file is not None # For Streamlit uploader objects
111
 
 
 
 
 
 
 
 
112
  player_exposure_format = {'Exposure Overall': '{:.2%}', 'Exposure Top 1%': '{:.2%}', 'Exposure Top 5%': '{:.2%}', 'Exposure Top 10%': '{:.2%}', 'Exposure Top 20%': '{:.2%}'}
113
  dupe_format = {'uniques%': '{:.2%}', 'under_5%': '{:.2%}', 'under_10%': '{:.2%}'}
114
 
@@ -496,6 +508,7 @@ with tab2:
496
  end_idx = min((st.session_state.current_page) * rows_per_page, total_rows)
497
  st.dataframe(
498
  st.session_state['display_contest_info'].iloc[start_idx:end_idx].style
 
499
  .background_gradient(axis=0)
500
  .background_gradient(cmap='RdYlGn')
501
  .format(precision=2),
 
10
 
11
  # Just setting a note here to say that I should attempt to do some memory allocation savings and swap to numpy soon
12
 
13
+ @st.cache_resource
14
  def init_conn():
15
 
16
  uri = st.secrets['mongo_uri']
 
18
  db = client['Contest_Information']
19
 
20
  return db
21
+
22
+ @st.cache_data
23
  def grab_contest_names(db, sport, type):
24
  if type == 'Classic':
25
  db_type = 'reg'
 
35
 
36
  return contest_names, curr_info
37
 
38
+ @st.cache_data
39
  def grab_contest_player_info(db, sport, type, contest_date, contest_name, contest_id_map):
40
  if type == 'Classic':
41
  db_type = 'reg'
 
59
 
60
  return player_info, info_maps
61
 
62
+ @st.cache_data
63
  def export_contest_file(db, sport, type, contest_date, contest_id, contest_data):
64
  if type == 'Classic':
65
  db_type = 'reg'
 
107
  from global_func.create_size_comparison import create_size_comparison
108
  from global_func.create_general_comparison import create_general_comparison
109
 
110
+ @st.cache_data
111
  def is_valid_input(file):
112
  if isinstance(file, pd.DataFrame):
113
  return not file.empty
114
  else:
115
  return file is not None # For Streamlit uploader objects
116
 
117
+ @st.cache_data
118
+ def highlight_row_condition(row):
119
+ if row['BaseName'] == 'Backtesting_upload':
120
+ return ['background-color: lightgreen'] * len(row)
121
+ else:
122
+ return [''] * len(row)
123
+
124
  player_exposure_format = {'Exposure Overall': '{:.2%}', 'Exposure Top 1%': '{:.2%}', 'Exposure Top 5%': '{:.2%}', 'Exposure Top 10%': '{:.2%}', 'Exposure Top 20%': '{:.2%}'}
125
  dupe_format = {'uniques%': '{:.2%}', 'under_5%': '{:.2%}', 'under_10%': '{:.2%}'}
126
 
 
508
  end_idx = min((st.session_state.current_page) * rows_per_page, total_rows)
509
  st.dataframe(
510
  st.session_state['display_contest_info'].iloc[start_idx:end_idx].style
511
+ .apply(highlight_row_condition, axis=1)
512
  .background_gradient(axis=0)
513
  .background_gradient(cmap='RdYlGn')
514
  .format(precision=2),