Spaces:
Running
Running
add option for AWS database
Browse files
app.py
CHANGED
@@ -1,46 +1,55 @@
|
|
1 |
|
2 |
import streamlit as st
|
3 |
import pandas as pd
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
st.sidebar.header("๐ Filter Questions")
|
32 |
-
selected_country = st.sidebar.selectbox("Select Country", sorted(df["
|
33 |
-
selected_year = st.sidebar.selectbox("Select Year", sorted(df["
|
34 |
keyword = st.sidebar.text_input("Keyword Search", "")
|
35 |
|
|
|
36 |
filtered = df[
|
37 |
-
(df["
|
38 |
-
(df["
|
39 |
-
(df["
|
40 |
]
|
41 |
|
42 |
st.markdown(f"### Results for **{selected_country}** in **{selected_year}**")
|
43 |
-
st.dataframe(filtered[["
|
44 |
|
45 |
if filtered.empty:
|
46 |
st.info("No matching questions found.")
|
|
|
|
1 |
|
2 |
import streamlit as st
|
3 |
import pandas as pd
|
4 |
+
import psycopg2
|
5 |
+
import os
|
6 |
+
|
7 |
+
# Load DB credentials from Hugging Face secrets or environment variables
|
8 |
+
DB_HOST = os.getenv("DB_HOST")
|
9 |
+
DB_PORT = os.getenv("DB_PORT", "5432")
|
10 |
+
DB_NAME = os.getenv("DB_NAME")
|
11 |
+
DB_USER = os.getenv("DB_USER")
|
12 |
+
DB_PASSWORD = os.getenv("DB_PASSWORD")
|
13 |
+
|
14 |
+
@st.cache_data(ttl=600)
|
15 |
+
def get_data():
|
16 |
+
try:
|
17 |
+
conn = psycopg2.connect(
|
18 |
+
host=DB_HOST,
|
19 |
+
port=DB_PORT,
|
20 |
+
dbname=DB_NAME,
|
21 |
+
user=DB_USER,
|
22 |
+
password=DB_PASSWORD
|
23 |
+
)
|
24 |
+
query = "SELECT country, year, section, question_code, question_text, answer_code, answer_text FROM survey_data;"
|
25 |
+
df = pd.read_sql_query(query, conn)
|
26 |
+
conn.close()
|
27 |
+
return df
|
28 |
+
except Exception as e:
|
29 |
+
st.error("Failed to connect to the database.")
|
30 |
+
st.stop()
|
31 |
+
|
32 |
+
# Load data
|
33 |
+
df = get_data()
|
34 |
+
|
35 |
+
# Streamlit UI
|
36 |
+
st.title("๐ CGD Survey Explorer (Live DB)")
|
37 |
|
38 |
st.sidebar.header("๐ Filter Questions")
|
39 |
+
selected_country = st.sidebar.selectbox("Select Country", sorted(df["country"].unique()))
|
40 |
+
selected_year = st.sidebar.selectbox("Select Year", sorted(df["year"].unique()))
|
41 |
keyword = st.sidebar.text_input("Keyword Search", "")
|
42 |
|
43 |
+
# Filtered data
|
44 |
filtered = df[
|
45 |
+
(df["country"] == selected_country) &
|
46 |
+
(df["year"] == selected_year) &
|
47 |
+
(df["question_text"].str.contains(keyword, case=False, na=False))
|
48 |
]
|
49 |
|
50 |
st.markdown(f"### Results for **{selected_country}** in **{selected_year}**")
|
51 |
+
st.dataframe(filtered[["country", "question_text", "answer_text"]])
|
52 |
|
53 |
if filtered.empty:
|
54 |
st.info("No matching questions found.")
|
55 |
+
|