File size: 1,776 Bytes
23f81dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b2e285
23f81dc
 
 
 
 
 
5b2e285
 
 
 
 
23f81dc
5b2e285
 
23f81dc
5b2e285
 
 
 
 
 
23f81dc
 
 
 
 
 
 
 
 
 
 
 
5b2e285
23f81dc
 
 
 
5b2e285
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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?")