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- 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
|
8 |
-
load_dotenv()
|
9 |
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
-
|
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 |
-
#
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|