James McCool commited on
Commit
39841d9
·
1 Parent(s): 06c79a5

Add contest file export functionality in app.py

Browse files

- Introduced a new function, export_contest_file, to handle the export of contest data to the database, ensuring data integrity by checking for existing entries.
- Implemented chunked data insertion to optimize database performance and added error handling for robust data processing.
- Updated the user interface to include a button for exporting contest files, enhancing user interaction and data management capabilities.

Files changed (1) hide show
  1. app.py +39 -1
app.py CHANGED
@@ -54,6 +54,39 @@ def grab_contest_player_info(db, sport, type, contest_date, contest_name, contes
54
 
55
  return player_info, info_maps
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  db = init_conn()
58
 
59
  ## import global functions
@@ -140,7 +173,12 @@ with tab1:
140
  st.session_state['Contest'] = st.session_state['Contest'].dropna(how='all')
141
  st.session_state['Contest'] = st.session_state['Contest'].reset_index(drop=True)
142
  if st.session_state['Contest'] is not None:
143
- st.success('Contest file loaded successfully!')
 
 
 
 
 
144
  st.dataframe(st.session_state['Contest'].head(100))
145
 
146
  if 'Contest_file' in st.session_state:
 
54
 
55
  return player_info, info_maps
56
 
57
+ def export_contest_file(db, sport, type, contest_date, contest_id, contest_data):
58
+ if type == 'Classic':
59
+ db_type = 'reg'
60
+ elif type == 'Showdown':
61
+ db_type = 'showdown'
62
+ collection = db[f'{sport}_{db_type}_contest_data']
63
+ try:
64
+ cursor = collection.find()
65
+ contest_import = pd.DataFrame(list(cursor)).drop('_id', axis=1)
66
+ except:
67
+ contest_import = pd.DataFrame(columns = contest_data.columns)
68
+
69
+ if contest_id in contest_import['Contest ID'].values:
70
+ st.info("Data for this contest already exists, no need to upload, but we appreciate the effort!")
71
+ return
72
+
73
+ contest_data['Contest Date'] = contest_date
74
+ contest_data['Contest ID'] = contest_id
75
+ contest_import = pd.concat([contest_import, contest_data], ignore_index=True)
76
+
77
+ chunk_size = 10000
78
+ collection.drop()
79
+ for i in range(0, len(contest_import), chunk_size):
80
+ for _ in range(5):
81
+ try:
82
+ df_chunk = contest_import.iloc[i:i + chunk_size]
83
+ collection.insert_many(df_chunk.to_dict('records'), ordered=False)
84
+ break
85
+ except Exception as e:
86
+ print(f"Retry due to error: {e}")
87
+
88
+ return
89
+
90
  db = init_conn()
91
 
92
  ## import global functions
 
173
  st.session_state['Contest'] = st.session_state['Contest'].dropna(how='all')
174
  st.session_state['Contest'] = st.session_state['Contest'].reset_index(drop=True)
175
  if st.session_state['Contest'] is not None:
176
+ success_col, blank_col, upload_col = st.columns([2, 1, 1])
177
+ with success_col:
178
+ st.success('Contest file loaded successfully!')
179
+ with upload_col:
180
+ if st.button('Send file to Database?', key='export_contest_file'):
181
+ export_contest_file(db, sport_select, type_var, date_select, contest_id_map[contest_name_var], st.session_state['Contest_file'])
182
  st.dataframe(st.session_state['Contest'].head(100))
183
 
184
  if 'Contest_file' in st.session_state: