duongthienz commited on
Commit
63845ed
Β·
verified Β·
1 Parent(s): 2cb98b2

Fix an issue where rename is not showing in role

Browse files
Files changed (1) hide show
  1. state.py +32 -29
state.py CHANGED
@@ -120,17 +120,22 @@ def removeCategory(index):
120
  del st.session_state.categorySelect[index]
121
 
122
 
123
- def updateCategoryOptions():
124
- """Store tokens ('fname: SPEAKER_##') in the global categorySelect lists."""
 
 
 
 
125
  if st.session_state.resetResult:
126
  return
127
- # Rebuild categorySelect from widget values, then sync widget keys back to
128
- # match β€” this keeps the data model and the displayed selection in lockstep.
129
  for i, category in enumerate(st.session_state.categories):
130
  ms_key = f"multiselect_{category}"
131
- new_val = list(st.session_state.get(ms_key, []))
132
- st.session_state.categorySelect[i] = new_val
133
- st.session_state[ms_key] = new_val
 
 
134
  # Recompute unusedSpeakers for all files
135
  all_assigned_tokens = {
136
  token
@@ -198,11 +203,10 @@ def removeGlobalRename(index):
198
  _write_rename(token, "")
199
  st.session_state.pop(_global_rename_key(index), None)
200
  del st.session_state.globalRenames[index]
201
- # Shift remaining widget keys down and sync them to their data model
 
202
  for i in range(index, len(st.session_state.globalRenames)):
203
- st.session_state[_global_rename_key(i)] = list(
204
- st.session_state.globalRenames[i]["speakers"]
205
- )
206
 
207
 
208
  def apply_inline_rename(currFile, raw_sp, new_name):
@@ -228,27 +232,23 @@ def apply_inline_rename(currFile, raw_sp, new_name):
228
  st.toast(f"Reverted {raw_sp} to original label")
229
 
230
 
231
- def on_grename_change(idx):
232
  """Callback for the sidebar rename multiselect at position idx.
233
 
234
- Diffs old vs new speakers list:
235
- - Newly added tokens β†’ enforce exclusivity, write name into speakerRenames.
236
- - Removed tokens β†’ revert speakerRenames for those tokens.
237
-
238
- Spurious-callback guard: Streamlit sometimes re-fires this callback with an
239
- empty widget value when available_tokens changes (e.g. another entry just
240
- claimed a token, shrinking options). We detect this by checking whether the
241
- widget value went to empty while the data model still has speakers β€” if ALL
242
- of the previous speakers are still valid options for this entry we treat the
243
- empty report as a Streamlit glitch and restore the widget key from the data
244
- model instead of acting on it.
245
  """
246
  grkey = _global_rename_key(idx)
247
  entry = st.session_state.globalRenames[idx]
248
  name = entry["name"]
249
 
250
- prev = list(entry["speakers"]) # current data-model state (ordered)
251
- reported = list(st.session_state.get(grkey, []))
 
 
 
 
 
252
 
253
  # Build the set of tokens that are legitimately available for this entry
254
  # right now (not claimed by any OTHER entry).
@@ -266,7 +266,8 @@ def on_grename_change(idx):
266
  if not reported and len(prev) > 1:
267
  all_still_valid = all(t not in other_claimed for t in prev)
268
  if all_still_valid:
269
- st.session_state[grkey] = prev
 
270
  return
271
 
272
  prev_set = set(prev)
@@ -281,7 +282,8 @@ def on_grename_change(idx):
281
  # Do NOT pop the key β€” popping causes Streamlit to re-initialise the widget
282
  # to [] on the next render (because no `default=` is passed), erasing the
283
  # selection visually even though the data model is correct.
284
- st.session_state[grkey] = list(entry["speakers"])
 
285
 
286
  # Enforce exclusivity and write renames for newly added tokens
287
  for token in added:
@@ -290,8 +292,9 @@ def on_grename_change(idx):
290
  continue
291
  if token in other_entry["speakers"]:
292
  other_entry["speakers"].remove(token)
293
- # Sync the other entry's widget key too (same reason β€” don't pop)
294
- st.session_state[_global_rename_key(other_idx)] = list(other_entry["speakers"])
 
295
  _write_rename(token, "")
296
  _write_rename(token, name)
297
 
 
120
  del st.session_state.categorySelect[index]
121
 
122
 
123
+ def updateCategoryOptions(token_display_map=None):
124
+ """Store tokens ('fname: SPEAKER_##') in the global categorySelect lists.
125
+
126
+ token_display_map: dict {display_label -> raw_token} passed from ui.py.
127
+ Widget keys hold display labels; categorySelect must hold raw tokens.
128
+ """
129
  if st.session_state.resetResult:
130
  return
131
+ display_to_raw = token_display_map or {}
 
132
  for i, category in enumerate(st.session_state.categories):
133
  ms_key = f"multiselect_{category}"
134
+ display_vals = list(st.session_state.get(ms_key, []))
135
+ # Translate display labels back to raw tokens for the data model
136
+ raw_vals = [display_to_raw.get(t, t) for t in display_vals]
137
+ st.session_state.categorySelect[i] = raw_vals
138
+ # Keep widget key as display labels (do not overwrite with raw)
139
  # Recompute unusedSpeakers for all files
140
  all_assigned_tokens = {
141
  token
 
203
  _write_rename(token, "")
204
  st.session_state.pop(_global_rename_key(index), None)
205
  del st.session_state.globalRenames[index]
206
+ # Shift remaining widget keys down β€” ui.py will re-sync them to display
207
+ # labels on the next render, so just clear them to force a clean re-seed.
208
  for i in range(index, len(st.session_state.globalRenames)):
209
+ st.session_state.pop(_global_rename_key(i), None)
 
 
210
 
211
 
212
  def apply_inline_rename(currFile, raw_sp, new_name):
 
232
  st.toast(f"Reverted {raw_sp} to original label")
233
 
234
 
235
+ def on_grename_change(idx, token_display_map=None):
236
  """Callback for the sidebar rename multiselect at position idx.
237
 
238
+ token_display_map: dict {display_label -> raw_token} passed from ui.py.
239
+ Widget keys hold display labels; entry["speakers"] must hold raw tokens.
 
 
 
 
 
 
 
 
 
240
  """
241
  grkey = _global_rename_key(idx)
242
  entry = st.session_state.globalRenames[idx]
243
  name = entry["name"]
244
 
245
+ display_to_raw = token_display_map or {}
246
+ raw_to_display = {v: k for k, v in display_to_raw.items()}
247
+
248
+ prev = list(entry["speakers"]) # raw tokens in data model
249
+ # Widget reports display labels β€” translate back to raw
250
+ reported_display = list(st.session_state.get(grkey, []))
251
+ reported = [display_to_raw.get(t, t) for t in reported_display]
252
 
253
  # Build the set of tokens that are legitimately available for this entry
254
  # right now (not claimed by any OTHER entry).
 
266
  if not reported and len(prev) > 1:
267
  all_still_valid = all(t not in other_claimed for t in prev)
268
  if all_still_valid:
269
+ # Restore widget key as display labels
270
+ st.session_state[grkey] = [raw_to_display.get(t, t) for t in prev]
271
  return
272
 
273
  prev_set = set(prev)
 
282
  # Do NOT pop the key β€” popping causes Streamlit to re-initialise the widget
283
  # to [] on the next render (because no `default=` is passed), erasing the
284
  # selection visually even though the data model is correct.
285
+ # Sync widget key as display labels
286
+ st.session_state[grkey] = [raw_to_display.get(t, t) for t in entry["speakers"]]
287
 
288
  # Enforce exclusivity and write renames for newly added tokens
289
  for token in added:
 
292
  continue
293
  if token in other_entry["speakers"]:
294
  other_entry["speakers"].remove(token)
295
+ st.session_state[_global_rename_key(other_idx)] = [
296
+ raw_to_display.get(t, t) for t in other_entry["speakers"]
297
+ ]
298
  _write_rename(token, "")
299
  _write_rename(token, name)
300