Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,53 +4,53 @@ import gradio as gr
|
|
4 |
db = tinydb.TinyDB("books.json")
|
5 |
table = db.table("books")
|
6 |
|
7 |
-
def add_book(
|
8 |
# Create a dictionary to store the book information
|
9 |
-
book = {"
|
10 |
# Insert the book into the table
|
11 |
table.insert(book)
|
12 |
# Return a confirmation message
|
13 |
-
return f"Added {
|
14 |
|
15 |
-
def
|
16 |
-
# Query the database to get books with the specified
|
17 |
-
books = table.search(tinydb.where("
|
18 |
# Format the books as a string for display
|
19 |
-
books_str = "\n".join([f"{book['
|
20 |
return books_str
|
21 |
|
22 |
-
def
|
23 |
-
# Fetch the existing
|
24 |
-
|
25 |
|
26 |
-
# Update the
|
27 |
-
table.update({"
|
28 |
|
29 |
-
return f"Updated
|
30 |
|
31 |
-
def
|
32 |
-
# Remove the book with the specified
|
33 |
-
table.remove(tinydb.where("
|
34 |
-
return f"Deleted book with
|
35 |
|
36 |
-
def process_data(option,
|
37 |
if option == "Insert":
|
38 |
-
return add_book(
|
39 |
elif option == "Fetch":
|
40 |
-
if
|
41 |
-
return
|
42 |
else:
|
43 |
return get_all_books()
|
44 |
elif option == "Update":
|
45 |
-
if
|
46 |
-
return
|
47 |
else:
|
48 |
-
return "
|
49 |
elif option == "Delete":
|
50 |
-
if
|
51 |
-
return
|
52 |
else:
|
53 |
-
return "
|
54 |
else:
|
55 |
return "Invalid option"
|
56 |
|
@@ -58,8 +58,8 @@ demo = gr.Interface(
|
|
58 |
fn=process_data,
|
59 |
inputs=[
|
60 |
gr.Radio(["Insert", "Fetch", "Update", "Delete"], label="Select Operation"),
|
61 |
-
gr.Textbox(label="
|
62 |
-
gr.Textbox(label="
|
63 |
],
|
64 |
outputs=gr.Textbox(label="Result")
|
65 |
)
|
|
|
4 |
db = tinydb.TinyDB("books.json")
|
5 |
table = db.table("books")
|
6 |
|
7 |
+
def add_book(url, users):
|
8 |
# Create a dictionary to store the book information
|
9 |
+
book = {"url": url, "users": users}
|
10 |
# Insert the book into the table
|
11 |
table.insert(book)
|
12 |
# Return a confirmation message
|
13 |
+
return f"Added {url} by {users} to the database."
|
14 |
|
15 |
+
def get_books_by_url(url):
|
16 |
+
# Query the database to get books with the specified URL
|
17 |
+
books = table.search(tinydb.where("url") == url)
|
18 |
# Format the books as a string for display
|
19 |
+
books_str = "\n".join([f"{book['url']} by {book['users']}" for book in books])
|
20 |
return books_str
|
21 |
|
22 |
+
def update_users_by_url(url, new_users):
|
23 |
+
# Fetch the existing users for the book
|
24 |
+
existing_users = table.search(tinydb.where("url") == url)[0].get("users")
|
25 |
|
26 |
+
# Update the users of the book with the specified URL
|
27 |
+
table.update({"users": new_users}, tinydb.where("url") == url)
|
28 |
|
29 |
+
return f"Updated users for the book '{url}' from '{existing_users}' to '{new_users}'."
|
30 |
|
31 |
+
def delete_book_by_url(url):
|
32 |
+
# Remove the book with the specified URL from the table
|
33 |
+
table.remove(tinydb.where("url") == url)
|
34 |
+
return f"Deleted book with URL '{url}' from the database."
|
35 |
|
36 |
+
def process_data(option, url, users):
|
37 |
if option == "Insert":
|
38 |
+
return add_book(url, users)
|
39 |
elif option == "Fetch":
|
40 |
+
if url:
|
41 |
+
return get_books_by_url(url)
|
42 |
else:
|
43 |
return get_all_books()
|
44 |
elif option == "Update":
|
45 |
+
if url and users:
|
46 |
+
return update_users_by_url(url, users)
|
47 |
else:
|
48 |
+
return "URL and users are required for update."
|
49 |
elif option == "Delete":
|
50 |
+
if url:
|
51 |
+
return delete_book_by_url(url)
|
52 |
else:
|
53 |
+
return "URL is required for delete."
|
54 |
else:
|
55 |
return "Invalid option"
|
56 |
|
|
|
58 |
fn=process_data,
|
59 |
inputs=[
|
60 |
gr.Radio(["Insert", "Fetch", "Update", "Delete"], label="Select Operation"),
|
61 |
+
gr.Textbox(label="URL"),
|
62 |
+
gr.Textbox(label="Users"),
|
63 |
],
|
64 |
outputs=gr.Textbox(label="Result")
|
65 |
)
|