Spaces:
Sleeping
Sleeping
add ability to group by question text
#2
by
myshirk
- opened
app.py
CHANGED
|
@@ -37,14 +37,16 @@ df = get_data()
|
|
| 37 |
# Streamlit UI
|
| 38 |
st.title("π CGD Survey Explorer (Live DB)")
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
| 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 |
-
#
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
else:
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
st.dataframe(filtered[["country", "year", "question_text", "answer_text"]])
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 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 |
|