Spaces:
Sleeping
Sleeping
import streamlit as st | |
import os | |
import types | |
# Retrieve your hidden code from the environment variable | |
app_code = os.environ.get("APP_CODE", "") | |
def execute_code(code_str: str): | |
""" | |
Dynamically compile and execute the provided code string. | |
If it defines a `main()` function, call it. | |
""" | |
try: | |
# Create a fresh module namespace | |
dynamic_module = types.ModuleType("dynamic_app") | |
exec(code_str, dynamic_module.__dict__) | |
# If the app defines a main(), invoke it | |
if hasattr(dynamic_module, "main"): | |
dynamic_module.main() | |
except Exception as e: | |
st.error(f"Error while executing hidden code: {e}") | |
# Load and run, or show an error | |
if app_code: | |
execute_code(app_code) | |
else: | |
st.error("Cannot load the app code. Did you set the APP_CODE secret correctly?") | |