-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathscript_runner.py
45 lines (39 loc) · 1.54 KB
/
script_runner.py
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
"""
Streamlit script runner for executing Python scripts and capturing their output and errors.
"""
import subprocess
import streamlit as st
def run_script(script_name: str) -> tuple[str, str]:
"""
Execute a script and capture its output and errors.
Args:
script_name (str): The name of the script to execute.
Returns:
tuple: A tuple containing the script output and errors.
"""
st.write(f"Running {script_name}...")
try:
process = subprocess.run(
["python", script_name],
capture_output=True, text=True, timeout=60
)
output = process.stdout.strip() if process.stdout else "No output"
errors = process.stderr.strip() if process.stderr else "No errors"
return output, errors
except subprocess.CalledProcessError as e:
return (f"Script {script_name} execution failed.\n\n"
f"Status: {e.returncode}, Output: {e.stdout}, Errors: {e.stderr}"), "Errors"
except subprocess.TimeoutExpired as e:
return f"Script {script_name} execution timed out.\n\nErrors: {e.stderr}", "Errors"
except Exception as e:
return f"An unexpected error occurred while running {script_name}: {e}", "Errors"
def main():
"""
Main function to run the specified script and display its output and errors.
"""
script_name = "script_runner.py" # Replace with your script name
output, errors = run_script(script_name)
st.code(f"```\n{output}\n```")
st.write(f"Errors:\n{errors}")
if __name__ == "__main__":
main()