Edwin Salguero
commited on
Commit
·
864919d
1
Parent(s):
3878dad
Implement Streamlit-native solution: remove dotenv dependency and use st.secrets directly
Browse files- frontend/app.py +1 -3
- streamlit_app.py +10 -33
frontend/app.py
CHANGED
|
@@ -10,8 +10,6 @@ import os
|
|
| 10 |
import sys
|
| 11 |
import io
|
| 12 |
from typing import Dict, List, Optional
|
| 13 |
-
from dotenv import load_dotenv
|
| 14 |
-
load_dotenv()
|
| 15 |
|
| 16 |
import os
|
| 17 |
print("DEBUG: FRED_API_KEY from os.getenv =", os.getenv('FRED_API_KEY'))
|
|
@@ -92,7 +90,7 @@ def load_config():
|
|
| 92 |
print(f"DEBUG: load_config() - FRED_API_KEY from os.getenv = {fred_key}")
|
| 93 |
if not fred_key:
|
| 94 |
try:
|
| 95 |
-
fred_key = st.secrets
|
| 96 |
print(f"DEBUG: load_config() - FRED_API_KEY from st.secrets = {fred_key}")
|
| 97 |
except Exception as e:
|
| 98 |
print(f"DEBUG: load_config() - Error getting from st.secrets: {e}")
|
|
|
|
| 10 |
import sys
|
| 11 |
import io
|
| 12 |
from typing import Dict, List, Optional
|
|
|
|
|
|
|
| 13 |
|
| 14 |
import os
|
| 15 |
print("DEBUG: FRED_API_KEY from os.getenv =", os.getenv('FRED_API_KEY'))
|
|
|
|
| 90 |
print(f"DEBUG: load_config() - FRED_API_KEY from os.getenv = {fred_key}")
|
| 91 |
if not fred_key:
|
| 92 |
try:
|
| 93 |
+
fred_key = st.secrets["FRED_API_KEY"]
|
| 94 |
print(f"DEBUG: load_config() - FRED_API_KEY from st.secrets = {fred_key}")
|
| 95 |
except Exception as e:
|
| 96 |
print(f"DEBUG: load_config() - Error getting from st.secrets: {e}")
|
streamlit_app.py
CHANGED
|
@@ -1,44 +1,21 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
-
"""
|
| 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 |
-
#
|
| 35 |
-
|
| 36 |
-
|
|
|
|
| 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 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
+
"""Streamlit-native entry point for Streamlit Cloud deployment."""
|
| 3 |
import os, sys
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# Streamlit-native solution: Use st.secrets directly
|
| 7 |
+
fred_key = os.getenv("FRED_API_KEY") or st.secrets["FRED_API_KEY"]
|
| 8 |
+
if not fred_key:
|
| 9 |
+
st.error("❌ FRED API key not found. Configure it in Streamlit Cloud Secrets.")
|
| 10 |
st.info("Available environment variables: " + str(list(os.environ.keys())))
|
| 11 |
st.info("Available secrets keys: " + str(list(st.secrets.keys())))
|
| 12 |
st.stop()
|
| 13 |
|
| 14 |
+
# Set the environment variable for the frontend app
|
| 15 |
+
os.environ["FRED_API_KEY"] = fred_key
|
| 16 |
+
print(f"DEBUG: Set FRED_API_KEY in environment = {os.environ.get('FRED_API_KEY')}")
|
| 17 |
+
|
| 18 |
+
# Now hook up your frontend code
|
| 19 |
HERE = os.path.dirname(os.path.abspath(__file__))
|
| 20 |
sys.path.insert(0, os.path.join(HERE, "frontend"))
|
| 21 |
from app import main as app_main
|