SharmilNK commited on
Commit
39657f0
·
1 Parent(s): 239f1a7

Add conditional handling for top 3 makes chart when fewer than 3 VINs are searched

Browse files
Files changed (1) hide show
  1. streamlit_app.py +18 -5
streamlit_app.py CHANGED
@@ -21,9 +21,10 @@ import pandas as pd
21
  import matplotlib.pyplot as plt
22
  from collections import Counter
23
 
24
- BASE_DIR = os.path.dirname(os.path.abspath(__file__)) # Gets the current directory
25
  logo_img_file = os.path.join(BASE_DIR, "logo_image.png")
26
  car_img_file = os.path.join(BASE_DIR, "car_image.png")
 
27
 
28
  # Check if the 'make_history' key is already in Streamlit's session state
29
  if "make_history" not in st.session_state:
@@ -51,13 +52,25 @@ Display a pie chart of the top 3 most searched vehicle makes.
51
  st.warning("No vehicle makes have been searched yet.")
52
  return
53
  """
 
 
 
 
 
 
 
 
54
  counts = Counter(st.session_state.make_history)
55
- top_3 = counts.most_common(3) # A tuple to hold values: (make_name, count)
 
 
56
 
 
 
57
 
58
- labels = [item[0].upper() for item in top_3] # Label for the vehicle make, displayed in upper case for each slice of the pie chart
59
- sizes = [item[1] for item in top_3] # Count of each make in the Top3. Used to determine how large each slice of the pie chart is.
60
- colors = ['#4682B4', '#1E3F66', '#0B1F3A'] # Blue, dark blue, night blue : colors of pie chart slices
61
  with st.container():
62
 
63
  st.markdown("""
 
21
  import matplotlib.pyplot as plt
22
  from collections import Counter
23
 
24
+ BASE_DIR = os.path.dirname(os.path.abspath(__file__)) # Gets the current directory
25
  logo_img_file = os.path.join(BASE_DIR, "logo_image.png")
26
  car_img_file = os.path.join(BASE_DIR, "car_image.png")
27
+ TOP3 = 3 # Define top N makes to display
28
 
29
  # Check if the 'make_history' key is already in Streamlit's session state
30
  if "make_history" not in st.session_state:
 
52
  st.warning("No vehicle makes have been searched yet.")
53
  return
54
  """
55
+
56
+
57
+
58
+ if not st.session_state.make_history:
59
+ st.warning("No vehicle makes have been searched yet.")
60
+ return
61
+
62
+ # Count how many times each make was searched
63
  counts = Counter(st.session_state.make_history)
64
+
65
+ if len(counts) < TOP3:
66
+ st.warning(f"Not enough data to display Top 3. Showing Top {len(counts)} instead.")
67
 
68
+ # Get the top N (3 or fewer)
69
+ top_n = counts.most_common(min(len(counts), TOP3))
70
 
71
+ labels = [item[0].upper() for item in top_n] # Label for the vehicle make, displayed in upper case for each slice of the pie chart
72
+ sizes = [item[1] for item in top_n] # Count of each make in the Top3. Used to determine how large each slice of the pie chart is.
73
+ colors = ['#4682B4', '#1E3F66', '#0B1F3A'][:len(labels)] #[colors of pie chart slices][match slice count]
74
  with st.container():
75
 
76
  st.markdown("""