myshirk commited on
Commit
23381bb
ยท
verified ยท
1 Parent(s): 7eaf4dc

add option for AWS database

Browse files
Files changed (1) hide show
  1. app.py +41 -32
app.py CHANGED
@@ -1,46 +1,55 @@
1
 
2
  import streamlit as st
3
  import pandas as pd
4
-
5
- # Sample dummy data
6
- data = {
7
- "Country": ["Mexico", "Mexico", "Peru", "Peru", "Guatemala"],
8
- "Year": [2020, 2021, 2020, 2021, 2021],
9
- "Variable": ["Q1", "Q2", "Q3", "Q4", "Q5"],
10
- "Question": [
11
- "How often do you visit a doctor?",
12
- "Do you trust the healthcare system?",
13
- "Have you received the COVID-19 vaccine?",
14
- "What is your primary source of news?",
15
- "Do you think education is affordable?"
16
- ],
17
- "Responses": [
18
- "Never, Rarely, Sometimes, Often",
19
- "Yes, No",
20
- "Yes, No",
21
- "TV, Internet, Radio",
22
- "Yes, No"
23
- ]
24
- }
25
-
26
- df = pd.DataFrame(data)
27
-
28
- # Streamlit UI layout
29
- st.title("๐ŸŒ CGD Survey Explorer (PoC)")
 
 
 
 
 
 
 
30
 
31
  st.sidebar.header("๐Ÿ”Ž Filter Questions")
32
- selected_country = st.sidebar.selectbox("Select Country", sorted(df["Country"].unique()))
33
- selected_year = st.sidebar.selectbox("Select Year", sorted(df["Year"].unique()))
34
  keyword = st.sidebar.text_input("Keyword Search", "")
35
 
 
36
  filtered = df[
37
- (df["Country"] == selected_country) &
38
- (df["Year"] == selected_year) &
39
- (df["Question"].str.contains(keyword, case=False, na=False))
40
  ]
41
 
42
  st.markdown(f"### Results for **{selected_country}** in **{selected_year}**")
43
- st.dataframe(filtered[["Variable", "Question", "Responses"]])
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
+