James McCool commited on
Commit
9c7e08b
·
1 Parent(s): 356c7d4

Add load_contest_file function to handle contest data loading in app.py

Browse files

- Introduced a new function `load_contest_file` in `load_contest_file.py` to streamline the loading and processing of contest data from uploaded files.
- Updated `app.py` to utilize `load_contest_file` instead of `load_file`, enhancing clarity and functionality in contest data management.
- Improved error handling and data extraction logic within the new function to ensure robust processing of uploaded contest files.

app.py CHANGED
@@ -8,6 +8,7 @@ import random
8
 
9
  ## import global functions
10
  from global_func.clean_player_name import clean_player_name
 
11
  from global_func.load_file import load_file
12
  from global_func.load_ss_file import load_ss_file
13
  from global_func.find_name_mismatches import find_name_mismatches
@@ -31,7 +32,7 @@ with tab1:
31
  del st.session_state['Contest']
32
 
33
  if Contest_file:
34
- st.session_state['Contest'], st.session_state['ownership_dict'], st.session_state['entry_list'] = load_file(Contest_file)
35
  st.session_state['Contest'] = st.session_state['Contest'].dropna(how='all')
36
  st.session_state['Contest'] = st.session_state['Contest'].reset_index(drop=True)
37
  if st.session_state['Contest'] is not None:
 
8
 
9
  ## import global functions
10
  from global_func.clean_player_name import clean_player_name
11
+ from global_func.load_contest_file import load_contest_file
12
  from global_func.load_file import load_file
13
  from global_func.load_ss_file import load_ss_file
14
  from global_func.find_name_mismatches import find_name_mismatches
 
32
  del st.session_state['Contest']
33
 
34
  if Contest_file:
35
+ st.session_state['Contest'], st.session_state['ownership_dict'], st.session_state['entry_list'] = load_contest_file(Contest_file)
36
  st.session_state['Contest'] = st.session_state['Contest'].dropna(how='all')
37
  st.session_state['Contest'] = st.session_state['Contest'].reset_index(drop=True)
38
  if st.session_state['Contest'] is not None:
global_func/load_contest_file.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ import pandas as pd
4
+ import time
5
+ from fuzzywuzzy import process
6
+
7
+ ## import global functions
8
+ from global_func.clean_player_name import clean_player_name
9
+
10
+ def load_contest_file(upload):
11
+ pos_values = ['P', 'C', '1B', '2B', '3B', 'SS', 'OF']
12
+ if upload is not None:
13
+ try:
14
+ if upload.name.endswith('.csv'):
15
+ raw_df = pd.read_csv(upload)
16
+ elif upload.name.endswith(('.xls', '.xlsx')):
17
+ raw_df = pd.read_excel(upload)
18
+ else:
19
+ st.error('Please upload either a CSV or Excel file')
20
+ return None
21
+
22
+ df = raw_df[['EntryId', 'EntryName', 'TimeRemaining', 'Points', 'Lineup', 'Player', 'Roster Position', '%Drafted', 'FPTS']]
23
+ df = df.rename(columns={'Roster Position': 'Pos', '%Drafted': 'Own'})
24
+
25
+ # Split EntryName into base name and entry count
26
+ df['BaseName'] = df['EntryName'].str.replace(r'\s*\(\d+/\d+\)$', '', regex=True)
27
+ df['EntryCount'] = df['EntryName'].str.extract(r'\((\d+/\d+)\)')
28
+ df['EntryCount'] = df['EntryCount'].fillna('1/1') # Default to 1/1 if no entry count
29
+
30
+ # Split the lineup string by replacing position indicators with commas
31
+ # We need to ensure we only replace position indicators that are at the start of a player entry
32
+ # and not those that might appear within player names
33
+ df['Lineup'] = df['Lineup'].str.replace(r'\b(' + '|'.join(pos_values) + r')\b', r'\1,', regex=True)
34
+
35
+ # Split into individual columns and remove position indicators
36
+ # First, determine the maximum number of players in any lineup
37
+ max_players = int(df['Lineup'].str.split(',').str.len().max())
38
+
39
+ if max_players <= 0:
40
+ st.error('No valid lineups found in the uploaded file')
41
+ return None
42
+
43
+ # Create columns for each player
44
+ for i in range(1, max_players - 1):
45
+ df[i] = df['Lineup'].str.split(',').str[i].str.strip()
46
+ # Remove position indicators from the end of each entry
47
+ df[i] = df[i].str.replace(r'\s+(' + '|'.join(pos_values) + r')$', '', regex=True)
48
+ ownership_dict = dict(zip(df['Player'], df['Own']))
49
+ cleaned_df = df.drop(columns=['EntryId', 'EntryName', 'TimeRemaining', 'Points', 'Lineup', 'Player', 'Pos', 'Own', 'FPTS'])
50
+ entry_list = list(set(df['BaseName']))
51
+ entry_list.sort()
52
+
53
+ return cleaned_df, ownership_dict, entry_list
54
+ except Exception as e:
55
+ st.error(f'Error loading file: {str(e)}')
56
+ return None
57
+ return None
global_func/load_file.py CHANGED
@@ -8,49 +8,23 @@ from fuzzywuzzy import process
8
  from global_func.clean_player_name import clean_player_name
