Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,14 +2,13 @@ import gradio as gr
|
|
2 |
import os
|
3 |
import json
|
4 |
import glob
|
|
|
5 |
|
6 |
# Directory to store JSON list files
|
7 |
LISTS_DIR = "lists"
|
8 |
-
|
9 |
# Ensure the lists directory exists
|
10 |
os.makedirs(LISTS_DIR, exist_ok=True)
|
11 |
|
12 |
-
|
13 |
# Get list of available JSON files
|
14 |
def get_list_files():
|
15 |
files = glob.glob(os.path.join(LISTS_DIR, "*.json"))
|
@@ -23,7 +22,25 @@ def load_list_data(list_name):
|
|
23 |
return json.load(f)
|
24 |
return {"name": "Empty List", "items": []}
|
25 |
|
26 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
def get_list_items(list_name):
|
28 |
data = load_list_data(list_name)
|
29 |
return [[item["name"], item["description"], item["link"]]
|
@@ -36,17 +53,13 @@ def get_list_title(list_name):
|
|
36 |
|
37 |
# Function to switch between lists
|
38 |
def switch_list(list_name):
|
39 |
-
return get_list_title(list_name),
|
40 |
-
|
41 |
-
gr.Markdown("""
|
42 |
-
## Useful Things Archive
|
43 |
-
|
44 |
-
""")
|
45 |
|
46 |
# Build the Gradio interface
|
47 |
-
with gr.Blocks(title="
|
48 |
-
gr.Markdown("
|
49 |
-
|
|
|
50 |
# Get available lists
|
51 |
list_names = get_list_files()
|
52 |
if not list_names:
|
@@ -65,43 +78,16 @@ with gr.Blocks(title="Static List Manager") as app:
|
|
65 |
|
66 |
list_title = gr.Markdown(f"## {get_list_title(list_names[0])}")
|
67 |
|
68 |
-
#
|
69 |
-
|
70 |
-
headers=["Name", "Description", "Link"],
|
71 |
-
datatype=["str", "str", "str"],
|
72 |
-
value=get_list_items(list_names[0]),
|
73 |
-
interactive=False
|
74 |
-
)
|
75 |
|
76 |
# Connect tab events to switch lists
|
77 |
for i, (tab, list_name) in enumerate(tab_list):
|
78 |
tab.select(
|
79 |
lambda name=list_name: switch_list(name),
|
80 |
inputs=[],
|
81 |
-
outputs=[list_title,
|
82 |
)
|
83 |
|
84 |
-
# Add instructions
|
85 |
-
gr.Markdown("""
|
86 |
-
### How to Add More Lists
|
87 |
-
|
88 |
-
1. Create a JSON file in the 'lists' folder with the format: listname.json
|
89 |
-
2. Use the following structure in your JSON file:
|
90 |
-
```json
|
91 |
-
{
|
92 |
-
"name": "Your List Title",
|
93 |
-
"items": [
|
94 |
-
{
|
95 |
-
"name": "Item Name",
|
96 |
-
"description": "Item Description",
|
97 |
-
"link": "https://example.com"
|
98 |
-
},
|
99 |
-
...
|
100 |
-
]
|
101 |
-
}
|
102 |
-
```
|
103 |
-
3. Restart the app to see your new list
|
104 |
-
""")
|
105 |
-
|
106 |
# Launch the app
|
107 |
app.launch()
|
|
|
2 |
import os
|
3 |
import json
|
4 |
import glob
|
5 |
+
import pandas as pd
|
6 |
|
7 |
# Directory to store JSON list files
|
8 |
LISTS_DIR = "lists"
|
|
|
9 |
# Ensure the lists directory exists
|
10 |
os.makedirs(LISTS_DIR, exist_ok=True)
|
11 |
|
|
|
12 |
# Get list of available JSON files
|
13 |
def get_list_files():
|
14 |
files = glob.glob(os.path.join(LISTS_DIR, "*.json"))
|
|
|
22 |
return json.load(f)
|
23 |
return {"name": "Empty List", "items": []}
|
24 |
|
25 |
+
# Create HTML table with clickable links
|
26 |
+
def get_list_items_html(list_name):
|
27 |
+
data = load_list_data(list_name)
|
28 |
+
html = "<table style='width:100%; border-collapse: collapse;'>"
|
29 |
+
html += "<tr><th style='text-align:left; padding:8px; border-bottom:1px solid #ddd;'>Name</th>"
|
30 |
+
html += "<th style='text-align:left; padding:8px; border-bottom:1px solid #ddd;'>Description</th>"
|
31 |
+
html += "<th style='text-align:left; padding:8px; border-bottom:1px solid #ddd;'>Link</th></tr>"
|
32 |
+
|
33 |
+
for item in data["items"]:
|
34 |
+
html += "<tr>"
|
35 |
+
html += f"<td style='padding:8px; border-bottom:1px solid #ddd;'>{item['name']}</td>"
|
36 |
+
html += f"<td style='padding:8px; border-bottom:1px solid #ddd;'>{item['description']}</td>"
|
37 |
+
html += f"<td style='padding:8px; border-bottom:1px solid #ddd;'><a href='{item['link']}' target='_blank'>{item['link']}</a></td>"
|
38 |
+
html += "</tr>"
|
39 |
+
|
40 |
+
html += "</table>"
|
41 |
+
return html
|
42 |
+
|
43 |
+
# Get list items in table format (for backup/alternative view)
|
44 |
def get_list_items(list_name):
|
45 |
data = load_list_data(list_name)
|
46 |
return [[item["name"], item["description"], item["link"]]
|
|
|
53 |
|
54 |
# Function to switch between lists
|
55 |
def switch_list(list_name):
|
56 |
+
return get_list_title(list_name), get_list_items_html(list_name)
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
# Build the Gradio interface
|
59 |
+
with gr.Blocks(title="Useful Things Archive") as app:
|
60 |
+
gr.Markdown("""
|
61 |
+
## Useful Things Archive
|
62 |
+
""")
|
63 |
# Get available lists
|
64 |
list_names = get_list_files()
|
65 |
if not list_names:
|
|
|
78 |
|
79 |
list_title = gr.Markdown(f"## {get_list_title(list_names[0])}")
|
80 |
|
81 |
+
# Use HTML component instead of Dataframe to enable clickable links
|
82 |
+
items_html = gr.HTML(value=get_list_items_html(list_names[0]))
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
# Connect tab events to switch lists
|
85 |
for i, (tab, list_name) in enumerate(tab_list):
|
86 |
tab.select(
|
87 |
lambda name=list_name: switch_list(name),
|
88 |
inputs=[],
|
89 |
+
outputs=[list_title, items_html]
|
90 |
)
|
91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
# Launch the app
|
93 |
app.launch()
|