unc-ho commited on
Commit
79b8fe6
·
1 Parent(s): cdb55e9

add update name and duplicate

Browse files
Files changed (1) hide show
  1. app.py +67 -2
app.py CHANGED
@@ -76,8 +76,10 @@ with gr.Blocks(title=settings.APP_TITLE) as demo:
76
 
77
  with gr.Group(elem_classes=["section-card"]):
78
  with gr.Accordion("Manage Notebook", open=False):
79
- gr.Markdown("<span class='small-muted'>Rename, delete, or duplicate this notebook.</span>")
80
- delete_nb = gr.Button("Delete", variant="danger", elem_classes=["full-width"])
 
 
81
 
82
  with gr.Group(elem_classes=["section-card"]):
83
  gr.Markdown("### Ingested Sources")
@@ -167,6 +169,69 @@ with gr.Blocks(title=settings.APP_TITLE) as demo:
167
  outputs=[notebook, notebook_choices],
168
  )
169
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
  # Upload source -> update global ingested_sources and table
171
  upload.change(
172
  fn=add_source,
 
76
 
77
  with gr.Group(elem_classes=["section-card"]):
78
  with gr.Accordion("Manage Notebook", open=False):
79
+ updated_name = gr.Textbox(label="Update name", interactive=True)
80
+ rename_nb = gr.Button("Rename", variant="secondary", elem_classes=["full-width"])
81
+ delete_nb = gr.Button("Delete", variant="stop", elem_classes=["full-width"])
82
+ duplicate_nb = gr.Button("Duplicate", variant="secondary", elem_classes=["full-width"])
83
 
84
  with gr.Group(elem_classes=["section-card"]):
85
  gr.Markdown("### Ingested Sources")
 
169
  outputs=[notebook, notebook_choices],
170
  )
171
 
172
+ def rename_notebook(old_name, new_name, choices):
173
+ old_name = (old_name or "").strip()
174
+ new_name = (new_name or "").strip()
175
+ choices = list(choices)
176
+
177
+ if not old_name or not new_name or old_name not in choices:
178
+ return gr.update(), choices, "" # no change, clear box
179
+
180
+ if new_name in choices:
181
+ return gr.update(), choices, "" # name already exists
182
+
183
+ # replace in-place
184
+ idx = choices.index(old_name)
185
+ choices[idx] = new_name
186
+
187
+ return gr.update(choices=choices, value=new_name), choices, ""
188
+
189
+
190
+ rename_nb.click(
191
+ rename_notebook,
192
+ inputs=[notebook, updated_name, notebook_choices],
193
+ outputs=[notebook, notebook_choices, updated_name],
194
+ )
195
+
196
+ def make_copy_name(base: str, choices: list[str]) -> str:
197
+ base = (base or "").strip()
198
+ if not base:
199
+ return ""
200
+
201
+ # First try "(copy)"
202
+ candidate = f"{base} (copy)"
203
+ if candidate not in choices:
204
+ return candidate
205
+
206
+ # Then "(copy 2)", "(copy 3)", ...
207
+ i = 2
208
+ while True:
209
+ candidate = f"{base} (copy {i})"
210
+ if candidate not in choices:
211
+ return candidate
212
+ i += 1
213
+
214
+
215
+ def duplicate_notebook(selected, choices):
216
+ selected = (selected or "").strip()
217
+ if not selected:
218
+ return gr.update(), choices
219
+
220
+ choices = list(choices)
221
+ new_name = make_copy_name(selected, choices)
222
+ if not new_name:
223
+ return gr.update(), choices
224
+
225
+ choices.append(new_name)
226
+ return gr.update(choices=choices, value=new_name), choices
227
+
228
+
229
+ duplicate_nb.click(
230
+ duplicate_notebook,
231
+ inputs=[notebook, notebook_choices],
232
+ outputs=[notebook, notebook_choices],
233
+ )
234
+
235
  # Upload source -> update global ingested_sources and table
236
  upload.change(
237
  fn=add_source,