Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,202 +1,102 @@
|
|
1 |
import gradio as gr
|
2 |
-
import json
|
3 |
import os
|
|
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
|
8 |
-
# Initialize with preset links
|
9 |
-
def initialize_data():
|
10 |
-
preset_links = [
|
11 |
-
{
|
12 |
-
"name": "Learn Git Branching",
|
13 |
-
"description": "Interactive Git tutorial",
|
14 |
-
"link": "https://learngitbranching.js.org/?locale=en_US"
|
15 |
-
},
|
16 |
-
{
|
17 |
-
"name": "Terminus",
|
18 |
-
"description": "Command line tutorial game",
|
19 |
-
"link": "https://web.mit.edu/mprat/Public/web/Terminus/Web/main.html"
|
20 |
-
},
|
21 |
-
{
|
22 |
-
"name": "VisuAlgo",
|
23 |
-
"description": "Algorithm visualization",
|
24 |
-
"link": "https://visualgo.net/en"
|
25 |
-
},
|
26 |
-
{
|
27 |
-
"name": "Linux Survival",
|
28 |
-
"description": "Linux tutorial",
|
29 |
-
"link": "https://linuxsurvival.com/"
|
30 |
-
}
|
31 |
-
]
|
32 |
-
|
33 |
-
return {
|
34 |
-
"lists": {
|
35 |
-
"default": {
|
36 |
-
"name": "Default List",
|
37 |
-
"items": preset_links
|
38 |
-
}
|
39 |
-
},
|
40 |
-
"current_list": "default"
|
41 |
-
}
|
42 |
|
43 |
-
#
|
44 |
-
|
45 |
-
if os.path.exists(DATA_FILE):
|
46 |
-
with open(DATA_FILE, "r") as f:
|
47 |
-
return json.load(f)
|
48 |
-
data = initialize_data()
|
49 |
-
save_data(data)
|
50 |
-
return data
|
51 |
|
52 |
-
def save_data(data):
|
53 |
-
with open(DATA_FILE, "w") as f:
|
54 |
-
json.dump(data, f)
|
55 |
|
56 |
-
#
|
57 |
-
|
|
|
|
|
58 |
|
59 |
-
#
|
60 |
-
def
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
if list_id in data["lists"]:
|
67 |
-
return gr.update(), f"List '{list_name}' already exists", data["current_list"], get_list_items()
|
68 |
-
|
69 |
-
data["lists"][list_id] = {
|
70 |
-
"name": list_name,
|
71 |
-
"items": []
|
72 |
-
}
|
73 |
-
|
74 |
-
data["current_list"] = list_id
|
75 |
-
save_data(data)
|
76 |
-
|
77 |
-
return gr.update(choices=get_list_choices(), value=list_id), f"Created list: {list_name}", list_name, []
|
78 |
-
|
79 |
-
# Function to switch between lists
|
80 |
-
def switch_list(list_id):
|
81 |
-
if list_id:
|
82 |
-
data["current_list"] = list_id
|
83 |
-
save_data(data)
|
84 |
-
list_name = data["lists"][list_id]["name"]
|
85 |
-
return list_name, get_list_items()
|
86 |
-
return "", []
|
87 |
-
|
88 |
-
# Function to add an item to the current list
|
89 |
-
def add_item(name, description, link):
|
90 |
-
if not data["current_list"]:
|
91 |
-
return "Please select or create a list first", get_list_items()
|
92 |
-
|
93 |
-
if not name.strip():
|
94 |
-
return "Please enter a name for the item", get_list_items()
|
95 |
-
|
96 |
-
current_list = data["current_list"]
|
97 |
-
|
98 |
-
new_item = {
|
99 |
-
"name": name,
|
100 |
-
"description": description,
|
101 |
-
"link": link
|
102 |
-
}
|
103 |
-
|
104 |
-
data["lists"][current_list]["items"].append(new_item)
|
105 |
-
save_data(data)
|
106 |
-
|
107 |
-
return f"Added item: {name}", get_list_items()
|
108 |
|
109 |
-
#
|
110 |
-
def get_list_items():
|
111 |
-
|
112 |
-
return []
|
113 |
-
|
114 |
return [[item["name"], item["description"], item["link"]]
|
115 |
-
for item in data["
|
116 |
|
117 |
-
#
|
118 |
-
def
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
row_index = evt.index[0]
|
123 |
-
current_list = data["current_list"]
|
124 |
-
|
125 |
-
if 0 <= row_index < len(data["lists"][current_list]["items"]):
|
126 |
-
removed_item = data["lists"][current_list]["items"].pop(row_index)
|
127 |
-
save_data(data)
|
128 |
-
return f"Removed: {removed_item['name']}", get_list_items()
|
129 |
-
|
130 |
-
return "Error removing item", get_list_items()
|
131 |
|
132 |
-
#
|
133 |
-
def
|
134 |
-
return
|
135 |
|
136 |
# Build the Gradio interface
|
137 |
-
with gr.Blocks(title="
|
138 |
-
gr.Markdown("#
|
139 |
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
list_dropdown = gr.Dropdown(
|
148 |
-
choices=get_list_choices(),
|
149 |
-
label="Select List",
|
150 |
-
value=data["current_list"] if data["current_list"] else None,
|
151 |
-
interactive=True
|
152 |
-
)
|
153 |
-
|
154 |
-
with gr.Column(scale=2):
|
155 |
-
current_list_name = gr.Markdown(
|
156 |
-
f"### Current List: {data['lists'][data['current_list']]['name'] if data['current_list'] in data['lists'] else 'None'}"
|
157 |
-
)
|
158 |
-
status_message = gr.Markdown("")
|
159 |
|
160 |
-
gr.Markdown("### Add New Item")
|
161 |
with gr.Row():
|
162 |
-
|
163 |
-
|
164 |
-
|
|
|
|
|
|
|
165 |
|
166 |
-
|
167 |
|
168 |
-
|
169 |
items_table = gr.Dataframe(
|
170 |
headers=["Name", "Description", "Link"],
|
171 |
datatype=["str", "str", "str"],
|
172 |
-
value=get_list_items(),
|
173 |
interactive=False
|
174 |
)
|
175 |
|
176 |
-
# Connect events
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
|
|
|
|
|
|
|
|
|
|
200 |
|
201 |
# Launch the app
|
202 |
-
app.launch()
|
|
|
1 |
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"))
|
16 |
+
return [os.path.basename(f)[:-5] for f in files] # Remove .json extension
|
17 |
|
18 |
+
# Load data from a JSON file
|
19 |
+
def load_list_data(list_name):
|
20 |
+
file_path = os.path.join(LISTS_DIR, f"{list_name}.json")
|
21 |
+
if os.path.exists(file_path):
|
22 |
+
with open(file_path, "r") as f:
|
23 |
+
return json.load(f)
|
24 |
+
return {"name": "Empty List", "items": []}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
+
# Get list items in table format
|
27 |
+
def get_list_items(list_name):
|
28 |
+
data = load_list_data(list_name)
|
|
|
|
|
29 |
return [[item["name"], item["description"], item["link"]]
|
30 |
+
for item in data["items"]]
|
31 |
|
32 |
+
# Get list title
|
33 |
+
def get_list_title(list_name):
|
34 |
+
data = load_list_data(list_name)
|
35 |
+
return data["name"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
+
# Function to switch between lists
|
38 |
+
def switch_list(list_name):
|
39 |
+
return get_list_title(list_name), get_list_items(list_name)
|
40 |
|
41 |
# Build the Gradio interface
|
42 |
+
with gr.Blocks(title="Static List Manager") as app:
|
43 |
+
gr.Markdown("# Static List Manager")
|
44 |
|
45 |
+
# Get available lists
|
46 |
+
list_names = get_list_files()
|
47 |
+
if not list_names:
|
48 |
+
list_names = ["learn"] # Fallback to default
|
49 |
+
|
50 |
+
# Current list state
|
51 |
+
current_list = gr.State(value=list_names[0])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
|
|
53 |
with gr.Row():
|
54 |
+
# Create tabs for each list
|
55 |
+
with gr.Tabs() as tabs:
|
56 |
+
tab_list = []
|
57 |
+
for list_name in list_names:
|
58 |
+
tab = gr.Tab(label=get_list_title(list_name))
|
59 |
+
tab_list.append((tab, list_name))
|
60 |
|
61 |
+
list_title = gr.Markdown(f"## {get_list_title(list_names[0])}")
|
62 |
|
63 |
+
# Display table of list items
|
64 |
items_table = gr.Dataframe(
|
65 |
headers=["Name", "Description", "Link"],
|
66 |
datatype=["str", "str", "str"],
|
67 |
+
value=get_list_items(list_names[0]),
|
68 |
interactive=False
|
69 |
)
|
70 |
|
71 |
+
# Connect tab events to switch lists
|
72 |
+
for i, (tab, list_name) in enumerate(tab_list):
|
73 |
+
tab.select(
|
74 |
+
lambda name=list_name: switch_list(name),
|
75 |
+
inputs=[],
|
76 |
+
outputs=[list_title, items_table]
|
77 |
+
)
|
78 |
+
|
79 |
+
# Add instructions
|
80 |
+
gr.Markdown("""
|
81 |
+
### How to Add More Lists
|
82 |
+
|
83 |
+
1. Create a JSON file in the 'lists' folder with the format: listname.json
|
84 |
+
2. Use the following structure in your JSON file:
|
85 |
+
```json
|
86 |
+
{
|
87 |
+
"name": "Your List Title",
|
88 |
+
"items": [
|
89 |
+
{
|
90 |
+
"name": "Item Name",
|
91 |
+
"description": "Item Description",
|
92 |
+
"link": "https://example.com"
|
93 |
+
},
|
94 |
+
...
|
95 |
+
]
|
96 |
+
}
|
97 |
+
```
|
98 |
+
3. Restart the app to see your new list
|
99 |
+
""")
|
100 |
|
101 |
# Launch the app
|
102 |
+
app.launch()
|