James McCool commited on
Commit
f8485d4
·
1 Parent(s): 1d2969e

Add create_position_export_dict function in app.py to generate position-specific export dictionaries from CSV files, enhancing player data handling and mapping in export functionality.

Browse files
Files changed (1) hide show
  1. app.py +39 -1
app.py CHANGED
@@ -32,6 +32,42 @@ stacking_sports = ['MLB', 'NHL', 'NFL', 'LOL']
32
  player_wrong_names_mlb = ['Enrique Hernandez', 'Joseph Cantillo', 'Mike Soroka', 'Jakob Bauers', 'Temi Fágbénlé']
33
  player_right_names_mlb = ['Kike Hernandez', 'Joey Cantillo', 'Michael Soroka', 'Jake Bauers', 'Temi Fagbenle']
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  with st.container():
36
 
37
  col1, col2, col3, col4 = st.columns(4)
@@ -1549,7 +1585,9 @@ with tab2:
1549
 
1550
  for col in st.session_state['export_file'].columns:
1551
  if col not in excluded_cols:
1552
- st.session_state['export_file'][col] = st.session_state['export_file'][col].map(st.session_state['export_dict'])
 
 
1553
 
1554
  if 'export_file' in st.session_state:
1555
  download_port, merge_port, partial_col, clear_export, blank_export_col = st.columns([1, 1, 1, 1, 8])
 
32
  player_wrong_names_mlb = ['Enrique Hernandez', 'Joseph Cantillo', 'Mike Soroka', 'Jakob Bauers', 'Temi Fágbénlé']
33
  player_right_names_mlb = ['Kike Hernandez', 'Joey Cantillo', 'Michael Soroka', 'Jake Bauers', 'Temi Fagbenle']
34
 
35
+ def create_position_export_dict(column_name, csv_file):
36
+ """
37
+ Create a position-specific export dictionary based on the column name.
38
+ Strips numbers from column names to get the position (e.g., 'SP1' -> 'SP', 'G2' -> 'G')
39
+ """
40
+ try:
41
+ # Remove any numbers from the column name to get the position
42
+ import re
43
+ position_filter = re.sub(r'\d+', '', column_name)
44
+
45
+ # Filter CSV file by position
46
+ if 'Position' in csv_file.columns:
47
+ filtered_df = csv_file[
48
+ csv_file['Position'].str.contains(position_filter, na=False, regex=False)
49
+ ]
50
+ else:
51
+ # Fallback to all players if no position column found
52
+ filtered_df = csv_file
53
+
54
+ # Create the export dictionary for this position
55
+
56
+ try:
57
+ filtered_df = filtered_df.sort_values(by='Salary', ascending=False).drop_duplicates(subset=['Name'])
58
+ return dict(zip(filtered_df['Name'], filtered_df['Name + ID']))
59
+ except:
60
+ try:
61
+ filtered_df = filtered_df.sort_values(by='Salary', ascending=False).drop_duplicates(subset=['Nickname'])
62
+ return dict(zip(filtered_df['Nickname'], filtered_df['Id']))
63
+ except:
64
+ # Final fallback
65
+ return {}
66
+
67
+ except Exception as e:
68
+ st.error(f"Error creating position export dict for {column_name}: {str(e)}")
69
+ return {}
70
+
71
  with st.container():
72
 
73
  col1, col2, col3, col4 = st.columns(4)
 
1585
 
1586
  for col in st.session_state['export_file'].columns:
1587
  if col not in excluded_cols:
1588
+ # Create position-specific export dictionary on the fly
1589
+ position_dict = create_position_export_dict(col, st.session_state['csv_file'])
1590
+ st.session_state['export_file'][col] = st.session_state['export_file'][col].map(position_dict)
1591
 
1592
  if 'export_file' in st.session_state:
1593
  download_port, merge_port, partial_col, clear_export, blank_export_col = st.columns([1, 1, 1, 1, 8])