myyim commited on
Commit
296130e
·
verified ·
1 Parent(s): 7c20620

Update src/app.py

Browse files
Files changed (1) hide show
  1. src/app.py +19 -9
src/app.py CHANGED
@@ -1,18 +1,28 @@
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?")
 
1
+ import streamlit as st
2
+ import os
3
+ import types
4
 
5
+ # Retrieve your hidden code from the environment variable
6
  app_code = os.environ.get("APP_CODE", "")
7
+ st.write(app_code)
8
 
9
+ def execute_code(code_str: str):
10
+ """
11
+ Dynamically compile and execute the provided code string.
12
+ If it defines a `main()` function, call it.
13
+ """
14
  try:
15
+ # Create a fresh module namespace
16
+ dynamic_module = types.ModuleType("dynamic_app")
17
+ exec(code_str, dynamic_module.__dict__)
18
+ # If the app defines a main(), invoke it
19
+ if hasattr(dynamic_module, "main"):
20
+ dynamic_module.main()
21
  except Exception as e:
22
+ st.error(f"Error while executing hidden code: {e}")
23
 
24
+ # Load and run, or show an error
25
  if app_code:
26
  execute_code(app_code)
27
  else:
28
+ st.error("Cannot load the app code. Did you set the APP_CODE secret correctly?")