9
 
10
  def load_file(upload):
11
- pos_values = ['P', 'C', '1B', '2B', '3B', 'SS', 'OF']
12
  if upload is not None:
13
  try:
14
  if upload.name.endswith('.csv'):
15
- raw_df = pd.read_csv(upload)
16
  elif upload.name.endswith(('.xls', '.xlsx')):
17
- raw_df = pd.read_excel(upload)
18
  else:
19
  st.error('Please upload either a CSV or Excel file')
20
  return None
21
 
22
- df = raw_df[['EntryId', 'EntryName', 'TimeRemaining', 'Points', 'Lineup', 'Player', 'Roster Position', '%Drafted', 'FPTS']]
23
- df = df.rename(columns={'Roster Position': 'Pos', '%Drafted': 'Own'})
24
 
25
- # Split EntryName into base name and entry count
26
- df['BaseName'] = df['EntryName'].str.replace(r'\s*\(\d+/\d+\)$', '', regex=True)
27
- df['EntryCount'] = df['EntryName'].str.extract(r'\((\d+/\d+)\)')
28
- df['EntryCount'] = df['EntryCount'].fillna('1/1') # Default to 1/1 if no entry count
29
 
30
- # Split the lineup string by replacing position indicators with commas
31
- # We need to ensure we only replace position indicators that are at the start of a player entry
32
- # and not those that might appear within player names
33
- df['Lineup'] = df['Lineup'].str.replace(r'\b(' + '|'.join(pos_values) + r')\b', r'\1,', regex=True)
34
-
35
- # Split into individual columns and remove position indicators
36
- # First, determine the maximum number of players in any lineup
37
- max_players = int(df['Lineup'].str.split(',').str.len().max())
38
-
39
- if max_players <= 0:
40
- st.error('No valid lineups found in the uploaded file')
41
- return None
42
-
43
- # Create columns for each player
44
- for i in range(1, max_players - 1):
45
- df[i] = df['Lineup'].str.split(',').str[i].str.strip()
46
- # Remove position indicators from the end of each entry
47
- df[i] = df[i].str.replace(r'\s+(' + '|'.join(pos_values) + r')$', '', regex=True)
48
- ownership_dict = dict(zip(df['Player'], df['Own']))
49
- cleaned_df = df.drop(columns=['EntryId', 'EntryName', 'TimeRemaining', 'Points', 'Lineup', 'Player', 'Pos', 'Own', 'FPTS'])
50
- entry_list = list(set(df['BaseName']))
51
- entry_list.sort()
52
-
53
- return cleaned_df, ownership_dict, entry_list
54
  except Exception as e:
55
  st.error(f'Error loading file: {str(e)}')
56
  return None
 
8
  from global_func.clean_player_name import clean_player_name
9
 
10
  def load_file(upload):
 
11
  if upload is not None:
12
  try:
13
  if upload.name.endswith('.csv'):
14
+ df = pd.read_csv(upload)
15
  elif upload.name.endswith(('.xls', '.xlsx')):
16
+ df = pd.read_excel(upload)
17
  else:
18
  st.error('Please upload either a CSV or Excel file')
19
  return None
20
 
21
+ export_df = df.copy()
 
22
 
23
+ for col in df.columns:
24
+ if df[col].dtype == 'object':
25
+ df[col] = df[col].apply(lambda x: clean_player_name(x) if isinstance(x, str) else x)
 
26
 
27
+ return export_df, df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  except Exception as e:
29
  st.error(f'Error loading file: {str(e)}')
30
  return None