clone3 commited on
Commit
13bf9bc
·
1 Parent(s): 9d26dce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -28
app.py CHANGED
@@ -4,53 +4,34 @@ import gradio as gr
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
 
 
4
  db = tinydb.TinyDB("books.json")
5
  table = db.table("books")
6
 
7
+ def Insert(url, users):
 
8
  book = {"url": url, "users": users}
 
9
  table.insert(book)
 
10
  return f"Added {url} by {users} to the database."
11
 
12
+ def Fetch(url):
 
13
  books = table.search(tinydb.where("url") == url)
 
14
  books_str = "\n".join([f"{book['url']} by {book['users']}" for book in books])
15
  return books_str
16
 
17
+ def Update(url, new_users):
 
18
  existing_users = table.search(tinydb.where("url") == url)[0].get("users")
19
+ table.update({"users": new_users}, tinydb.where("url") == url)
 
 
 
20
  return f"Updated users for the book '{url}' from '{existing_users}' to '{new_users}'."
21
 
22
+ def Delete(url):
 
23
  table.remove(tinydb.where("url") == url)
24
  return f"Deleted book with URL '{url}' from the database."
25
 
26
  def process_data(option, url, users):
27
  if option == "Insert":
28
+ return Insert(url, users)
29
  elif option == "Fetch":
30
+ return Fetch(url)
 
 
 
31
  elif option == "Update":
32
+ return Update(url, users)
 
 
 
33
  elif option == "Delete":
34
+ return Delete(url)
 
 
 
35
  else:
36
  return "Invalid option"
37