-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary_manager_cli.py
More file actions
116 lines (103 loc) · 4.62 KB
/
library_manager_cli.py
File metadata and controls
116 lines (103 loc) · 4.62 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import streamlit as st
import os
# Function to load library from a file
def load_library():
library = set()
if os.path.exists('library.txt'):
with open('library.txt', 'r') as f:
for line in f:
title, author, publication_year, genre, read_status = line.strip().split(',')
library.add((
title, author, int(publication_year), genre, read_status.lower() == 'true'
))
return library
# Function to save library to a file
def save_library(library):
with open('library.txt', 'w') as f:
for book in library:
f.write(f"{book[0]},{book[1]},{book[2]},{book[3]},{book[4]}\n")
st.success("Library saved to file.")
# Main function for the Streamlit app
def main():
st.title("Personal Library Manager")
library = load_library()
menu = ["Add a Book", "Remove a Book", "Search for a Book", "Display All Books", "Display Statistics", "Exit"]
choice = st.sidebar.selectbox("Menu", menu)
if choice == "Add a Book":
st.subheader("Add a Book")
title = st.text_input("Enter the book title")
author = st.text_input("Enter the author")
publication_year = st.number_input("Enter the publication year", min_value=0, step=1)
genre = st.text_input("Enter the genre")
read_status = st.selectbox("Have you read this book?", ["Yes", "No"]) == "Yes"
if st.button("Add Book"):
book = (title, author, publication_year, genre, read_status)
if book in library:
st.warning("This book is already in your library!")
else:
library.add(book) # Add book to set
st.success("Book added successfully!")
save_library(library) # Save library
elif choice == "Remove a Book":
st.subheader("Remove a Book")
title = st.text_input("Enter the title of the book to remove")
if st.button("Remove Book"):
book_found = None
for book in library:
if book[0].lower() == title.lower():
book_found = book
break
if book_found:
library.remove(book_found)
st.success("Book removed successfully!")
save_library(library)
else:
st.error("Book not found.")
elif choice == "Search for a Book":
st.subheader("Search for a Book")
search_choice = st.selectbox("Search by", ["Title", "Author"])
if search_choice == "Title":
title = st.text_input("Enter the title")
if st.button("Search"):
matching_books = [book for book in library if book[0].lower() == title.lower()]
if matching_books:
st.write("Matching Books:")
for book in matching_books:
status = "Read" if book[4] else "Unread"
st.write(f"{book[0]} by {book[1]} ({book[2]}) - {book[3]} - {status}")
else:
st.error("No matching books found.")
elif search_choice == "Author":
author = st.text_input("Enter the author")
if st.button("Search"):
matching_books = [book for book in library if book[1].lower() == author.lower()]
if matching_books:
st.write("Matching Books:")
for book in matching_books:
status = "Read" if book[4] else "Unread"
st.write(f"{book[0]} by {book[1]} ({book[2]}) - {book[3]} - {status}")
else:
st.error("No matching books found.")
elif choice == "Display All Books":
st.subheader("Your Library")
if library:
for i, book in enumerate(sorted(library), start=1): # Sorted for consistent order
status = "Read" if book[4] else "Unread"
st.write(f"{i}. {book[0]} by {book[1]} ({book[2]}) - {book[3]} - {status}")
else:
st.write("Your library is empty.")
elif choice == "Display Statistics":
st.subheader("Statistics")
total_books = len(library)
if total_books == 0:
st.write("No books in the library.")
else:
read_books = sum(1 for book in library if book[4])
percentage_read = (read_books / total_books) * 100
st.write(f"Total books: {total_books}")
st.write(f"Percentage read: {percentage_read:.2f}%")
elif choice == "Exit":
save_library(library)
st.write("Goodbye!")
if __name__ == "__main__":
main()