duongthienz commited on
Commit
c9a70a9
·
verified ·
1 Parent(s): 5b6254c

Update ui.py

Browse files
Files changed (1) hide show
  1. ui.py +30 -45
ui.py CHANGED
@@ -145,7 +145,7 @@ def render_data_table(tableDF, speakerNames, raw_to_display, currFile):
145
  # ---------------------------------------------------------------------------
146
 
147
  def render_speaker_samples_tab(speakerNames, raw_to_display, currFile):
148
- """Render a table: Speaker (with inline ✎ rename + scrollable dropdown history) | Audio Sample | ↺ button."""
149
  file_samples = st.session_state.speakerClips.get(currFile, {})
150
  has_waveform = currFile in st.session_state.speakerWaveforms
151
  has_samples = bool(file_samples)
@@ -165,6 +165,8 @@ def render_speaker_samples_tab(speakerNames, raw_to_display, currFile):
165
  if not has_samples:
166
  st.info("Audio samples are only available for files analyzed from audio (not RTTM/CSV/TXT).")
167
 
 
 
168
  for sp in speakerNames:
169
  display_name = raw_to_display.get(sp, sp)
170
  edit_key = (currFile, sp)
@@ -174,62 +176,45 @@ def render_speaker_samples_tab(speakerNames, raw_to_display, currFile):
174
 
175
  # --- Speaker cell ---
176
  if is_editing:
 
177
  input_key = f"inline_rename_input_{currFile}_{sp}"
178
  confirm_key = f"inline_rename_confirm_{currFile}_{sp}"
179
  cancel_key = f"inline_rename_cancel_{currFile}_{sp}"
180
 
181
- history = st.session_state.inline_rename_history
182
- current_val = display_name if display_name != sp else ""
183
 
184
- with row_cols[0]:
185
- # Render a native <datalist> above the Streamlit input.
186
- # The browser connects them by matching the input's list= attribute,
187
- # which we set on the nearest text input via scoped JS.
188
- if history:
189
- dl_id = f"dl_{sp}_{currFile}".replace(" ", "_").replace(".", "_")
190
- options = "\n".join(f'<option value="{n}"></option>' for n in history)
191
- st.markdown(
192
- f"""
193
- <datalist id="{dl_id}">{options}</datalist>
194
- <style>
195
- /* Make the native dropdown scrollable at ~160px */
196
- #{dl_id} {{ max-height: 160px; overflow-y: auto; }}
197
- </style>
198
- <script>
199
- (function() {{
200
- // Wait one tick for Streamlit to render the input,
201
- // then attach the datalist to the very next text input
202
- // that appears after this script tag.
203
- var dl = document.getElementById('{dl_id}');
204
- var observer = new MutationObserver(function() {{
205
- var inputs = document.querySelectorAll('input[data-testid="stTextInput"]');
206
- if (inputs.length) {{
207
- inputs[inputs.length - 1].setAttribute('list', '{dl_id}');
208
- observer.disconnect();
209
- }}
210
- }});
211
- observer.observe(document.body, {{ childList: true, subtree: true }});
212
- // Also try immediately
213
- var inputs = document.querySelectorAll('input[data-testid="stTextInput"]');
214
- if (inputs.length) inputs[inputs.length - 1].setAttribute('list', '{dl_id}');
215
- }})();
216
- </script>
217
- """,
218
- unsafe_allow_html=True,
219
- )
220
 
221
- new_name = st.text_input(
222
- "Rename",
223
- value=current_val,
224
- key=input_key,
 
 
225
  label_visibility="collapsed",
226
- placeholder=f"Rename {sp}…",
227
  )
228
 
 
 
 
 
 
 
 
 
 
 
 
 
229
  btn_col1, btn_col2 = st.columns(2)
230
  if btn_col1.button("✓", key=confirm_key, help="Confirm rename"):
231
  confirmed = new_name.strip()
232
- if confirmed:
233
  apply_inline_rename(currFile, sp, confirmed)
234
  if confirmed not in st.session_state.inline_rename_history:
235
  st.session_state.inline_rename_history.append(confirmed)
 
145
  # ---------------------------------------------------------------------------
146
 
147
  def render_speaker_samples_tab(speakerNames, raw_to_display, currFile):
148
+ """Render a table: Speaker (with inline ✎ rename + history dropdown) | Audio Sample | ↺ button."""
149
  file_samples = st.session_state.speakerClips.get(currFile, {})
150
  has_waveform = currFile in st.session_state.speakerWaveforms
151
  has_samples = bool(file_samples)
 
165
  if not has_samples:
166
  st.info("Audio samples are only available for files analyzed from audio (not RTTM/CSV/TXT).")
167
 
168
+ NEW_NAME_SENTINEL = "+ Type a new name…"
169
+
170
  for sp in speakerNames:
171
  display_name = raw_to_display.get(sp, sp)
172
  edit_key = (currFile, sp)
 
176
 
177
  # --- Speaker cell ---
178
  if is_editing:
179
+ select_key = f"inline_rename_select_{currFile}_{sp}"
180
  input_key = f"inline_rename_input_{currFile}_{sp}"
181
  confirm_key = f"inline_rename_confirm_{currFile}_{sp}"
182
  cancel_key = f"inline_rename_cancel_{currFile}_{sp}"
183
 
184
+ history = st.session_state.inline_rename_history
185
+ current_val = display_name if display_name != sp else ""
186
 
187
+ # Dropdown options: history entries first, then sentinel for free text
188
+ options = history + [NEW_NAME_SENTINEL]
189
+
190
+ # Pre-select current name if it's already in history
191
+ default_idx = history.index(current_val) if current_val in history else len(history)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
 
193
+ with row_cols[0]:
194
+ chosen = st.selectbox(
195
+ "Pick or type",
196
+ options=options,
197
+ index=default_idx,
198
+ key=select_key,
199
  label_visibility="collapsed",
 
200
  )
201
 
202
+ # If sentinel selected, show a free-text input
203
+ if chosen == NEW_NAME_SENTINEL:
204
+ new_name = st.text_input(
205
+ "New name",
206
+ value=current_val,
207
+ key=input_key,
208
+ label_visibility="collapsed",
209
+ placeholder=f"Rename {sp}…",
210
+ )
211
+ else:
212
+ new_name = chosen
213
+
214
  btn_col1, btn_col2 = st.columns(2)
215
  if btn_col1.button("✓", key=confirm_key, help="Confirm rename"):
216
  confirmed = new_name.strip()
217
+ if confirmed and confirmed != NEW_NAME_SENTINEL:
218
  apply_inline_rename(currFile, sp, confirmed)
219
  if confirmed not in st.session_state.inline_rename_history:
220
  st.session_state.inline_rename_history.append(confirmed)