clone3 commited on
Commit
9d26dce
·
1 Parent(s): 526df0c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -29
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(title, author):
8
  # Create a dictionary to store the book information
9
- book = {"title": title, "author": author}
10
  # Insert the book into the table
11
  table.insert(book)
12
  # Return a confirmation message
13
- return f"Added {title} by {author} to the database."
14
 
15
- def get_books_by_title(title):
16
- # Query the database to get books with the specified title
17
- books = table.search(tinydb.where("title") == title)
18
  # Format the books as a string for display
19
- books_str = "\n".join([f"{book['title']} by {book['author']}" for book in books])
20
  return books_str
21
 
22
- def update_author_by_title(title, new_author):
23
- # Fetch the existing author for the book
24
- existing_author = table.search(tinydb.where("title") == title)[0].get("author")
25
 
26
- # Update the author of the book with the specified title
27
- table.update({"author": new_author}, tinydb.where("title") == title)
28
 
29
- return f"Updated author for the book '{title}' from '{existing_author}' to '{new_author}'."
30
 
31
- def delete_book_by_title(title):
32
- # Remove the book with the specified title from the table
33
- table.remove(tinydb.where("title") == title)
34
- return f"Deleted book with title '{title}' from the database."
35
 
36
- def process_data(option, title, author):
37
  if option == "Insert":
38
- return add_book(title, author)
39
  elif option == "Fetch":
40
- if title:
41
- return get_books_by_title(title)
42
  else:
43
  return get_all_books()
44
  elif option == "Update":
45
- if title and author:
46
- return update_author_by_title(title, author)
47
  else:
48
- return "Title and author are required for update."
49
  elif option == "Delete":
50
- if title:
51
- return delete_book_by_title(title)
52
  else:
53
- return "Title is required for delete."
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="Title"),
62
- gr.Textbox(label="Author"),
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
  )