James McCool
commited on
Commit
·
415e9f9
1
Parent(s):
27f1d0e
adding a dataframe export function to start creating some ownership databases
Browse files
app.py
CHANGED
|
@@ -104,6 +104,39 @@ def export_contest_file(db, sport, type, contest_date, contest_id, contest_data)
|
|
| 104 |
|
| 105 |
return return_message
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
def get_payout_for_position(finish_pos, payout_df, dupes_count):
|
| 108 |
"""
|
| 109 |
Calculate payout for a position, handling ties by splitting the combined payout.
|
|
@@ -812,7 +845,13 @@ if selected_tab == 'Contest Analysis':
|
|
| 812 |
format(formatter='{:.2f}', subset=st.session_state['player_frame'].iloc[:, 2:4].select_dtypes(include=['number']).columns).
|
| 813 |
format(formatter='{:.2%}', subset=st.session_state['player_frame'].iloc[:, 4:].select_dtypes(include=['number']).columns),
|
| 814 |
hide_index=True)
|
| 815 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 816 |
else:
|
| 817 |
st.session_state['player_frame'] = create_player_exposures(st.session_state['display_contest_info'], st.session_state['player_columns'], st.session_state['entry_names'])
|
| 818 |
hold_frame = st.session_state['player_frame'].copy()
|
|
|
|
| 104 |
|
| 105 |
return return_message
|
| 106 |
|
| 107 |
+
def export_exposure_frame(db, sport, type, contest_date, contest_id, exposure_frame):
|
| 108 |
+
if type == 'Classic':
|
| 109 |
+
db_type = 'reg'
|
| 110 |
+
elif type == 'Showdown':
|
| 111 |
+
db_type = 'showdown'
|
| 112 |
+
collection = db[f'{sport}_{db_type}_exposure_frames']
|
| 113 |
+
try:
|
| 114 |
+
cursor = collection.find()
|
| 115 |
+
exposure_frame_import = pd.DataFrame(list(cursor)).drop('_id', axis=1)
|
| 116 |
+
if contest_id in exposure_frame_import['Contest ID'].values:
|
| 117 |
+
return_message = "Data for this contest already exists, no need to upload, but we appreciate the effort!"
|
| 118 |
+
return return_message
|
| 119 |
+
except:
|
| 120 |
+
exposure_frame_import = pd.DataFrame(columns = ['Player', 'Pos', 'Salary', 'Actual', 'Exposure Overall', 'Exposure Top 1%', 'Exposure Top 5%', 'Exposure Top 10%', 'Exposure Top 20%', 'Contest Date', 'Contest ID'])
|
| 121 |
+
|
| 122 |
+
exposure_frame_import['Contest Date'] = contest_date
|
| 123 |
+
exposure_frame_import['Contest ID'] = contest_id
|
| 124 |
+
exposure_frame_import = pd.concat([exposure_frame_import, exposure_frame], ignore_index=True)
|
| 125 |
+
|
| 126 |
+
chunk_size = 100000
|
| 127 |
+
collection.drop()
|
| 128 |
+
for i in range(0, len(exposure_frame_import), chunk_size):
|
| 129 |
+
for _ in range(5):
|
| 130 |
+
try:
|
| 131 |
+
df_chunk = exposure_frame_import.iloc[i:i + chunk_size]
|
| 132 |
+
collection.insert_many(df_chunk.to_dict('records'), ordered=False)
|
| 133 |
+
break
|
| 134 |
+
except Exception as e:
|
| 135 |
+
print(f"Retry due to error: {e}")
|
| 136 |
+
return_message = "Contest data uploaded successfully! We appreciate the data!"
|
| 137 |
+
|
| 138 |
+
return return_message
|
| 139 |
+
|
| 140 |
def get_payout_for_position(finish_pos, payout_df, dupes_count):
|
| 141 |
"""
|
| 142 |
Calculate payout for a position, handling ties by splitting the combined payout.
|
|
|
|
| 845 |
format(formatter='{:.2f}', subset=st.session_state['player_frame'].iloc[:, 2:4].select_dtypes(include=['number']).columns).
|
| 846 |
format(formatter='{:.2%}', subset=st.session_state['player_frame'].iloc[:, 4:].select_dtypes(include=['number']).columns),
|
| 847 |
hide_index=True)
|
| 848 |
+
player_exposure_download_col, player_exposure_export_col, player_exposure_blank_col = st.columns([2, 2, 8])
|
| 849 |
+
with player_exposure_download_col:
|
| 850 |
+
st.download_button(label="Download Player Info", data=st.session_state['player_frame'].to_csv(index=False), file_name="player_info.csv", mime="text/csv", key='player_exp_comp_download')
|
| 851 |
+
with player_exposure_export_col:
|
| 852 |
+
if st.button("Export Player Info"):
|
| 853 |
+
return_message = export_exposure_frame(db, st.session_state['sport_select'], st.session_state['type_init'], st.session_state['contest_date'], st.session_state['contest_id'], st.session_state['player_frame'])
|
| 854 |
+
st.write(return_message)
|
| 855 |
else:
|
| 856 |
st.session_state['player_frame'] = create_player_exposures(st.session_state['display_contest_info'], st.session_state['player_columns'], st.session_state['entry_names'])
|
| 857 |
hold_frame = st.session_state['player_frame'].copy()
|