# import os, types, streamlit as st | |
# # Fetch the hidden code from env var | |
# app_code = os.environ.get("APP_CODE", "") | |
# def execute_code(code_str): | |
# module = types.ModuleType("dynamic_app") | |
# try: | |
# exec(code_str, module.__dict__) | |
# if hasattr(module, "main"): | |
# module.main() | |
# except Exception as e: | |
# st.error(f"Error in hidden code: {e}") | |
# if app_code: | |
# execute_code(app_code) | |
# else: | |
# st.error("APP_CODE is empty. Did you set it?") | |
import os, types, streamlit as st | |
from functools import wraps | |
# Clear all caches to ensure fresh execution | |
st.session_state.clear() | |
st.cache_data.clear() | |
st.cache_resource.clear() | |
# Fetch the hidden code from env var | |
app_code = os.environ.get("APP_CODE", "") | |
def execute_code(code_str): | |
# Create a new module for each execution | |
module = types.ModuleType("dynamic_app") | |
try: | |
# Execute in a fresh namespace each time | |
exec(code_str, module.__dict__) | |
if hasattr(module, "main"): | |
module.main() | |
except Exception as e: | |
st.error(f"Error in hidden code: {e}") | |
# Force rerun when query parameters change | |
def check_for_query_change(): | |
current_query = str(st.experimental_get_query_params()) | |
if 'last_query' not in st.session_state: | |
st.session_state.last_query = current_query | |
elif current_query != st.session_state.last_query: | |
st.session_state.last_query = current_query | |
st.experimental_rerun() | |
# Check for query changes before executing | |
check_for_query_change() | |
if app_code: | |
# Create a container that will be cleared on each run | |
main_container = st.container() | |
with main_container: | |
execute_code(app_code) | |
else: | |
st.error("APP_CODE is empty. Did you set it?") |