James McCool
commited on
Commit
·
996f8cb
1
Parent(s):
2ac8839
Refactor contest data handling in app.py and load_contest_file.py for improved clarity and functionality
Browse files- Updated app.py to ensure player information is retrieved before contest data, enhancing the flow of data processing.
- Simplified the load_contest_file function by removing the helper_var parameter, streamlining the return values for better clarity.
- Introduced a new manual_contest_upload.py file to encapsulate the grab_contest_data function, improving code organization and maintainability.
- Maintained existing functionality while enhancing the structure and readability of contest data handling.
- app.py +3 -6
- global_func/load_contest_file.py +2 -5
- global_func/manual_contest_upload.py +75 -0
app.py
CHANGED
@@ -105,8 +105,8 @@ with tab1:
|
|
105 |
del st.session_state['Contest_file']
|
106 |
if 'Contest_file' not in st.session_state:
|
107 |
if st.button('Load Contest Data', key='load_contest_data'):
|
108 |
-
st.session_state['Contest_file'] = grab_contest_data(sport_select, contest_name_var, contest_id_map, date_select, date_select2)
|
109 |
st.session_state['player_info'], st.session_state['info_maps'] = grab_contest_player_info(db, sport_select, type_var, date_select)
|
|
|
110 |
else:
|
111 |
pass
|
112 |
with col2:
|
@@ -118,16 +118,13 @@ with tab1:
|
|
118 |
del st.session_state['Contest_file']
|
119 |
if 'Contest_file' not in st.session_state:
|
120 |
st.session_state['Contest_upload'] = st.file_uploader("Upload Contest File (CSV or Excel)", type=['csv', 'xlsx', 'xls'])
|
|
|
121 |
st.session_state['Contest_file'] = pd.read_csv(st.session_state['Contest_upload'])
|
122 |
-
st.session_state['Contest_file_helper'] = grab_contest_data(sport_select, contest_name_var, contest_id_map, date_select, date_select2)
|
123 |
else:
|
124 |
pass
|
125 |
|
126 |
if 'Contest_file' in st.session_state:
|
127 |
-
|
128 |
-
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'], 1, st.session_state['Contest_file_helper'], sport_select)
|
129 |
-
else:
|
130 |
-
st.session_state['Contest'], st.session_state['ownership_df'], st.session_state['actual_df'], st.session_state['entry_list'], check_lineups = load_contest_file(st.session_state['Contest_file'], 0, st.session_state['player_info'], sport_select)
|
131 |
st.session_state['Contest'] = st.session_state['Contest'].dropna(how='all')
|
132 |
st.session_state['Contest'] = st.session_state['Contest'].reset_index(drop=True)
|
133 |
if st.session_state['Contest'] is not None:
|
|
|
105 |
del st.session_state['Contest_file']
|
106 |
if 'Contest_file' not in st.session_state:
|
107 |
if st.button('Load Contest Data', key='load_contest_data'):
|
|
|
108 |
st.session_state['player_info'], st.session_state['info_maps'] = grab_contest_player_info(db, sport_select, type_var, date_select)
|
109 |
+
st.session_state['Contest_file'] = grab_contest_data(sport_select, contest_name_var, contest_id_map, date_select, date_select2)
|
110 |
else:
|
111 |
pass
|
112 |
with col2:
|
|
|
118 |
del st.session_state['Contest_file']
|
119 |
if 'Contest_file' not in st.session_state:
|
120 |
st.session_state['Contest_upload'] = st.file_uploader("Upload Contest File (CSV or Excel)", type=['csv', 'xlsx', 'xls'])
|
121 |
+
st.session_state['player_info'], st.session_state['info_maps'] = grab_contest_player_info(db, sport_select, type_var, date_select)
|
122 |
st.session_state['Contest_file'] = pd.read_csv(st.session_state['Contest_upload'])
|
|
|
123 |
else:
|
124 |
pass
|
125 |
|
126 |
if 'Contest_file' in st.session_state:
|
127 |
+
st.session_state['Contest'], st.session_state['ownership_df'], st.session_state['actual_df'], st.session_state['entry_list'], check_lineups = load_contest_file(st.session_state['Contest_file'], st.session_state['player_info'], sport_select)
|
|
|
|
|
|
|
128 |
st.session_state['Contest'] = st.session_state['Contest'].dropna(how='all')
|
129 |
st.session_state['Contest'] = st.session_state['Contest'].reset_index(drop=True)
|
130 |
if st.session_state['Contest'] is not None:
|
global_func/load_contest_file.py
CHANGED
@@ -2,7 +2,7 @@ import streamlit as st
|
|
2 |
import pandas as pd
|
3 |
from rapidfuzz import process, fuzz
|
4 |
|
5 |
-
def load_contest_file(upload,
|
6 |
if upload is not None:
|
7 |
try:
|
8 |
try:
|
@@ -132,10 +132,7 @@ def load_contest_file(upload, helper_var, helper = None, sport = None):
|
|
132 |
entry_list = list(set(df['BaseName'].dropna()))
|
133 |
entry_list.sort()
|
134 |
|
135 |
-
|
136 |
-
return cleaned_df, ownership_df, fpts_df, salary_df, team_df, pos_df, entry_list, check_lineups
|
137 |
-
else:
|
138 |
-
return cleaned_df, ownership_df, fpts_df, entry_list, check_lineups
|
139 |
|
140 |
except Exception as e:
|
141 |
st.error(f'Error loading file: {str(e)}')
|
|
|
2 |
import pandas as pd
|
3 |
from rapidfuzz import process, fuzz
|
4 |
|
5 |
+
def load_contest_file(upload, helper = None, sport = None):
|
6 |
if upload is not None:
|
7 |
try:
|
8 |
try:
|
|
|
132 |
entry_list = list(set(df['BaseName'].dropna()))
|
133 |
entry_list.sort()
|
134 |
|
135 |
+
return cleaned_df, ownership_df, fpts_df, entry_list, check_lineups
|
|
|
|
|
|
|
136 |
|
137 |
except Exception as e:
|
138 |
st.error(f'Error loading file: {str(e)}')
|
global_func/manual_contest_upload.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import requests
|
3 |
+
|
4 |
+
def grab_contest_data(sport, contest_file):
|
5 |
+
|
6 |
+
def format_lineup_string(lineup_hash, positions):
|
7 |
+
"""Replaces colons in a lineup hash with sequential positions."""
|
8 |
+
# Remove the leading colon and split by the remaining colons
|
9 |
+
# Only split on colons that are between numbers
|
10 |
+
player_ids = [pid for pid in lineup_hash.lstrip(':').split(':') if pid.isdigit() or pid == '-1']
|
11 |
+
|
12 |
+
# Get the actual positions needed for this lineup
|
13 |
+
actual_positions = positions[:len(player_ids)]
|
14 |
+
|
15 |
+
# Check if the number of IDs matches the number of positions
|
16 |
+
if len(player_ids) != len(actual_positions):
|
17 |
+
print(f"Warning: Mismatch for hash {lineup_hash}. IDs: {len(player_ids)}, Positions: {len(actual_positions)}")
|
18 |
+
return lineup_hash
|
19 |
+
|
20 |
+
# Combine positions and player IDs, only for valid player IDs
|
21 |
+
combined_parts = []
|
22 |
+
for pos, pid in zip(actual_positions, player_ids):
|
23 |
+
if pid == '-1': # Handle empty slots
|
24 |
+
combined_parts.append(pos + '')
|
25 |
+
else:
|
26 |
+
combined_parts.append(pos + pid_map.get(pid, pid))
|
27 |
+
|
28 |
+
# # Just combine positions and PIDs without name mapping
|
29 |
+
# combined_parts = [pos + pid for pos, pid in zip(actual_positions, player_ids)]
|
30 |
+
|
31 |
+
# Join them into a single string
|
32 |
+
return "".join(combined_parts)
|
33 |
+
|
34 |
+
lineup_data = []
|
35 |
+
player_data = []
|
36 |
+
position_inserts = ['1B ', ' 2B ', ' 3B ', ' C ', ' OF ', ' OF ', ' OF ', ' P ', ' P ', ' SS ']
|
37 |
+
|
38 |
+
for players, player_info in data_json['players'].items():
|
39 |
+
player_data.append({
|
40 |
+
'fullName': player_info['fullName'],
|
41 |
+
'playerId': player_info['playerId'],
|
42 |
+
'salary': player_info['salary'],
|
43 |
+
'currentTeam': player_info['currentTeam'],
|
44 |
+
'rosterPosition': player_info['rosterPosition'],
|
45 |
+
'ownership': player_info['ownership'],
|
46 |
+
'actualPoints': player_info['actualPoints']
|
47 |
+
})
|
48 |
+
|
49 |
+
players_df = pd.DataFrame(player_data)
|
50 |
+
players_df = players_df.sort_values(by='ownership', ascending=False).reset_index(drop=True)
|
51 |
+
players_df = players_df.rename(columns={'fullName': 'Player', 'rosterPosition': 'Roster Position', 'ownership': '%Drafted', 'actualPoints': 'FPTS', 'salary': 'Salary', 'currentTeam': 'Team'})
|
52 |
+
pid_map = dict(zip(players_df['playerId'].astype(str), players_df['Player']))
|
53 |
+
|
54 |
+
for lineup_hash, lineup_info in lineups_json['lineups'].items():
|
55 |
+
lineup_data.append({
|
56 |
+
'lineupHash': lineup_hash,
|
57 |
+
'points': lineup_info['points'],
|
58 |
+
'entryNameList': lineup_info['entryNameList'][0]
|
59 |
+
})
|
60 |
+
|
61 |
+
lineups_df = pd.DataFrame(lineup_data)
|
62 |
+
lineups_df = lineups_df.sort_values(by='points', ascending=False)
|
63 |
+
lineups_df = lineups_df.reset_index()
|
64 |
+
lineups_df['index'] = lineups_df.index + 1
|
65 |
+
lineups_df['TimeRemaining'] = str(0)
|
66 |
+
lineups_df['EntryId'] = lineups_df['index'].astype(str) + lineups_df['entryNameList'].astype(str)
|
67 |
+
lineups_df['lineupHash'] = ':' + lineups_df['lineupHash']
|
68 |
+
lineups_df = lineups_df.rename(columns={'index': 'Rank', 'points': 'Points', 'entryNameList': 'EntryName', 'lineupHash': 'Lineup'})
|
69 |
+
lineups_df['EntryName'] = lineups_df['EntryName'] + ' (1/1)'
|
70 |
+
lineups_df['Lineup'] = lineups_df['Lineup'].apply(lambda x: format_lineup_string(x, position_inserts))
|
71 |
+
lineups_df = lineups_df[['Rank', 'EntryId', 'EntryName', 'TimeRemaining', 'Points', 'Lineup']]
|
72 |
+
|
73 |
+
total_data = lineups_df.merge(players_df, how='left', left_index=True, right_index=True)
|
74 |
+
|
75 |
+
return total_data
|