-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_function_handler.py
More file actions
49 lines (47 loc) · 2.35 KB
/
Copy pathcustom_function_handler.py
File metadata and controls
49 lines (47 loc) · 2.35 KB
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
import streamlit as st
import importlib.util
import os
import numpy as np
def handle_custom_function():
st.sidebar.header("Add Your Custom Function")
custom_input_method = st.sidebar.radio("Choose how to add your custom function:", ("Write Code", "Upload File"))
if custom_input_method == "Write Code":
# Allow users to write their custom function code directly
custom_function_code = st.sidebar.text_area(
"Function Code (use 'pos' as the input variable, e.g., 'lambda pos: np.mean(pos, axis=0).reshape(-1,1)')",
"lambda pos: np.mean(pos, axis=0).reshape(-1,1)"
)
try:
custom_function = eval(custom_function_code)
st.sidebar.success("Custom function added successfully!")
return custom_function
except Exception as e:
st.sidebar.error(f"Error in your function code: {e}")
return None
elif custom_input_method == "Upload File":
uploaded_file = st.sidebar.file_uploader("Upload a Python file (.py) with your custom function", type="py")
if uploaded_file is not None:
try:
# Save the uploaded file temporarily
file_path = "temp_uploaded_function.py"
with open(file_path, "wb") as f:
f.write(uploaded_file.getbuffer())
# Dynamically import the module
spec = importlib.util.spec_from_file_location("custom_module", file_path)
custom_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(custom_module)
# Assume the user has defined a function named `custom_function`
if hasattr(custom_module, "custom_function"):
st.sidebar.success("Custom function loaded successfully from file!")
return getattr(custom_module, "custom_function")
else:
st.sidebar.error("The uploaded file does not contain a function named 'custom_function'.")
return None
except Exception as e:
st.sidebar.error(f"Error loading custom function from file: {e}")
return None
finally:
# Clean up the temporary file
if os.path.exists(file_path):
os.remove(file_path)
return None