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 files
Files changed (1) hide show
  1. streamlit_app.py +28 -11
streamlit_app.py CHANGED
@@ -1,27 +1,44 @@
1
  #!/usr/bin/env python3
2
  """Diagnostic entry‐point for Streamlit Cloud deployment."""
3
  import os, sys
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- print(f"DEBUG: FRED_API_KEY from os.getenv = {os.getenv('FRED_API_KEY')}")
13
- print(f"DEBUG: FRED_API_KEY from st.secrets = {st.secrets.get('FRED_API_KEY')}")
14
- print(f"DEBUG: Final fred_key = {fred_key}")
15
- if not fred_key:
16
  st.error("❌ FRED API not available. Please configure your FRED_API_KEY.")
17
  st.info("Available environment variables: " + str(list(os.environ.keys())))
18
  st.info("Available secrets keys: " + str(list(st.secrets.keys())))
19
  st.stop()
20
 
21
- # 3. Propagate it into the actual env namespace only
22
- os.environ["FRED_API_KEY"] = fred_key
23
-
24
- # 4. Now hook up your frontend code
25
  HERE = os.path.dirname(os.path.abspath(__file__))
26
  sys.path.insert(0, os.path.join(HERE, "frontend"))
27
  from app import main as app_main
 
1
  #!/usr/bin/env python3
2
  """Diagnostic entry‐point for Streamlit Cloud deployment."""
3
  import os, sys
4
+
5
+ # CRITICAL: Set environment variable BEFORE importing streamlit
6
+ # This ensures the key is available when the frontend app imports
7
+
8
+ # 1. Try to get the key from environment first
9
+ fred_key = os.getenv("FRED_API_KEY")
10
+
11
+ # 2. If not in environment, we'll get it from Streamlit secrets later
12
+ if not fred_key:
13
+ # Import streamlit only when needed
14
+ import streamlit as st
15
+ fred_key = st.secrets.get("FRED_API_KEY")
16
+
17
+ print(f"DEBUG: FRED_API_KEY from os.getenv = {os.getenv('FRED_API_KEY')}")
18
+ print(f"DEBUG: Final fred_key = {fred_key}")
19
+
20
+ # 3. Set the environment variable IMMEDIATELY
21
+ if fred_key:
22
+ os.environ["FRED_API_KEY"] = fred_key
23
+ print(f"DEBUG: Set FRED_API_KEY in environment = {os.environ.get('FRED_API_KEY')}")
24
+ else:
25
+ print("DEBUG: No FRED API key found!")
26
+
27
+ # 4. Now import streamlit and frontend code
28
  import streamlit as st
29
  from dotenv import load_dotenv
30
 
31
+ # Load .env locally (no‐op in Cloud)…
32
  load_dotenv()
33
 
34
+ # 5. Double-check the key is available
35
+ if not os.getenv("FRED_API_KEY"):
 
 
 
 
36
  st.error("❌ FRED API not available. Please configure your FRED_API_KEY.")
37
  st.info("Available environment variables: " + str(list(os.environ.keys())))
38
  st.info("Available secrets keys: " + str(list(st.secrets.keys())))
39
  st.stop()
40
 
41
+ # 6. Now hook up your frontend code
 
 
 
42
  HERE = os.path.dirname(os.path.abspath(__file__))
43
  sys.path.insert(0, os.path.join(HERE, "frontend"))
44
  from app import main as app_main