Edwin Salguero commited on
Commit
5745bd6
·
1 Parent(s): 48056ef

Fix FRED API key propagation - ensure key is available in both os.environ and st.secrets for the real app

Browse files
Files changed (1) hide show
  1. streamlit_app.py +17 -40
streamlit_app.py CHANGED
@@ -4,46 +4,23 @@ import os, sys
4
  import streamlit as st
5
  from dotenv import load_dotenv
6
 
7
- # 1) Load .env (locally) and merge in Streamlit secrets
8
- load_dotenv() # Local .env, no-op on Cloud
9
 
10
- st.write("🌱 os.environ keys with 'FRED': ", [k for k in os.environ if "FRED" in k])
11
- st.write("🌱 st.secrets keys: ", list(st.secrets.keys()))
 
 
 
12
 
13
- # Now fetch the key from both places
14
- env_key = os.getenv("FRED_API_KEY")
15
- secrets_key = st.secrets.get("FRED_API_KEY")
16
- st.write("🌱 os.getenv FRED_API_KEY:", env_key or "‹None›")
17
- st.write("🌱 st.secrets FRED_API_KEY:", secrets_key or "‹None›")
18
 
19
- # Test FRED API call if we have a key
20
- fred_key = env_key or secrets_key
21
- if fred_key:
22
- try:
23
- from fredapi import Fred
24
- fred = Fred(api_key=fred_key)
25
-
26
- # Quick sanity check:
27
- pts = fred.get_series("GDP", observation_start="2020-01-01", observation_end="2020-01-01")
28
- st.write("✅ Fetched test point:", pts.iloc[0])
29
- st.success("🎉 FRED API connection successful!")
30
-
31
- # If we get here, the API works - let's try the real app
32
- st.write("🚀 Attempting to load real app...")
33
-
34
- # 2) Ensure our code is importable
35
- HERE = os.path.dirname(os.path.abspath(__file__))
36
- sys.path.insert(0, os.path.join(HERE, "frontend"))
37
- sys.path.insert(0, HERE)
38
-
39
- # 3) Import and run your real app
40
- from frontend.app import main as app_main
41
- app_main()
42
-
43
- except Exception as e:
44
- st.error(f"🚨 fredapi call failed: {e}")
45
- st.stop()
46
- else:
47
- st.error("❌ No FRED API key found in environment or secrets")
48
- st.info("Please configure FRED_API_KEY in Streamlit Cloud secrets")
49
- st.stop()
 
4
  import streamlit as st
5
  from dotenv import load_dotenv
6
 
7
+ # 1. Load .env locally (no‐op in Cloud)…
8
+ load_dotenv()
9
 
10
+ # 2. Grab the key from env OR Cloud secrets
11
+ fred_key = os.getenv("FRED_API_KEY") or st.secrets.get("FRED_API_KEY")
12
+ if not fred_key:
13
+ st.error("❌ FRED API not available. Please configure your FRED_API_KEY.")
14
+ st.stop()
15
 
16
+ # 3. Propagate it into the actual env & secret namespace
17
+ os.environ["FRED_API_KEY"] = fred_key
18
+ st.secrets["FRED_API_KEY"] = fred_key # so any direct st.secrets lookup also works
 
 
19
 
20
+ # 4. Now hook up your frontend code
21
+ HERE = os.path.dirname(os.path.abspath(__file__))
22
+ sys.path.insert(0, os.path.join(HERE, "frontend"))
23
+ from app import main as app_main
24
+
25
+ # Call the main function directly for Streamlit Cloud
26
+ app_main()