myshirk commited on
Commit
cc9cf8b
ยท
verified ยท
1 Parent(s): 4e71d04

add ability to group by question text (#2)

Browse files

- add ability to group by question text (443384fa02a3fa8384ecd9404799d4ba76d6cd74)

Files changed (1) hide show
  1. app.py +41 -16
app.py CHANGED
@@ -37,14 +37,16 @@ df = get_data()
37
  # Streamlit UI
38
  st.title("๐ŸŒ CGD Survey Explorer (Live DB)")
39
 
40
- # Multiselect filters (defaults = empty, shows all if none selected)
 
 
41
  country_options = sorted(df["country"].dropna().unique())
42
  year_options = sorted(df["year"].dropna().unique())
43
 
44
  selected_countries = st.sidebar.multiselect("Select Country/Countries", country_options)
45
  selected_years = st.sidebar.multiselect("Select Year(s)", year_options)
46
-
47
  keyword = st.sidebar.text_input("Keyword Search", "")
 
48
 
49
  # Apply filters
50
  filtered = df[
@@ -53,21 +55,44 @@ filtered = df[
53
  (df["question_text"].str.contains(keyword, case=False, na=False))
54
  ]
55
 
56
- # Generate dynamic subheading
57
- heading_parts = []
58
- if selected_countries:
59
- heading_parts.append("Countries: " + ", ".join(selected_countries))
60
- if selected_years:
61
- heading_parts.append("Years: " + ", ".join(map(str, selected_years)))
62
- if heading_parts:
63
- st.markdown("### Results for " + " | ".join(heading_parts))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  else:
65
- st.markdown("### Results for All Countries and Years")
 
 
 
 
 
 
 
 
 
66
 
67
- # Display results including answer_text
68
- st.dataframe(filtered[["country", "year", "question_text", "answer_text"]])
69
 
70
- # Empty result message
71
- if filtered.empty:
72
- st.info("No matching questions found.")
73
 
 
37
  # Streamlit UI
38
  st.title("๐ŸŒ CGD Survey Explorer (Live DB)")
39
 
40
+ st.sidebar.header("๐Ÿ”Ž Filter Questions")
41
+
42
+ # Multiselect filters with default = show all
43
  country_options = sorted(df["country"].dropna().unique())
44
  year_options = sorted(df["year"].dropna().unique())
45
 
46
  selected_countries = st.sidebar.multiselect("Select Country/Countries", country_options)
47
  selected_years = st.sidebar.multiselect("Select Year(s)", year_options)
 
48
  keyword = st.sidebar.text_input("Keyword Search", "")
49
+ group_by_question = st.sidebar.checkbox("Group by Question Text")
50
 
51
  # Apply filters
52
  filtered = df[
 
55
  (df["question_text"].str.contains(keyword, case=False, na=False))
56
  ]
57
 
58
+ # Output
59
+ if group_by_question:
60
+ st.subheader("๐Ÿ“Š Grouped by Question Text")
61
+
62
+ grouped = (
63
+ filtered.groupby("question_text")
64
+ .agg({
65
+ "country": lambda x: sorted(set(x)),
66
+ "year": lambda x: sorted(set(x)),
67
+ "answer_text": lambda x: list(x)[:3] # preview up to 3 answers
68
+ })
69
+ .reset_index()
70
+ .rename(columns={
71
+ "country": "Countries",
72
+ "year": "Years",
73
+ "answer_text": "Sample Answers"
74
+ })
75
+ )
76
+
77
+ st.dataframe(grouped)
78
+
79
+ if grouped.empty:
80
+ st.info("No questions found with current filters.")
81
+
82
  else:
83
+ # Context-aware heading
84
+ heading_parts = []
85
+ if selected_countries:
86
+ heading_parts.append("Countries: " + ", ".join(selected_countries))
87
+ if selected_years:
88
+ heading_parts.append("Years: " + ", ".join(map(str, selected_years)))
89
+ if heading_parts:
90
+ st.markdown("### Results for " + " | ".join(heading_parts))
91
+ else:
92
+ st.markdown("### Results for All Countries and Years")
93
 
94
+ st.dataframe(filtered[["country", "year", "question_text", "answer_text"]])
 
95
 
96
+ if filtered.empty:
97
+ st.info("No matching questions found.")
 
98