Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,202 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
|
5 |
+
# File to store all lists data
|
6 |
+
DATA_FILE = "lists_data.json"
|
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 |
+
# Load or initialize data
|
44 |
+
def load_data():
|
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 |
+
# Initialize data
|
57 |
+
data = load_data()
|
58 |
+
|
59 |
+
# Function to create a new list
|
60 |
+
def create_list(list_name):
|
61 |
+
if not list_name.strip():
|
62 |
+
return gr.update(), "Please enter a list name", data["current_list"], get_list_items()
|
63 |
+
|
64 |
+
list_id = list_name.lower().replace(" ", "_")
|
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 |
+
# Function to get current list items for display
|
110 |
+
def get_list_items():
|
111 |
+
if not data["current_list"] or data["current_list"] not in data["lists"]:
|
112 |
+
return []
|
113 |
+
|
114 |
+
return [[item["name"], item["description"], item["link"]]
|
115 |
+
for item in data["lists"][data["current_list"]]["items"]]
|
116 |
+
|
117 |
+
# Function to remove an item
|
118 |
+
def remove_item(evt: gr.SelectData):
|
119 |
+
if not data["current_list"]:
|
120 |
+
return "No list selected", get_list_items()
|
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 |
+
# Get list choices for dropdown
|
133 |
+
def get_list_choices():
|
134 |
+
return [(list_id, data["lists"][list_id]["name"]) for list_id in data["lists"]]
|
135 |
+
|
136 |
+
# Build the Gradio interface
|
137 |
+
with gr.Blocks(title="Simple List Manager") as app:
|
138 |
+
gr.Markdown("# Simple List Manager")
|
139 |
+
|
140 |
+
with gr.Row():
|
141 |
+
with gr.Column(scale=1):
|
142 |
+
gr.Markdown("### Create New List")
|
143 |
+
new_list_name = gr.Textbox(label="New List Name")
|
144 |
+
create_btn = gr.Button("Create List")
|
145 |
+
|
146 |
+
gr.Markdown("### Switch Lists")
|
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 |
+
item_name = gr.Textbox(label="Name")
|
163 |
+
item_description = gr.Textbox(label="Description")
|
164 |
+
item_link = gr.Textbox(label="Link")
|
165 |
+
|
166 |
+
add_btn = gr.Button("Add Item")
|
167 |
+
|
168 |
+
gr.Markdown("### List Items (click row to delete)")
|
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 |
+
create_btn.click(
|
178 |
+
create_list,
|
179 |
+
inputs=[new_list_name],
|
180 |
+
outputs=[list_dropdown, status_message, current_list_name, items_table]
|
181 |
+
)
|
182 |
+
|
183 |
+
list_dropdown.change(
|
184 |
+
switch_list,
|
185 |
+
inputs=[list_dropdown],
|
186 |
+
outputs=[current_list_name, items_table]
|
187 |
+
)
|
188 |
+
|
189 |
+
add_btn.click(
|
190 |
+
add_item,
|
191 |
+
inputs=[item_name, item_description, item_link],
|
192 |
+
outputs=[status_message, items_table]
|
193 |
+
)
|
194 |
+
|
195 |
+
items_table.select(
|
196 |
+
remove_item,
|
197 |
+
None,
|
198 |
+
[status_message, items_table]
|
199 |
+
)
|
200 |
+
|
201 |
+
# Launch the app
|
202 |
+
app.launch()
|