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 files3 unmodified lines456789101112131415161718192021222324252627282930313233343536373839404142434445464748493 unmodified linesimport streamlit as stfrom dotenv import load_dotenv# 1) Load .env (locally) and merge in Streamlit secretsload_dotenv() # Local .env, no-op on Cloudst.write("🌱 os.environ keys with 'FRED': ", [k for k in os.environ if "FRED" in k])st.write("🌱 st.secrets keys: ", list(st.secrets.keys()))# Now fetch the key from both placesenv_key = os.getenv("FRED_API_KEY")secrets_key = st.secrets.get("FRED_API_KEY")st.write("🌱 os.getenv FRED_API_KEY:", env_key or "‹None›")st.write("🌱 st.secrets FRED_API_KEY:", secrets_key or "‹None›")# Test FRED API call if we have a keyfred_key = env_key or secrets_keyif fred_key:try:from fredapi import Fredfred = Fred(api_key=fred_key)# Quick sanity check:pts = fred.get_series("GDP", observation_start="2020-01-01", observation_end="2020-01-01")st.write("✅ Fetched test point:", pts.iloc[0])st.success("🎉 FRED API connection successful!")# If we get here, the API works - let's try the real appst.write("🚀 Attempting to load real app...")# 2) Ensure our code is importableHERE = os.path.dirname(os.path.abspath(__file__))sys.path.insert(0, os.path.join(HERE, "frontend"))sys.path.insert(0, HERE)# 3) Import and run your real appfrom frontend.app import main as app_mainapp_main()except Exception as e:st.error(f"🚨 fredapi call failed: {e}")st.stop()else:st.error("❌ No FRED API key found in environment or secrets")st.info("Please configure FRED_API_KEY in Streamlit Cloud secrets")st.stop()3 unmodified lines45678910111213141516171819202122232425263 unmodified linesimport streamlit as stfrom dotenv import load_dotenv# 1. Load .env locally (no‐op in Cloud)…load_dotenv()# 2. Grab the key from env OR Cloud secretsfred_key = os.getenv("FRED_API_KEY") or st.secrets.get("FRED_API_KEY")if not fred_key:st.error("❌ FRED API not available. Please configure your FRED_API_KEY.")st.stop()# 3. Propagate it into the actual env & secret namespaceos.environ["FRED_API_KEY"] = fred_keyst.secrets["FRED_API_KEY"] = fred_key # so any direct st.secrets lookup also works# 4. Now hook up your frontend codeHERE = os.path.dirname(os.path.abspath(__file__))sys.path.insert(0, os.path.join(HERE, "frontend"))from app import main as app_main# Call the main function directly for Streamlit Cloudapp_main()
streamlit_app.py+17-40