Spaces:
Runtime error
Runtime error
import tinydb | |
import gradio as gr | |
db = tinydb.TinyDB("books.json") | |
table = db.table("books") | |
def Insert(url, users): | |
book = {"url": url, "users": users} | |
table.insert(book) | |
return f"Added {url} by {users} to the database." | |
def Fetch(users): | |
books = table.search(tinydb.where("users") == users) | |
books_str = "\n".join([f"{book['url']} by {book['users']}" for book in books]) | |
return books_str | |
def Update(url, new_users): | |
existing_users = table.search(tinydb.where("url") == url)[0].get("users") | |
table.update({"users": new_users}, tinydb.where("url") == url) | |
return f"Updated users for the book '{url}' from '{existing_users}' to '{new_users}'." | |
def Delete(url): | |
table.remove(tinydb.where("url") == url) | |
return f"Deleted book with URL '{url}' from the database." | |
def process_data(option, url, users): | |
if option == "Insert": | |
return Insert(url, users) | |
elif option == "Fetch": | |
return Fetch(users) | |
elif option == "Update": | |
return Update(url, users) | |
elif option == "Delete": | |
return Delete(url) | |
else: | |
return "Invalid option" | |
demo = gr.Interface( | |
fn=process_data, | |
inputs=[ | |
gr.Radio(["Insert", "Fetch", "Update", "Delete"], label="Select Operation"), | |
gr.Textbox(label="URL"), | |
gr.Textbox(label="Users"), | |
], | |
outputs=gr.Textbox(label="Result") | |
) | |
demo.launch() |