File size: 842 Bytes
58adc8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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?")