Spaces:
Sleeping
Sleeping
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() | |