File size: 5,896 Bytes
a13d323
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
202
203
import gradio as gr
import json
import os

# File to store all lists data
DATA_FILE = "lists_data.json"

# Initialize with preset links
def initialize_data():
    preset_links = [
        {
            "name": "Learn Git Branching",
            "description": "Interactive Git tutorial",
            "link": "https://learngitbranching.js.org/?locale=en_US"
        },
        {
            "name": "Terminus",
            "description": "Command line tutorial game",
            "link": "https://web.mit.edu/mprat/Public/web/Terminus/Web/main.html"
        },
        {
            "name": "VisuAlgo",
            "description": "Algorithm visualization",
            "link": "https://visualgo.net/en"
        },
        {
            "name": "Linux Survival",
            "description": "Linux tutorial",
            "link": "https://linuxsurvival.com/"
        }
    ]
    
    return {
        "lists": {
            "default": {
                "name": "Default List",
                "items": preset_links
            }
        }, 
        "current_list": "default"
    }

# Load or initialize data
def load_data():
    if os.path.exists(DATA_FILE):
        with open(DATA_FILE, "r") as f:
            return json.load(f)
    data = initialize_data()
    save_data(data)
    return data

def save_data(data):
    with open(DATA_FILE, "w") as f:
        json.dump(data, f)

# Initialize data
data = load_data()

# Function to create a new list
def create_list(list_name):
    if not list_name.strip():
        return gr.update(), "Please enter a list name", data["current_list"], get_list_items()
    
    list_id = list_name.lower().replace(" ", "_")
    
    if list_id in data["lists"]:
        return gr.update(), f"List '{list_name}' already exists", data["current_list"], get_list_items()
    
    data["lists"][list_id] = {
        "name": list_name,
        "items": []
    }
    
    data["current_list"] = list_id
    save_data(data)

    return gr.update(choices=get_list_choices(), value=list_id), f"Created list: {list_name}", list_name, []

# Function to switch between lists
def switch_list(list_id):
    if list_id:
        data["current_list"] = list_id
        save_data(data)
        list_name = data["lists"][list_id]["name"]
        return list_name, get_list_items()
    return "", []

# Function to add an item to the current list
def add_item(name, description, link):
    if not data["current_list"]:
        return "Please select or create a list first", get_list_items()
    
    if not name.strip():
        return "Please enter a name for the item", get_list_items()
    
    current_list = data["current_list"]
    
    new_item = {
        "name": name,
        "description": description,
        "link": link
    }
    
    data["lists"][current_list]["items"].append(new_item)
    save_data(data)
    
    return f"Added item: {name}", get_list_items()

# Function to get current list items for display
def get_list_items():
    if not data["current_list"] or data["current_list"] not in data["lists"]:
        return []
    
    return [[item["name"], item["description"], item["link"]] 
            for item in data["lists"][data["current_list"]]["items"]]

# Function to remove an item
def remove_item(evt: gr.SelectData):
    if not data["current_list"]:
        return "No list selected", get_list_items()
    
    row_index = evt.index[0]
    current_list = data["current_list"]
    
    if 0 <= row_index < len(data["lists"][current_list]["items"]):
        removed_item = data["lists"][current_list]["items"].pop(row_index)
        save_data(data)
        return f"Removed: {removed_item['name']}", get_list_items()
    
    return "Error removing item", get_list_items()

# Get list choices for dropdown
def get_list_choices():
    return [(list_id, data["lists"][list_id]["name"]) for list_id in data["lists"]]

# Build the Gradio interface
with gr.Blocks(title="Simple List Manager") as app:
    gr.Markdown("# Simple List Manager")
    
    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown("### Create New List")
            new_list_name = gr.Textbox(label="New List Name")
            create_btn = gr.Button("Create List")
            
            gr.Markdown("### Switch Lists")
            list_dropdown = gr.Dropdown(
                choices=get_list_choices(),
                label="Select List",
                value=data["current_list"] if data["current_list"] else None,
                interactive=True
            )
        
        with gr.Column(scale=2):
            current_list_name = gr.Markdown(
                f"### Current List: {data['lists'][data['current_list']]['name'] if data['current_list'] in data['lists'] else 'None'}"
            )
            status_message = gr.Markdown("")
    
    gr.Markdown("### Add New Item")
    with gr.Row():
        item_name = gr.Textbox(label="Name")
        item_description = gr.Textbox(label="Description")
        item_link = gr.Textbox(label="Link")
    
    add_btn = gr.Button("Add Item")
    
    gr.Markdown("### List Items (click row to delete)")
    items_table = gr.Dataframe(
        headers=["Name", "Description", "Link"],
        datatype=["str", "str", "str"],
        value=get_list_items(),
        interactive=False
    )
    
    # Connect events
    create_btn.click(
        create_list,
        inputs=[new_list_name],
        outputs=[list_dropdown, status_message, current_list_name, items_table]
    )
    
    list_dropdown.change(
        switch_list,
        inputs=[list_dropdown],
        outputs=[current_list_name, items_table]
    )
    
    add_btn.click(
        add_item,
        inputs=[item_name, item_description, item_link],
        outputs=[status_message, items_table]
    )
    
    items_table.select(
        remove_item,
        None,
        [status_message, items_table]
    )

# Launch the app
app.launch()