feat(ui_remove): improve remove

change remove function to button based action
pull/86/head
MakinoHaruka 2023-12-14 00:45:43 +08:00
parent 20fc95e16b
commit 04d93cf12a
No known key found for this signature in database
GPG Key ID: 39F856B2368E37BE
1 changed files with 63 additions and 52 deletions

View File

@ -9,15 +9,15 @@ from scripts.mo.utils import find_preview_file, find_info_file
def _on_id_change(record_id):
logger.info(f'_on_id_change record_id: {record_id}', )
logger.info('_on_id_change record_id: %s', record_id)
if not record_id:
return [
gr.HTML.update(value='Record is missing.'),
gr.Button.update(visible=True),
gr.Button.update(visible=False),
gr.Checkbox.update(visible=False),
gr.Checkbox.update(visible=False)
gr.Button.update(visible=False),
gr.Button.update(visible=False),
gr.Button.update(visible=False)
]
if os.path.isfile(record_id):
@ -25,63 +25,68 @@ def _on_id_change(record_id):
gr.HTML.update(
value=styled.alert_danger(f'Are you sure you what to remove "{os.path.basename(record_id)}"?')),
gr.Button.update(visible=True),
gr.Button.update(visible=False),
gr.Button.update(visible=True),
gr.Checkbox.update(visible=False, value=True),
gr.Checkbox.update(visible=False, value=True)
gr.Button.update(visible=False)
]
else:
record = env.storage.get_record_by_id(record_id)
if record is None:
return [
gr.HTML.update(value='Record was not found in database.'),
gr.Button.update(visible=False),
gr.Button.update(visible=False),
gr.Button.update(visible=False),
gr.Button.update(visible=False)
]
elif os.path.exists(record.location):
return [
gr.HTML.update(value=styled.alert_danger(f'Are you sure you what to remove "{record.name}"?')),
gr.Button.update(visible=True),
gr.Button.update(visible=True),
gr.Button.update(visible=True),
gr.Button.update(visible=True)
]
else:
return [
gr.HTML.update(value=styled.alert_danger(f'Are you sure you what to remove "{record.name}"?')),
gr.Button.update(visible=True),
gr.Button.update(visible=True),
gr.Button.update(visible=False),
gr.Checkbox.update(visible=False),
gr.Checkbox.update(visible=False)
gr.Button.update(visible=False)
]
return [
gr.HTML.update(value=styled.alert_danger(f'Are you sure you what to remove "{record.name}"?')),
gr.Button.update(visible=True),
gr.Button.update(visible=True),
gr.Checkbox.update(visible=bool(record.location), value=True),
gr.Checkbox.update(visible=bool(record.location), value=True)
]
def _on_remove_record_button_click(record_id):
env.storage.remove_record(record_id)
logger.info('removed record: %s', record_id)
return generate_ui_token()
def _on_remove_click(record_id, remove_record, remove_files):
logger.info(f'_on_remove_click record_id: {record_id} remove_record: {remove_record} remove_files: {remove_files}')
def _on_remove_files_button_click(record_id):
if os.path.isfile(record_id):
preview_file = find_preview_file(record_id)
if preview_file is not None and os.path.isfile(preview_file):
os.remove(preview_file)
info_file = find_info_file(record_id)
if info_file is not None and os.path.isfile(info_file):
os.remove(info_file)
os.remove(record_id)
else:
record = env.storage.get_record_by_id(record_id)
if record.location and os.path.exists(record.location):
logger.info('removed model file: %s', record.location)
os.remove(record.location)
if remove_record:
env.storage.remove_record(record_id)
preview_path = find_preview_file(record.location)
if preview_path and os.path.exists(preview_path):
logger.info('removed preview file: %s', preview_path)
os.remove(preview_path)
if record.location and remove_files:
if record.location and os.path.exists(record.location):
os.remove(record.location)
preview_path = find_preview_file(record.location)
if preview_path and os.path.exists(preview_path):
os.remove(preview_path)
info_file = find_info_file(record.location)
if info_file is not None and os.path.isfile(info_file):
os.remove(info_file)
info_file = find_info_file(record.location)
if info_file is not None and os.path.isfile(info_file):
logger.info('removed info file: %s', info_file)
os.remove(info_file)
return generate_ui_token()
def _on_remove_both_button(record_id):
_on_remove_files_button_click(record_id)
_on_remove_record_button_click(record_id)
return generate_ui_token()
def remove_ui_block():
with gr.Blocks():
@ -91,25 +96,31 @@ def remove_ui_block():
gr.Markdown('## Record removal')
html_widget = gr.HTML()
with gr.Column():
gr.Markdown()
remove_record_checkbox = gr.Checkbox(value=True, label='Remove record', interactive=True)
remove_files_checkbox = gr.Checkbox(value=True, label='Remove files', interactive=True)
gr.Markdown()
with gr.Row():
gr.Markdown()
cancel_button = gr.Button('Cancel', visible=False)
remove_button = gr.Button('Remove', visible=False, elem_id='mo_button_remove')
cancel_button = gr.Button('Cancel')
remove_record_button = gr.Button('Remove Record', visible=False, elem_id='mo_button_remove')
remove_files_button = gr.Button('Remove Files', visible=False, elem_id='mo_button_remove')
remove_both_button = gr.Button('Remove Record and Files', visible=False, elem_id='mo_button_remove')
gr.Markdown()
remove_record_button.click(_on_remove_record_button_click,
inputs=remove_id_box,
outputs=remove_back_box)
remove_files_button.click(_on_remove_files_button_click,
inputs=remove_id_box,
outputs=remove_back_box)
remove_both_button.click(_on_remove_both_button,
inputs=remove_id_box,
outputs=remove_back_box)
cancel_button.click(fn=None, _js='navigateBack')
remove_button.click(_on_remove_click, inputs=[remove_id_box, remove_record_checkbox, remove_files_checkbox],
outputs=remove_back_box)
remove_id_box.change(_on_id_change, inputs=remove_id_box,
outputs=[html_widget, cancel_button, remove_button, remove_record_checkbox,
remove_files_checkbox])
outputs=[html_widget, cancel_button, remove_record_button, remove_files_button,
remove_both_button])
remove_back_box.change(fn=None, _js='navigateHome')
return remove_id_box