QuantLib_Pro / streamlit_app.py
gdukens's picture
Initial HF Space deploy bundle
72fc0ee
Raw History Blame Contribute Delete
8.15 kB
"""
QuantLib Pro - Streamlit application entry point.
Uses st.navigation with Material Design icons (Streamlit 1.36+).
"""
import streamlit as st
import os
# Global page config - called ONCE here; pages must NOT call it
st.set_page_config(
page_title="QuantLib Pro",
page_icon=":material/candlestick_chart:",
layout="wide",
initial_sidebar_state="expanded",
)
# Check API credentials and show setup dialog if needed
def check_and_prompt_credentials():
"""Check for API credentials and prompt user if they're missing."""
fred_key = os.getenv("FRED_API_KEY", "")
fred_configured = fred_key and fred_key != "REPLACE_WITH_FRED_API_KEY" and len(fred_key) > 10
av_key = os.getenv("ALPHA_VANTAGE_API_KEY", "")
av_configured = av_key and av_key != "REPLACE_WITH_ALPHA_VANTAGE_KEY" and len(av_key) > 10
# If credentials are missing, show setup instructions
if not fred_configured or not av_configured:
st.warning("πŸ”‘ **API Credentials Setup Required**")
with st.expander("**Click here to set up your API credentials for full functionality**", expanded=True):
st.markdown("""
### πŸš€ Get the Most Out of QuantLib Pro!
To access all features including real-time market data, economic indicators, and enhanced analytics,
please configure your free API credentials:
""")
col1, col2 = st.columns(2)
with col1:
if not fred_configured:
st.error("❌ **FRED API Key Missing**")
st.markdown("""
**Federal Reserve Economic Data (FRED)**
- Free API for US economic data
- Get your key: [https://fred.stlouisfed.org/docs/api/api_key.html](https://fred.stlouisfed.org/docs/api/api_key.html)
- Required for: Economic indicators, yield curves, inflation data
""")
else:
st.success("βœ… **FRED API Key Configured**")
with col2:
if not av_configured:
st.error("❌ **Alpha Vantage API Key Missing**")
st.markdown("""
**Alpha Vantage Market Data**
- Free API for stock market data
- Get your key: [https://www.alphavantage.co/support/#api-key](https://www.alphavantage.co/support/#api-key)
- Required for: Real-time quotes, technical indicators, forex data
""")
else:
st.success("βœ… **Alpha Vantage API Key Configured**")
st.info("""
**πŸ“ How to Configure:**
1. Get your free API keys from the links above
2. Go to **Settings** (in the Developer section of the sidebar)
3. Enter your API keys in the configuration panel
4. Restart the application to apply changes
**πŸ’‘ Note:** You can still use QuantLib Pro without these keys, but some features will be limited to Yahoo Finance data only.
""")
if st.button("πŸ”§ Go to Settings", type="primary"):
st.switch_page("pages/18_Settings.py")
st.divider()
# Show credentials prompt on app startup
check_and_prompt_credentials()
# Persistent sidebar shown on every page
with st.sidebar:
st.markdown("## QuantLib Pro")
st.caption("Enterprise Quantitative Finance Platform")
st.divider()
st.markdown("### Analysis Parameters")
st.markdown("##### Date Range")
c1, c2 = st.columns(2)
with c1:
start_date = st.date_input("Start", value=None)
with c2:
end_date = st.date_input("End", value=None)
st.markdown("##### Risk Parameters")
risk_free_rate = st.slider(
"Risk-free Rate (%)", 0.0, 10.0, 2.0, 0.1
) / 100
confidence_level = st.slider(
"VaR Confidence (%)", 90, 99, 95
) / 100
st.session_state.risk_free_rate = risk_free_rate
st.session_state.confidence_level = confidence_level
if start_date:
st.session_state.start_date = start_date
if end_date:
st.session_state.end_date = end_date
st.divider()
st.markdown("### System Status")
# Import requests at module level to avoid issues
try:
import requests
except ImportError:
requests = None
# Check API status (only if not in cloud environment)
is_cloud_deployment = os.getenv("STREAMLIT_SHARING_MODE") or os.getenv("STREAMLIT_CLOUD") or "streamlit.app" in os.getenv("HOSTNAME", "")
if not is_cloud_deployment and requests:
try:
r = requests.get("http://localhost:8000/health/", timeout=1)
st.success("API: Online") if r.status_code == 200 else st.warning("API: Degraded")
except Exception:
st.info("API: Standalone Mode")
else:
st.info("API: Cloud Mode")
# Check data providers
fred_key = os.getenv("FRED_API_KEY", "")
fred_ok = fred_key and fred_key != "REPLACE_WITH_FRED_API_KEY" and len(fred_key) > 10
av_key = os.getenv("ALPHA_VANTAGE_API_KEY", "")
av_ok = av_key and av_key != "REPLACE_WITH_ALPHA_VANTAGE_KEY" and len(av_key) > 10
st.success("UI: Online")
st.success("Yahoo Finance: Ready")
if fred_ok:
st.success("FRED: Connected")
else:
st.warning("FRED: Configure in Settings")
if av_ok:
st.success("Alpha Vantage: Connected")
else:
st.warning("Alpha Vantage: Configure in Settings")
st.divider()
st.caption("v1.0.0 Β· Streamlit Β· FastAPI")
# Navigation with grouped sections and Material Design icons
pg = st.navigation(
{
"Core": [
st.Page("pages/0_Home.py", title="Home", icon=":material/home:"),
st.Page("pages/1_Portfolio.py", title="Portfolio", icon=":material/pie_chart:"),
st.Page("pages/2_Risk.py", title="Risk", icon=":material/shield:"),
st.Page("pages/3_Options.py", title="Options", icon=":material/calculate:"),
],
"Market": [
st.Page("pages/4_Market_Regime.py", title="Market Regime", icon=":material/track_changes:"),
st.Page("pages/5_Macro.py", title="Macro", icon=":material/public:"),
st.Page("pages/6_Volatility_Surface.py", title="Vol Surface", icon=":material/show_chart:"),
st.Page("pages/10_Market_Analysis.py", title="Market Analysis", icon=":material/bar_chart:"),
],
"Trading": [
st.Page("pages/7_Backtesting.py", title="Backtesting", icon=":material/history:"),
st.Page("pages/11_Trading_Signals.py", title="Trading Signals", icon=":material/sensors:"),
st.Page("pages/8_Advanced_Analytics.py", title="Analytics", icon=":material/analytics:"),
],
"Infrastructure": [
st.Page("pages/12_Liquidity.py", title="Liquidity", icon=":material/water_drop:"),
st.Page("pages/13_Systemic_Risk.py", title="Systemic Risk", icon=":material/hub:"),
st.Page("pages/9_Data_Management.py", title="Data Management", icon=":material/storage:"),
],
"Monitoring": [
st.Page("pages/14_Trader_Stress_Monitor.py", title="Stress Monitor", icon=":material/psychology:"),
st.Page("pages/16_UAT_Dashboard.py", title="UAT Dashboard", icon=":material/dashboard:"),
st.Page("pages/15_Testing.py", title="Testing", icon=":material/science:"),
],
"Developer": [
st.Page("pages/17_API_Explorer.py", title="API Explorer", icon=":material/api:"),
st.Page("pages/18_Settings.py", title="Settings", icon=":material/settings:"),
],
}
)
pg.run()