SyedAliHusnainGillani commited on
Commit
d5c8d66
·
verified ·
1 Parent(s): 8adcb3f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -23
app.py CHANGED
@@ -1,10 +1,24 @@
1
  import streamlit as st
2
  import random
3
- from quotegen import QuoteFetcher
4
 
5
- # Fetch a large dataset of quotes
6
- fetcher = QuoteFetcher()
7
- categories = fetcher.categories()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  # App Title
10
  st.title("Random Quote Generator")
@@ -13,43 +27,44 @@ st.title("Random Quote Generator")
13
  st.subheader("Choose a category to view quotes:")
14
 
15
  # Display category buttons
 
16
  selected_genre = None
17
 
18
- cols = st.columns(len(categories))
19
- for idx, cat in enumerate(categories):
20
- if cols[idx].button(cat.title()):
21
- selected_genre = cat
 
 
 
22
 
23
  # Display quotes only if a category is selected
24
  if selected_genre:
25
- # Initialize session state for shown quotes
26
  if "shown_quotes" not in st.session_state:
27
- st.session_state["shown_quotes"] = {cat: [] for cat in categories}
28
  if "current_quote" not in st.session_state or st.session_state["selected_genre"] != selected_genre:
29
  st.session_state["current_quote"] = None
30
  st.session_state["selected_genre"] = selected_genre
31
 
32
- # Fetch quotes for the selected genre
33
- all_quotes = fetcher.fetch_by_category(selected_genre)
34
  available_quotes = [
35
- quote for quote in all_quotes
36
- if quote["text"] not in st.session_state["shown_quotes"][selected_genre]
37
  ]
38
 
39
- # Check for available quotes
40
  if not available_quotes:
41
  st.warning("No more quotes left in this category! Resetting...")
42
  st.session_state["shown_quotes"][selected_genre] = []
43
- available_quotes = all_quotes
44
 
45
- # Display a new quote
46
  if not st.session_state["current_quote"]:
47
- new_quote = random.choice(available_quotes)
48
- st.session_state["current_quote"] = new_quote
49
- st.session_state["shown_quotes"][selected_genre].append(new_quote["text"])
50
 
 
51
  current_quote = st.session_state["current_quote"]
52
- st.markdown(f"**{current_quote['text']}**")
53
  st.markdown(f"- *{current_quote['author']}*")
54
 
55
  # Buttons for user interaction
@@ -58,8 +73,8 @@ if selected_genre:
58
  with col4:
59
  if st.button("I Like This Quote"):
60
  st.success("Thank you for using the Random Quote Generator! Goodbye! 👋")
61
- st.stop()
62
 
63
  with col5:
64
  if st.button("Show Me Another Quote"):
65
- st.session_state["current_quote"] = None
 
1
  import streamlit as st
2
  import random
 
3
 
4
+ # Quotes categorized by genres
5
+ quotes = {
6
+ "Motivation": [
7
+ {"quote": "The best way to predict the future is to create it.", "author": "Peter Drucker"},
8
+ {"quote": "Believe you can and you're halfway there.", "author": "Theodore Roosevelt"},
9
+ {"quote": "Success is not final, failure is not fatal: It is the courage to continue that counts.", "author": "Winston Churchill"},
10
+ ],
11
+ "Happiness": [
12
+ {"quote": "Happiness is not something ready-made. It comes from your own actions.", "author": "Dalai Lama"},
13
+ {"quote": "For every minute you are angry you lose sixty seconds of happiness.", "author": "Ralph Waldo Emerson"},
14
+ {"quote": "Happiness depends upon ourselves.", "author": "Aristotle"},
15
+ ],
16
+ "Life": [
17
+ {"quote": "Your time is limited, don't waste it living someone else's life.", "author": "Steve Jobs"},
18
+ {"quote": "Life is 10% what happens to us and 90% how we react to it.", "author": "Charles R. Swindoll"},
19
+ {"quote": "Do not dwell in the past, do not dream of the future, concentrate the mind on the present moment.", "author": "Buddha"},
20
+ ],
21
+ }
22
 
23
  # App Title
24
  st.title("Random Quote Generator")
 
27
  st.subheader("Choose a category to view quotes:")
28
 
29
  # Display category buttons
30
+ categories = list(quotes.keys())
31
  selected_genre = None
32
 
33
+ col1, col2, col3 = st.columns(3)
34
+ if col1.button("Motivation"):
35
+ selected_genre = "Motivation"
36
+ elif col2.button("Happiness"):
37
+ selected_genre = "Happiness"
38
+ elif col3.button("Life"):
39
+ selected_genre = "Life"
40
 
41
  # Display quotes only if a category is selected
42
  if selected_genre:
43
+ # Initialize session state for the selected genre
44
  if "shown_quotes" not in st.session_state:
45
+ st.session_state["shown_quotes"] = {genre: [] for genre in quotes.keys()}
46
  if "current_quote" not in st.session_state or st.session_state["selected_genre"] != selected_genre:
47
  st.session_state["current_quote"] = None
48
  st.session_state["selected_genre"] = selected_genre
49
 
50
+ # Get a new quote that has not been shown yet
 
51
  available_quotes = [
52
+ quote for quote in quotes[selected_genre]
53
+ if quote not in st.session_state["shown_quotes"][selected_genre]
54
  ]
55
 
 
56
  if not available_quotes:
57
  st.warning("No more quotes left in this category! Resetting...")
58
  st.session_state["shown_quotes"][selected_genre] = []
59
+ available_quotes = quotes[selected_genre]
60
 
 
61
  if not st.session_state["current_quote"]:
62
+ st.session_state["current_quote"] = random.choice(available_quotes)
63
+ st.session_state["shown_quotes"][selected_genre].append(st.session_state["current_quote"])
 
64
 
65
+ # Display the current quote
66
  current_quote = st.session_state["current_quote"]
67
+ st.markdown(f"**{current_quote['quote']}**")
68
  st.markdown(f"- *{current_quote['author']}*")
69
 
70
  # Buttons for user interaction
 
73
  with col4:
74
  if st.button("I Like This Quote"):
75
  st.success("Thank you for using the Random Quote Generator! Goodbye! 👋")
76
+ st.stop() # End the app
77
 
78
  with col5:
79
  if st.button("Show Me Another Quote"):
80
+ st.session_state["current_quote"] = None # Clear the current quote