Update src/streamlit_app.py
Browse files- src/streamlit_app.py +4 -45
src/streamlit_app.py
CHANGED
|
@@ -1,61 +1,20 @@
|
|
| 1 |
-
# import os, types, streamlit as st
|
| 2 |
-
|
| 3 |
-
# # Fetch the hidden code from env var
|
| 4 |
-
# app_code = os.environ.get("APP_CODE", "")
|
| 5 |
-
|
| 6 |
-
# def execute_code(code_str):
|
| 7 |
-
# module = types.ModuleType("dynamic_app")
|
| 8 |
-
# try:
|
| 9 |
-
# exec(code_str, module.__dict__)
|
| 10 |
-
# if hasattr(module, "main"):
|
| 11 |
-
# module.main()
|
| 12 |
-
# except Exception as e:
|
| 13 |
-
# st.error(f"Error in hidden code: {e}")
|
| 14 |
-
|
| 15 |
-
# if app_code:
|
| 16 |
-
# execute_code(app_code)
|
| 17 |
-
# else:
|
| 18 |
-
# st.error("APP_CODE is empty. Did you set it?")
|
| 19 |
-
|
| 20 |
-
|
| 21 |
import os, types, streamlit as st
|
| 22 |
-
from functools import wraps
|
| 23 |
-
|
| 24 |
-
# Clear all caches to ensure fresh execution
|
| 25 |
-
st.session_state.clear()
|
| 26 |
-
st.cache_data.clear()
|
| 27 |
-
st.cache_resource.clear()
|
| 28 |
|
| 29 |
# Fetch the hidden code from env var
|
| 30 |
app_code = os.environ.get("APP_CODE", "")
|
| 31 |
|
| 32 |
def execute_code(code_str):
|
| 33 |
-
# Create a new module for each execution
|
| 34 |
module = types.ModuleType("dynamic_app")
|
| 35 |
try:
|
| 36 |
-
# Execute in a fresh namespace each time
|
| 37 |
exec(code_str, module.__dict__)
|
| 38 |
if hasattr(module, "main"):
|
| 39 |
module.main()
|
| 40 |
except Exception as e:
|
| 41 |
st.error(f"Error in hidden code: {e}")
|
| 42 |
|
| 43 |
-
# Force rerun when query parameters change
|
| 44 |
-
def check_for_query_change():
|
| 45 |
-
current_query = str(st.experimental_get_query_params())
|
| 46 |
-
if 'last_query' not in st.session_state:
|
| 47 |
-
st.session_state.last_query = current_query
|
| 48 |
-
elif current_query != st.session_state.last_query:
|
| 49 |
-
st.session_state.last_query = current_query
|
| 50 |
-
st.experimental_rerun()
|
| 51 |
-
|
| 52 |
-
# Check for query changes before executing
|
| 53 |
-
check_for_query_change()
|
| 54 |
-
|
| 55 |
if app_code:
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
with main_container:
|
| 59 |
-
execute_code(app_code)
|
| 60 |
else:
|
| 61 |
-
st.error("APP_CODE is empty. Did you set it?")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os, types, streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
# Fetch the hidden code from env var
|
| 4 |
app_code = os.environ.get("APP_CODE", "")
|
| 5 |
|
| 6 |
def execute_code(code_str):
|
|
|
|
| 7 |
module = types.ModuleType("dynamic_app")
|
| 8 |
try:
|
|
|
|
| 9 |
exec(code_str, module.__dict__)
|
| 10 |
if hasattr(module, "main"):
|
| 11 |
module.main()
|
| 12 |
except Exception as e:
|
| 13 |
st.error(f"Error in hidden code: {e}")
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
if app_code:
|
| 16 |
+
execute_code(app_code)
|
| 17 |
+
st.rerun()
|
|
|
|
|
|
|
| 18 |
else:
|
| 19 |
+
st.error("APP_CODE is empty. Did you set it?")
|
| 20 |
+
|