Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -39,7 +39,7 @@ def load_excel(file):
|
|
39 |
df["role2"] = df["role2"].astype(str).str.strip()
|
40 |
df["role3"] = df["role3"].astype(str).str.strip()
|
41 |
df["xb90"] = pd.to_numeric(df["xb90"], errors="coerce")
|
42 |
-
# Get union of roles.
|
43 |
roles = pd.concat([df["role1"], df["role2"], df["role3"]]).dropna().unique().tolist()
|
44 |
roles = sorted(roles)
|
45 |
roles = [""] + roles # Prepend blank.
|
@@ -75,7 +75,7 @@ def update_players(selected_role):
|
|
75 |
|
76 |
def sync_pref(selected_role):
|
77 |
"""
|
78 |
-
If the selected role is "Por", returns "Por"
|
79 |
Otherwise, returns an empty string.
|
80 |
"""
|
81 |
if selected_role == "Por":
|
@@ -100,6 +100,7 @@ def get_xb90_value(role, player):
|
|
100 |
def get_candidate_roles(candidate_name):
|
101 |
"""
|
102 |
Returns a comma-separated string of roles for the candidate.
|
|
|
103 |
"""
|
104 |
global df
|
105 |
if df is None or not candidate_name:
|
@@ -109,8 +110,9 @@ def get_candidate_roles(candidate_name):
|
|
109 |
roles = set()
|
110 |
for col in ["role1", "role2", "role3"]:
|
111 |
val = row.iloc[0][col]
|
112 |
-
|
113 |
-
|
|
|
114 |
return ", ".join(sorted(roles))
|
115 |
return ""
|
116 |
|
@@ -126,8 +128,7 @@ def compute_exchange(role1, player1, pref1, role2, player2, pref2, role3, player
|
|
126 |
- Excludes any candidate that is among the selected players.
|
127 |
- Generates candidate combinations and selects up to three proposals ensuring no candidate
|
128 |
is repeated across proposals.
|
129 |
-
- Enforces that if a player's role is "Por", then the preferred must be "Por".
|
130 |
-
- If a user somehow sets a different preferred value for a "Por" player, an error is returned.
|
131 |
"""
|
132 |
global df
|
133 |
logging.debug("compute_exchange: Starting computation")
|
@@ -139,7 +140,6 @@ def compute_exchange(role1, player1, pref1, role2, player2, pref2, role3, player
|
|
139 |
msg = f"Puoi scambiare un portiere solo per un altro portiere. (Giocatore {i})"
|
140 |
logging.error(msg)
|
141 |
return msg
|
142 |
-
# Automatically force preferred to Por.
|
143 |
pref = "Por"
|
144 |
xb90_val = get_xb90_value(r, p)
|
145 |
if xb90_val == "":
|
@@ -210,7 +210,7 @@ def compute_exchange(role1, player1, pref1, role2, player2, pref2, role3, player
|
|
210 |
combo_diffs.append((combo, combo_value, diff))
|
211 |
combo_diffs.sort(key=lambda x: x[2])
|
212 |
|
213 |
-
# Choose up to three proposals ensuring
|
214 |
selected_options = []
|
215 |
used_candidates = set()
|
216 |
for combo, combo_value, diff in combo_diffs:
|
@@ -308,7 +308,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css="""
|
|
308 |
role1.change(fn=update_players, inputs=role1, outputs=player1)
|
309 |
role2.change(fn=update_players, inputs=role2, outputs=player2)
|
310 |
role3.change(fn=update_players, inputs=role3, outputs=player3)
|
311 |
-
# Sync preferred role
|
312 |
role1.change(fn=sync_pref, inputs=role1, outputs=pref1)
|
313 |
role2.change(fn=sync_pref, inputs=role2, outputs=pref2)
|
314 |
role3.change(fn=sync_pref, inputs=role3, outputs=pref3)
|
|
|
39 |
df["role2"] = df["role2"].astype(str).str.strip()
|
40 |
df["role3"] = df["role3"].astype(str).str.strip()
|
41 |
df["xb90"] = pd.to_numeric(df["xb90"], errors="coerce")
|
42 |
+
# Get union of roles from all three columns.
|
43 |
roles = pd.concat([df["role1"], df["role2"], df["role3"]]).dropna().unique().tolist()
|
44 |
roles = sorted(roles)
|
45 |
roles = [""] + roles # Prepend blank.
|
|
|
75 |
|
76 |
def sync_pref(selected_role):
|
77 |
"""
|
78 |
+
If the selected role is "Por", returns "Por" for the corresponding preferred proposal role.
|
79 |
Otherwise, returns an empty string.
|
80 |
"""
|
81 |
if selected_role == "Por":
|
|
|
100 |
def get_candidate_roles(candidate_name):
|
101 |
"""
|
102 |
Returns a comma-separated string of roles for the candidate.
|
103 |
+
Omits any role that is 'nan' (case-insensitive).
|
104 |
"""
|
105 |
global df
|
106 |
if df is None or not candidate_name:
|
|
|
110 |
roles = set()
|
111 |
for col in ["role1", "role2", "role3"]:
|
112 |
val = row.iloc[0][col]
|
113 |
+
val_str = str(val).strip()
|
114 |
+
if pd.notna(val) and val_str and val_str.lower() != "nan":
|
115 |
+
roles.add(val_str)
|
116 |
return ", ".join(sorted(roles))
|
117 |
return ""
|
118 |
|
|
|
128 |
- Excludes any candidate that is among the selected players.
|
129 |
- Generates candidate combinations and selects up to three proposals ensuring no candidate
|
130 |
is repeated across proposals.
|
131 |
+
- Enforces that if a player's role is "Por", then the preferred must be "Por"; if not, returns an error.
|
|
|
132 |
"""
|
133 |
global df
|
134 |
logging.debug("compute_exchange: Starting computation")
|
|
|
140 |
msg = f"Puoi scambiare un portiere solo per un altro portiere. (Giocatore {i})"
|
141 |
logging.error(msg)
|
142 |
return msg
|
|
|
143 |
pref = "Por"
|
144 |
xb90_val = get_xb90_value(r, p)
|
145 |
if xb90_val == "":
|
|
|
210 |
combo_diffs.append((combo, combo_value, diff))
|
211 |
combo_diffs.sort(key=lambda x: x[2])
|
212 |
|
213 |
+
# Choose up to three proposals ensuring no candidate is repeated across proposals.
|
214 |
selected_options = []
|
215 |
used_candidates = set()
|
216 |
for combo, combo_value, diff in combo_diffs:
|
|
|
308 |
role1.change(fn=update_players, inputs=role1, outputs=player1)
|
309 |
role2.change(fn=update_players, inputs=role2, outputs=player2)
|
310 |
role3.change(fn=update_players, inputs=role3, outputs=player3)
|
311 |
+
# Sync preferred role if "Por" is selected.
|
312 |
role1.change(fn=sync_pref, inputs=role1, outputs=pref1)
|
313 |
role2.change(fn=sync_pref, inputs=role2, outputs=pref2)
|
314 |
role3.change(fn=sync_pref, inputs=role3, outputs=pref3)
|