Edwin Salguero commited on
Commit ·
3878dad
1
Parent(s): 82f45f2
Fix execution order: set FRED_API_KEY environment variable BEFORE importing streamlit and frontend app
Browse files1234567891011121314151617181920212223242526272829307313233101112131415343536373839402122232441424344#!/usr/bin/env python3"""Diagnostic entry‐point for Streamlit Cloud deployment."""import os, sys
# CRITICAL: Set environment variable BEFORE importing streamlit# This ensures the key is available when the frontend app imports
# 1. Try to get the key from environment firstfred_key = os.getenv("FRED_API_KEY")
# 2. If not in environment, we'll get it from Streamlit secrets laterif not fred_key: # Import streamlit only when needed import streamlit as st fred_key = st.secrets.get("FRED_API_KEY")
print(f"DEBUG: FRED_API_KEY from os.getenv = {os.getenv('FRED_API_KEY')}")print(f"DEBUG: Final fred_key = {fred_key}")
# 3. Set the environment variable IMMEDIATELYif fred_key: os.environ["FRED_API_KEY"] = fred_key print(f"DEBUG: Set FRED_API_KEY in environment = {os.environ.get('FRED_API_KEY')}")else: print("DEBUG: No FRED API key found!")
# 4. Now import streamlit and frontend codeimport streamlit as stfrom dotenv import load_dotenv
# 1. Load .env locally (no‐op in Cloud)…# 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")print(f"DEBUG: FRED_API_KEY from os.getenv = {os.getenv('FRED_API_KEY')}")print(f"DEBUG: FRED_API_KEY from st.secrets = {st.secrets.get('FRED_API_KEY')}")print(f"DEBUG: Final fred_key = {fred_key}")if not fred_key:# 5. Double-check the key is availableif not os.getenv("FRED_API_KEY"): st.error("❌ FRED API not available. Please configure your FRED_API_KEY.") st.info("Available environment variables: " + str(list(os.environ.keys()))) st.info("Available secrets keys: " + str(list(st.secrets.keys()))) st.stop()
# 3. Propagate it into the actual env namespace onlyos.environ["FRED_API_KEY"] = fred_key
# 4. Now hook up your frontend code# 6. 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_mainstreamlit_app.py+28-11