Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,27 @@
|
|
1 |
-
|
2 |
import streamlit as st
|
3 |
import sys
|
4 |
import io
|
5 |
|
6 |
-
def execute_code(code):
|
7 |
-
"""Execute the given code and return the output
|
8 |
old_stdout = sys.stdout # Backup original stdout
|
9 |
redirected_output = io.StringIO() # Create a new string buffer
|
10 |
sys.stdout = redirected_output # Redirect stdout to buffer
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
try:
|
13 |
-
exec(code, {}) # Execute the user's code
|
14 |
output = redirected_output.getvalue() # Get the output from buffer
|
15 |
except Exception as e:
|
16 |
output = f"Error: {str(e)}" # Capture and display any errors
|
@@ -21,14 +32,15 @@ def execute_code(code):
|
|
21 |
|
22 |
# Streamlit UI
|
23 |
st.title("💻 Code Runner")
|
24 |
-
st.write("Write your code and get the correct output!")
|
25 |
|
26 |
code_input = st.text_area("Enter your Python code:", height=200)
|
|
|
27 |
|
28 |
if st.button("Run Code"):
|
29 |
if code_input.strip():
|
30 |
with st.spinner("Executing..."):
|
31 |
-
output = execute_code(code_input) # Execute user code
|
32 |
st.subheader("Output:")
|
33 |
st.code(output, language="plaintext")
|
34 |
else:
|
@@ -43,13 +55,6 @@ if st.button("Run Code"):
|
|
43 |
|
44 |
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
|
54 |
# V1 without gemini api
|
55 |
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import sys
|
3 |
import io
|
4 |
|
5 |
+
def execute_code(code, user_input=""):
|
6 |
+
"""Execute the given code with simulated input and return the output."""
|
7 |
old_stdout = sys.stdout # Backup original stdout
|
8 |
redirected_output = io.StringIO() # Create a new string buffer
|
9 |
sys.stdout = redirected_output # Redirect stdout to buffer
|
10 |
|
11 |
+
input_values = user_input.strip().split("\n") # Split user inputs by line
|
12 |
+
input_counter = 0
|
13 |
+
|
14 |
+
def mock_input(prompt=""):
|
15 |
+
nonlocal input_counter
|
16 |
+
if input_counter < len(input_values):
|
17 |
+
value = input_values[input_counter]
|
18 |
+
input_counter += 1
|
19 |
+
return value
|
20 |
+
else:
|
21 |
+
raise ValueError("Not enough inputs provided.")
|
22 |
+
|
23 |
try:
|
24 |
+
exec(code, {"input": mock_input}) # Execute the user's code with mock input
|
25 |
output = redirected_output.getvalue() # Get the output from buffer
|
26 |
except Exception as e:
|
27 |
output = f"Error: {str(e)}" # Capture and display any errors
|
|
|
32 |
|
33 |
# Streamlit UI
|
34 |
st.title("💻 Code Runner")
|
35 |
+
st.write("Write your Python code and get the correct output!")
|
36 |
|
37 |
code_input = st.text_area("Enter your Python code:", height=200)
|
38 |
+
user_input = st.text_area("Enter input values (one per line):", height=100) # Added input field
|
39 |
|
40 |
if st.button("Run Code"):
|
41 |
if code_input.strip():
|
42 |
with st.spinner("Executing..."):
|
43 |
+
output = execute_code(code_input, user_input) # Execute user code with mock input
|
44 |
st.subheader("Output:")
|
45 |
st.code(output, language="plaintext")
|
46 |
else:
|
|
|
55 |
|
56 |
|
57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
# V1 without gemini api
|
60 |
|