-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunPython.py
More file actions
55 lines (43 loc) · 1.46 KB
/
RunPython.py
File metadata and controls
55 lines (43 loc) · 1.46 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
50
51
52
53
54
55
import streamlit as st
import sys
import io
import traceback
from contextlib import redirect_stdout, redirect_stderr
def execute_python_code(code):
# Capture stdout and stderr
output = io.StringIO()
error = io.StringIO()
try:
with redirect_stdout(output), redirect_stderr(error):
exec(code)
return output.getvalue(), error.getvalue(), None
except Exception:
return output.getvalue(), error.getvalue(), traceback.format_exc()
st.set_page_config(page_title="Python Code Executor", layout="wide")
st.title("Python Code Executor")
# Default code
default_code = """
# Enter your Python code here
print("Hello, World!")
# Example: Define a function
def greet(name):
return f"Hello, {name}!"
# Call the function
result = greet("Streamlit")
print(result)
"""
# Create a text area for code input
code = st.text_area("Enter Python Code", value=default_code, height=300)
# Create a button to run the code
if st.button("Run Code"):
stdout, stderr, exception = execute_python_code(code)
# Display the output
if stdout:
st.subheader("Output:")
st.code(stdout)
# Display any errors
if stderr or exception:
st.subheader("Errors:")
st.error(stderr + (exception or ""))
st.markdown("---")
st.write("Note: This app executes Python code in a restricted environment. Some operations may be limited for security reasons.")