Spaces:
Sleeping
Sleeping
siddhartharya
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -632,7 +632,7 @@ def assign_category(bookmark):
|
|
632 |
else:
|
633 |
bookmark['category'] = 'Uncategorized'
|
634 |
|
635 |
-
def process_uploaded_file(file):
|
636 |
"""
|
637 |
Process the uploaded bookmarks file with enhanced error handling and user feedback.
|
638 |
"""
|
@@ -640,23 +640,23 @@ def process_uploaded_file(file):
|
|
640 |
logger.info("Processing uploaded file")
|
641 |
|
642 |
if file is None:
|
643 |
-
return "⚠️ Please upload a bookmarks HTML file.", '', gr.update(choices=[]),
|
644 |
-
|
645 |
try:
|
646 |
file_content = file.decode('utf-8')
|
647 |
except UnicodeDecodeError as e:
|
648 |
logger.error(f"Error decoding file: {e}")
|
649 |
-
return "⚠️ Error decoding file. Please ensure it's a valid HTML file.", '', gr.update(choices=[]),
|
650 |
-
|
651 |
try:
|
652 |
bookmarks = parse_bookmarks(file_content)
|
653 |
except Exception as e:
|
654 |
logger.error(f"Error parsing bookmarks: {e}")
|
655 |
-
return "⚠️ Error parsing the bookmarks file.", '', gr.update(choices=[]),
|
656 |
-
|
657 |
if not bookmarks:
|
658 |
-
return "⚠️ No valid bookmarks found in the file.", '', gr.update(choices=[]),
|
659 |
-
|
660 |
try:
|
661 |
logger.info("Processing bookmarks...")
|
662 |
asyncio.run(process_bookmarks_async(bookmarks))
|
@@ -675,20 +675,20 @@ def process_uploaded_file(file):
|
|
675 |
for i, bookmark in enumerate(bookmarks)]
|
676 |
|
677 |
bookmark_html = display_bookmarks()
|
678 |
-
return message, bookmark_html, gr.update(choices=choices),
|
679 |
|
680 |
except Exception as e:
|
681 |
logger.error(f"Error processing bookmarks: {e}")
|
682 |
-
return "⚠️ Error processing bookmarks. Please try again.", '', gr.update(choices=[]),
|
683 |
|
684 |
-
def delete_selected_bookmarks(selected_indices):
|
685 |
"""
|
686 |
Delete selected bookmarks with enhanced error handling.
|
687 |
"""
|
688 |
global bookmarks, faiss_index
|
689 |
|
690 |
if not selected_indices:
|
691 |
-
return "⚠️ No bookmarks selected.", gr.update(choices=[]), display_bookmarks()
|
692 |
|
693 |
try:
|
694 |
indices = [int(s.split('.')[0])-1 for s in selected_indices]
|
@@ -710,19 +710,19 @@ def delete_selected_bookmarks(selected_indices):
|
|
710 |
choices = [f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})"
|
711 |
for i, bookmark in enumerate(bookmarks)]
|
712 |
|
713 |
-
return message, gr.update(choices=choices), display_bookmarks()
|
714 |
except Exception as e:
|
715 |
logger.error(f"Error deleting bookmarks: {e}")
|
716 |
-
return "⚠️ Error deleting bookmarks.", gr.update(choices=[]), display_bookmarks()
|
717 |
|
718 |
-
def edit_selected_bookmarks_category(selected_indices, new_category):
|
719 |
"""
|
720 |
Edit category of selected bookmarks with enhanced error handling.
|
721 |
"""
|
722 |
if not selected_indices:
|
723 |
-
return "⚠️ No bookmarks selected.", gr.update(choices=[]), display_bookmarks()
|
724 |
if not new_category:
|
725 |
-
return "⚠️ No new category selected.", gr.update(choices=[]), display_bookmarks()
|
726 |
|
727 |
try:
|
728 |
indices = [int(s.split('.')[0])-1 for s in selected_indices]
|
@@ -739,10 +739,10 @@ def edit_selected_bookmarks_category(selected_indices, new_category):
|
|
739 |
choices = [f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})"
|
740 |
for i, bookmark in enumerate(bookmarks)]
|
741 |
|
742 |
-
return message, gr.update(choices=choices), display_bookmarks()
|
743 |
except Exception as e:
|
744 |
logger.error(f"Error updating categories: {e}")
|
745 |
-
return "⚠️ Error updating categories.", gr.update(choices=[]), display_bookmarks()
|
746 |
|
747 |
def export_bookmarks():
|
748 |
"""
|
@@ -794,9 +794,9 @@ def export_bookmarks():
|
|
794 |
# Add bookmark
|
795 |
dt = soup.new_tag('DT')
|
796 |
a = soup.new_tag('A', href=bookmark['url'])
|
797 |
-
if 'add_date' in bookmark:
|
798 |
a['ADD_DATE'] = bookmark['add_date']
|
799 |
-
if 'icon' in bookmark:
|
800 |
a['ICON'] = bookmark['icon']
|
801 |
a.string = bookmark['title']
|
802 |
dt.append(a)
|
@@ -918,6 +918,10 @@ def build_app():
|
|
918 |
delete_button = gr.Button("Delete Selected Bookmarks")
|
919 |
export_button = gr.Button("Export Bookmarks")
|
920 |
|
|
|
|
|
|
|
|
|
921 |
with gr.Column():
|
922 |
bookmarks_display = gr.HTML(label="Bookmarks")
|
923 |
|
@@ -928,25 +932,22 @@ def build_app():
|
|
928 |
# Processing File
|
929 |
process_button.click(
|
930 |
fn=process_uploaded_file,
|
931 |
-
inputs=file_input,
|
932 |
-
outputs=[process_message, bookmarks_display,
|
933 |
)
|
934 |
|
935 |
# Deleting Bookmarks
|
936 |
delete_button.click(
|
937 |
fn=delete_selected_bookmarks,
|
938 |
-
inputs=
|
939 |
-
outputs=[process_message,
|
940 |
)
|
941 |
|
942 |
# Editing Categories
|
943 |
edit_button.click(
|
944 |
fn=edit_selected_bookmarks_category,
|
945 |
-
inputs=[
|
946 |
-
|
947 |
-
category_dropdown
|
948 |
-
],
|
949 |
-
outputs=[process_message, gr.Dropdown.update(), bookmarks_display]
|
950 |
)
|
951 |
|
952 |
# Exporting Bookmarks
|
|
|
632 |
else:
|
633 |
bookmark['category'] = 'Uncategorized'
|
634 |
|
635 |
+
def process_uploaded_file(file, delete_checkbox, edit_checkbox):
|
636 |
"""
|
637 |
Process the uploaded bookmarks file with enhanced error handling and user feedback.
|
638 |
"""
|
|
|
640 |
logger.info("Processing uploaded file")
|
641 |
|
642 |
if file is None:
|
643 |
+
return "⚠️ Please upload a bookmarks HTML file.", '', gr.Dropdown.update(choices=[]), gr.Dropdown.update(choices=[])
|
644 |
+
|
645 |
try:
|
646 |
file_content = file.decode('utf-8')
|
647 |
except UnicodeDecodeError as e:
|
648 |
logger.error(f"Error decoding file: {e}")
|
649 |
+
return "⚠️ Error decoding file. Please ensure it's a valid HTML file.", '', gr.Dropdown.update(choices=[]), gr.Dropdown.update(choices=[])
|
650 |
+
|
651 |
try:
|
652 |
bookmarks = parse_bookmarks(file_content)
|
653 |
except Exception as e:
|
654 |
logger.error(f"Error parsing bookmarks: {e}")
|
655 |
+
return "⚠️ Error parsing the bookmarks file.", '', gr.Dropdown.update(choices=[]), gr.Dropdown.update(choices=[])
|
656 |
+
|
657 |
if not bookmarks:
|
658 |
+
return "⚠️ No valid bookmarks found in the file.", '', gr.Dropdown.update(choices=[]), gr.Dropdown.update(choices=[])
|
659 |
+
|
660 |
try:
|
661 |
logger.info("Processing bookmarks...")
|
662 |
asyncio.run(process_bookmarks_async(bookmarks))
|
|
|
675 |
for i, bookmark in enumerate(bookmarks)]
|
676 |
|
677 |
bookmark_html = display_bookmarks()
|
678 |
+
return message, bookmark_html, gr.CheckboxGroup.update(choices=choices), gr.CheckboxGroup.update(choices=choices)
|
679 |
|
680 |
except Exception as e:
|
681 |
logger.error(f"Error processing bookmarks: {e}")
|
682 |
+
return "⚠️ Error processing bookmarks. Please try again.", '', gr.CheckboxGroup.update(choices=[]), gr.CheckboxGroup.update(choices=[])
|
683 |
|
684 |
+
def delete_selected_bookmarks(selected_indices, delete_checkbox, edit_checkbox):
|
685 |
"""
|
686 |
Delete selected bookmarks with enhanced error handling.
|
687 |
"""
|
688 |
global bookmarks, faiss_index
|
689 |
|
690 |
if not selected_indices:
|
691 |
+
return "⚠️ No bookmarks selected.", gr.CheckboxGroup.update(choices=[]), gr.CheckboxGroup.update(choices=[]), display_bookmarks()
|
692 |
|
693 |
try:
|
694 |
indices = [int(s.split('.')[0])-1 for s in selected_indices]
|
|
|
710 |
choices = [f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})"
|
711 |
for i, bookmark in enumerate(bookmarks)]
|
712 |
|
713 |
+
return message, gr.CheckboxGroup.update(choices=choices), gr.CheckboxGroup.update(choices=choices), display_bookmarks()
|
714 |
except Exception as e:
|
715 |
logger.error(f"Error deleting bookmarks: {e}")
|
716 |
+
return "⚠️ Error deleting bookmarks.", gr.CheckboxGroup.update(choices=[]), gr.CheckboxGroup.update(choices=[]), display_bookmarks()
|
717 |
|
718 |
+
def edit_selected_bookmarks_category(selected_indices, new_category, delete_checkbox, edit_checkbox):
|
719 |
"""
|
720 |
Edit category of selected bookmarks with enhanced error handling.
|
721 |
"""
|
722 |
if not selected_indices:
|
723 |
+
return "⚠️ No bookmarks selected.", gr.CheckboxGroup.update(choices=[]), gr.CheckboxGroup.update(choices=[]), display_bookmarks()
|
724 |
if not new_category:
|
725 |
+
return "⚠️ No new category selected.", gr.CheckboxGroup.update(choices=[]), gr.CheckboxGroup.update(choices=[]), display_bookmarks()
|
726 |
|
727 |
try:
|
728 |
indices = [int(s.split('.')[0])-1 for s in selected_indices]
|
|
|
739 |
choices = [f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})"
|
740 |
for i, bookmark in enumerate(bookmarks)]
|
741 |
|
742 |
+
return message, gr.CheckboxGroup.update(choices=choices), gr.CheckboxGroup.update(choices=choices), display_bookmarks()
|
743 |
except Exception as e:
|
744 |
logger.error(f"Error updating categories: {e}")
|
745 |
+
return "⚠️ Error updating categories.", gr.CheckboxGroup.update(choices=[]), gr.CheckboxGroup.update(choices=[]), display_bookmarks()
|
746 |
|
747 |
def export_bookmarks():
|
748 |
"""
|
|
|
794 |
# Add bookmark
|
795 |
dt = soup.new_tag('DT')
|
796 |
a = soup.new_tag('A', href=bookmark['url'])
|
797 |
+
if 'add_date' in bookmark and bookmark['add_date']:
|
798 |
a['ADD_DATE'] = bookmark['add_date']
|
799 |
+
if 'icon' in bookmark and bookmark['icon']:
|
800 |
a['ICON'] = bookmark['icon']
|
801 |
a.string = bookmark['title']
|
802 |
dt.append(a)
|
|
|
918 |
delete_button = gr.Button("Delete Selected Bookmarks")
|
919 |
export_button = gr.Button("Export Bookmarks")
|
920 |
|
921 |
+
# Define CheckboxGroups and assign to variables
|
922 |
+
delete_checkbox = gr.CheckboxGroup(label="Select Bookmarks to Delete", choices=[])
|
923 |
+
edit_checkbox = gr.CheckboxGroup(label="Select Bookmarks to Edit", choices=[])
|
924 |
+
|
925 |
with gr.Column():
|
926 |
bookmarks_display = gr.HTML(label="Bookmarks")
|
927 |
|
|
|
932 |
# Processing File
|
933 |
process_button.click(
|
934 |
fn=process_uploaded_file,
|
935 |
+
inputs=[file_input, delete_checkbox, edit_checkbox],
|
936 |
+
outputs=[process_message, bookmarks_display, delete_checkbox, edit_checkbox]
|
937 |
)
|
938 |
|
939 |
# Deleting Bookmarks
|
940 |
delete_button.click(
|
941 |
fn=delete_selected_bookmarks,
|
942 |
+
inputs=[delete_checkbox, edit_checkbox],
|
943 |
+
outputs=[process_message, delete_checkbox, edit_checkbox, bookmarks_display]
|
944 |
)
|
945 |
|
946 |
# Editing Categories
|
947 |
edit_button.click(
|
948 |
fn=edit_selected_bookmarks_category,
|
949 |
+
inputs=[edit_checkbox, category_dropdown, delete_checkbox, edit_checkbox],
|
950 |
+
outputs=[process_message, delete_checkbox, edit_checkbox, bookmarks_display]
|
|
|
|
|
|
|
951 |
)
|
952 |
|
953 |
# Exporting Bookmarks
|