James McCool
commited on
Commit
·
dd89b11
1
Parent(s):
df8ffd8
Enhance contest file loading in app.py and load_contest_file function
Browse files- Updated the app.py to include a helper parameter for contest file processing, allowing for additional data handling during file uploads.
- Modified the load_contest_file function to accept an optional helper argument, enabling differentiated processing of contest data based on the presence of helper data.
- Improved the selection and renaming of essential columns in both the main and helper dataframes, enhancing data clarity and usability for further analysis.
- app.py +5 -1
- global_func/load_contest_file.py +36 -8
app.py
CHANGED
|
@@ -81,11 +81,15 @@ with tab1:
|
|
| 81 |
elif parse_type == 'Manual':
|
| 82 |
st.session_state.clear()
|
| 83 |
st.session_state['Contest_file'] = st.file_uploader("Upload Contest File (CSV or Excel)", type=['csv', 'xlsx', 'xls'])
|
|
|
|
| 84 |
if 'Contest' in st.session_state:
|
| 85 |
del st.session_state['Contest']
|
| 86 |
|
| 87 |
if 'Contest_file' in st.session_state and 'Adj_Contest' not in st.session_state:
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
| 89 |
st.session_state['Contest'] = st.session_state['Contest'].dropna(how='all')
|
| 90 |
st.session_state['Contest'] = st.session_state['Contest'].reset_index(drop=True)
|
| 91 |
if st.session_state['Contest'] is not None:
|
|
|
|
| 81 |
elif parse_type == 'Manual':
|
| 82 |
st.session_state.clear()
|
| 83 |
st.session_state['Contest_file'] = st.file_uploader("Upload Contest File (CSV or Excel)", type=['csv', 'xlsx', 'xls'])
|
| 84 |
+
st.session_state['Contest_file_helper'] = grab_contest_data(sport_select, name_parse[0], contest_id_map, date_select)
|
| 85 |
if 'Contest' in st.session_state:
|
| 86 |
del st.session_state['Contest']
|
| 87 |
|
| 88 |
if 'Contest_file' in st.session_state and 'Adj_Contest' not in st.session_state:
|
| 89 |
+
if 'Contest_file_helper' in st.session_state:
|
| 90 |
+
st.session_state['Contest'], st.session_state['ownership_df'], st.session_state['actual_df'], st.session_state['salary_df'], st.session_state['team_df'], st.session_state['pos_df'], st.session_state['entry_list'], check_lineups = load_contest_file(st.session_state['Contest_file'], st.session_state['Contest_file_helper'], sport_select)
|
| 91 |
+
else:
|
| 92 |
+
st.session_state['Contest'], st.session_state['ownership_df'], st.session_state['actual_df'], st.session_state['salary_df'], st.session_state['team_df'], st.session_state['pos_df'], st.session_state['entry_list'], check_lineups = load_contest_file(st.session_state['Contest_file'], sport_select)
|
| 93 |
st.session_state['Contest'] = st.session_state['Contest'].dropna(how='all')
|
| 94 |
st.session_state['Contest'] = st.session_state['Contest'].reset_index(drop=True)
|
| 95 |
if st.session_state['Contest'] is not None:
|
global_func/load_contest_file.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
|
| 4 |
-
def load_contest_file(upload, sport):
|
| 5 |
if sport == 'MLB':
|
| 6 |
pos_list = [' P ', ' C ', '1B ', ' 2B ', ' 3B ', ' SS ', ' OF ']
|
| 7 |
if upload is not None:
|
|
@@ -16,9 +16,14 @@ def load_contest_file(upload, sport):
|
|
| 16 |
return None
|
| 17 |
except:
|
| 18 |
raw_df = upload
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
# Select and rename essential columns
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
| 22 |
df = df.rename(columns={'Roster Position': 'Pos', '%Drafted': 'Own'})
|
| 23 |
|
| 24 |
# Split EntryName into base name and entry count
|
|
@@ -32,12 +37,35 @@ def load_contest_file(upload, sport):
|
|
| 32 |
except:
|
| 33 |
df['Own'] = df['Own'].astype(float)
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
# Create separate dataframes for different player attributes
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# Create the cleaned dataframe with just the essential columns
|
| 43 |
cleaned_df = df[['BaseName', 'Lineup']]
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
|
| 4 |
+
def load_contest_file(upload, helper = None, sport = None):
|
| 5 |
if sport == 'MLB':
|
| 6 |
pos_list = [' P ', ' C ', '1B ', ' 2B ', ' 3B ', ' SS ', ' OF ']
|
| 7 |
if upload is not None:
|
|
|
|
| 16 |
return None
|
| 17 |
except:
|
| 18 |
raw_df = upload
|
| 19 |
+
if helper is not None:
|
| 20 |
+
helper_df = helper
|
| 21 |
|
| 22 |
+
# Select and rename essential columns for the actual upload
|
| 23 |
+
if helper is None:
|
| 24 |
+
df = raw_df[['EntryId', 'EntryName', 'TimeRemaining', 'Points', 'Lineup', 'Player', 'Roster Position', '%Drafted', 'FPTS', 'Salary', 'Team']]
|
| 25 |
+
else:
|
| 26 |
+
df = raw_df[['EntryId', 'EntryName', 'TimeRemaining', 'Points', 'Lineup', 'Player', 'Roster Position', '%Drafted', 'FPTS']]
|
| 27 |
df = df.rename(columns={'Roster Position': 'Pos', '%Drafted': 'Own'})
|
| 28 |
|
| 29 |
# Split EntryName into base name and entry count
|
|
|
|
| 37 |
except:
|
| 38 |
df['Own'] = df['Own'].astype(float)
|
| 39 |
|
| 40 |
+
# Select and rename essential columns for the actual upload
|
| 41 |
+
if helper is not None:
|
| 42 |
+
df_helper = helper_df[['EntryId', 'EntryName', 'TimeRemaining', 'Points', 'Lineup', 'Player', 'Roster Position', '%Drafted', 'FPTS', 'Salary', 'Team']]
|
| 43 |
+
df_helper = df_helper.rename(columns={'Roster Position': 'Pos', '%Drafted': 'Own'})
|
| 44 |
+
|
| 45 |
+
# Split EntryName into base name and entry count
|
| 46 |
+
df_helper['BaseName'] = df_helper['EntryName'].str.replace(r'\s*\(\d+/\d+\)$', '', regex=True)
|
| 47 |
+
df_helper['EntryCount'] = df_helper['EntryName'].str.extract(r'\((\d+/\d+)\)')
|
| 48 |
+
df_helper['EntryCount'] = df_helper['EntryCount'].fillna('1/1') # Default to 1/1 if no entry count
|
| 49 |
+
|
| 50 |
+
# Convert ownership percentage to float
|
| 51 |
+
try:
|
| 52 |
+
df_helper['Own'] = df_helper['Own'].str.replace('%', '').astype(float)
|
| 53 |
+
except:
|
| 54 |
+
df_helper['Own'] = df_helper['Own'].astype(float)
|
| 55 |
+
|
| 56 |
# Create separate dataframes for different player attributes
|
| 57 |
+
if helper is not None:
|
| 58 |
+
ownership_df = df[['Player', 'Own']]
|
| 59 |
+
fpts_df = df[['Player', 'FPTS']]
|
| 60 |
+
salary_df = df_helper[['Player', 'Salary']]
|
| 61 |
+
team_df = df_helper[['Player', 'Team']]
|
| 62 |
+
pos_df = df[['Player', 'Pos']]
|
| 63 |
+
else:
|
| 64 |
+
ownership_df = df[['Player', 'Own']]
|
| 65 |
+
fpts_df = df[['Player', 'FPTS']]
|
| 66 |
+
salary_df = df[['Player', 'Salary']]
|
| 67 |
+
team_df = df[['Player', 'Team']]
|
| 68 |
+
pos_df = df[['Player', 'Pos']]
|
| 69 |
|
| 70 |
# Create the cleaned dataframe with just the essential columns
|
| 71 |
cleaned_df = df[['BaseName', 'Lineup']]
|