Spaces:
Running
Running
Update pages/2_CodeSimulator.py
Browse files- pages/2_CodeSimulator.py +29 -13
pages/2_CodeSimulator.py
CHANGED
|
@@ -1,12 +1,26 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import torch
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
# Function to execute the input code
|
| 5 |
def execute_code(code):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
global_vars = {}
|
| 7 |
local_vars = {}
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
st.title('PyTorch Code Runner')
|
| 12 |
|
|
@@ -15,13 +29,15 @@ code_input = st.text_area("Enter your PyTorch code here", height=300)
|
|
| 15 |
|
| 16 |
# Button to execute the code
|
| 17 |
if st.button("Run Code"):
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
st.
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import torch
|
| 3 |
+
import io
|
| 4 |
+
import sys
|
| 5 |
|
| 6 |
+
# Function to execute the input code and capture print statements
|
| 7 |
def execute_code(code):
|
| 8 |
+
# Redirect stdout to capture print statements
|
| 9 |
+
old_stdout = sys.stdout
|
| 10 |
+
sys.stdout = mystdout = io.StringIO()
|
| 11 |
+
|
| 12 |
global_vars = {}
|
| 13 |
local_vars = {}
|
| 14 |
+
try:
|
| 15 |
+
exec(code, global_vars, local_vars)
|
| 16 |
+
output = mystdout.getvalue()
|
| 17 |
+
except Exception as e:
|
| 18 |
+
output = str(e)
|
| 19 |
+
finally:
|
| 20 |
+
# Reset redirect.
|
| 21 |
+
sys.stdout = old_stdout
|
| 22 |
+
|
| 23 |
+
return output, local_vars
|
| 24 |
|
| 25 |
st.title('PyTorch Code Runner')
|
| 26 |
|
|
|
|
| 29 |
|
| 30 |
# Button to execute the code
|
| 31 |
if st.button("Run Code"):
|
| 32 |
+
# Execute the code and capture the output
|
| 33 |
+
output, variables = execute_code(code_input)
|
| 34 |
+
|
| 35 |
+
# Display the output
|
| 36 |
+
st.subheader('Output')
|
| 37 |
+
st.text(output)
|
| 38 |
+
|
| 39 |
+
# Display returned variables
|
| 40 |
+
if variables:
|
| 41 |
+
st.subheader('Variables')
|
| 42 |
+
for key, value in variables.items():
|
| 43 |
+
st.text(f"{key}: {value}")
|