myshirk commited on
Commit
c74ae45
ยท
verified ยท
1 Parent(s): 1068edf

allow optional filter

Browse files
Files changed (1) hide show
  1. app.py +23 -10
app.py CHANGED
@@ -38,20 +38,33 @@ df = get_data()
38
  st.title("๐ŸŒ CGD Survey Explorer (Live DB)")
39
 
40
  st.sidebar.header("๐Ÿ”Ž Filter Questions")
41
- selected_country = st.sidebar.selectbox("Select Country", sorted(df["country"].unique()))
42
- selected_year = st.sidebar.selectbox("Select Year", sorted(df["year"].unique()))
43
- keyword = st.sidebar.text_input("Keyword Search", "")
44
 
45
- # Filtered data
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  filtered = df[
47
- (df["country"] == selected_country) &
48
- (df["year"] == selected_year) &
49
- (df["question_text"].str.contains(keyword, case=False, na=False))
50
  ]
51
 
52
- st.markdown(f"### Results for **{selected_country}** in **{selected_year}**")
53
- st.dataframe(filtered[["country", "question_text", "answer_text"]])
54
 
55
- if filtered.empty:
 
 
 
 
56
  st.info("No matching questions found.")
57
 
 
38
  st.title("๐ŸŒ CGD Survey Explorer (Live DB)")
39
 
40
  st.sidebar.header("๐Ÿ”Ž Filter Questions")
 
 
 
41
 
42
+ # Dropdown filters (optional)
43
+ countries = sorted(df["country"].dropna().unique())
44
+ years = sorted(df["year"].dropna().unique())
45
+
46
+ selected_countries = st.sidebar.multiselect("Select Country", countries, default=countries)
47
+ selected_years = st.sidebar.multiselect("Select Year", years, default=years)
48
+ keyword = st.sidebar.text_input("Keyword Search (in question)", "")
49
+
50
+ # Column selector
51
+ all_columns = df.columns.tolist()
52
+ default_columns = ["country", "question", "responses"]
53
+ selected_columns = st.sidebar.multiselect("Columns to Display", all_columns, default=default_columns)
54
+
55
+ # Apply filters
56
  filtered = df[
57
+ df["country"].isin(selected_countries) &
58
+ df["year"].isin(selected_years)
 
59
  ]
60
 
61
+ if keyword:
62
+ filtered = filtered[filtered["question"].str.contains(keyword, case=False, na=False)]
63
 
64
+ # Show results
65
+ st.markdown("### Filtered Results")
66
+ if not filtered.empty:
67
+ st.dataframe(filtered[selected_columns])
68
+ else:
69
  st.info("No matching questions found.")
70