import configparser import os import subprocess import sys def run_streamlit_app(): """ Reads the configuration file and launches the Streamlit app, optionally in headless mode. """ config_path = "config/config.ini" headless = False try: if os.path.exists(config_path): config = configparser.ConfigParser() config.read(config_path) if config.has_section("base"): headless = config.getboolean("base", "streamlit_headless", fallback=False) if headless: print(f"INFO: Headless mode enabled via {config_path}.") else: print(f"INFO: Headless mode disabled via {config_path}.") else: print(f"WARNING: [base] section not found in {config_path}. Defaulting to non-headless.") else: print(f"WARNING: Configuration file not found at {config_path}. Defaulting to non-headless.") except Exception as e: print(f"ERROR: Could not read headless config from {config_path}: {e}. Defaulting to non-headless.") headless = False # Ensure default on error # Construct the command command = [sys.executable, "-m", "streamlit", "run", "src/app.py"] if headless: command.extend(["--server.headless", "true"]) print(f"Running command: {' '.join(command)}") try: # Run Streamlit using subprocess.run which waits for completion # Use check=True to raise an error if Streamlit fails # Capture output might be useful for debugging but can be complex with interactive apps process = subprocess.Popen(command) process.wait() # Wait for the Streamlit process to exit print(f"Streamlit process finished with exit code: {process.returncode}") except FileNotFoundError: print("ERROR: 'streamlit' command not found. Make sure Streamlit is installed and in your PATH.") sys.exit(1) except Exception as e: print(f"ERROR: Failed to run Streamlit: {e}") sys.exit(1) if __name__ == "__main__": run_streamlit_app()