Spaces:
Runtime error
Runtime error
File size: 1,397 Bytes
0ba984d 607d357 0ba984d 607d357 13bf9bc 9d26dce 0ba984d 9d26dce 0ba984d 07622e3 9d26dce 0ba984d 13bf9bc 9d26dce 13bf9bc 9d26dce 0ba984d 13bf9bc 9d26dce 526df0c 9d26dce 0ba984d 13bf9bc 0ba984d 07622e3 0ba984d 13bf9bc 526df0c 13bf9bc 0ba984d 526df0c 9d26dce 0ba984d 1de9bec 0ba984d |
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 |
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() |