Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,18 @@
|
|
| 1 |
#
|
| 2 |
import streamlit as st
|
|
|
|
| 3 |
import sys
|
| 4 |
import io
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
old_stdout = sys.stdout
|
| 9 |
-
redirected_output = io.StringIO()
|
| 10 |
-
sys.stdout = redirected_output
|
| 11 |
|
| 12 |
-
# Convert user input into list format
|
| 13 |
input_values = user_input.strip().split("\n")
|
| 14 |
|
| 15 |
-
# Replace input() calls with user-provided input values
|
| 16 |
input_counter = 0
|
| 17 |
def mock_input(prompt=""):
|
| 18 |
nonlocal input_counter
|
|
@@ -24,34 +24,63 @@ def execute_code(code, user_input):
|
|
| 24 |
raise ValueError("Not enough inputs provided.")
|
| 25 |
|
| 26 |
try:
|
| 27 |
-
exec(code, {"input": mock_input})
|
| 28 |
-
output = redirected_output.getvalue()
|
| 29 |
except Exception as e:
|
| 30 |
-
output = f"Error: {str(e)}"
|
| 31 |
finally:
|
| 32 |
-
sys.stdout = old_stdout
|
| 33 |
|
| 34 |
-
return output.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
# Streamlit UI
|
| 37 |
-
st.title("💻 Code Runner")
|
| 38 |
-
st.write("Write your Python code and
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
| 41 |
user_input = st.text_area("Enter input values (one per line):", height=100)
|
| 42 |
|
| 43 |
if st.button("Run Code"):
|
| 44 |
if code_input.strip():
|
| 45 |
with st.spinner("Executing..."):
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
st.subheader("Output:")
|
| 48 |
st.code(output, language="plaintext")
|
| 49 |
else:
|
| 50 |
-
st.warning("⚠️ Please enter some
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
|
| 56 |
|
| 57 |
# V1 without gemini api
|
|
|
|
| 1 |
#
|
| 2 |
import streamlit as st
|
| 3 |
+
import subprocess
|
| 4 |
import sys
|
| 5 |
import io
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
+
# Function to execute Python code
|
| 9 |
+
def execute_python(code, user_input):
|
| 10 |
+
old_stdout = sys.stdout
|
| 11 |
+
redirected_output = io.StringIO()
|
| 12 |
+
sys.stdout = redirected_output
|
| 13 |
|
|
|
|
| 14 |
input_values = user_input.strip().split("\n")
|
| 15 |
|
|
|
|
| 16 |
input_counter = 0
|
| 17 |
def mock_input(prompt=""):
|
| 18 |
nonlocal input_counter
|
|
|
|
| 24 |
raise ValueError("Not enough inputs provided.")
|
| 25 |
|
| 26 |
try:
|
| 27 |
+
exec(code, {"input": mock_input})
|
| 28 |
+
output = redirected_output.getvalue()
|
| 29 |
except Exception as e:
|
| 30 |
+
output = f"Error: {str(e)}"
|
| 31 |
finally:
|
| 32 |
+
sys.stdout = old_stdout
|
| 33 |
|
| 34 |
+
return output.strip()
|
| 35 |
+
|
| 36 |
+
# Function to execute Java code
|
| 37 |
+
def execute_java(code, user_input):
|
| 38 |
+
with open("Main.java", "w") as file:
|
| 39 |
+
file.write(code)
|
| 40 |
+
|
| 41 |
+
compile_process = subprocess.run(["javac", "Main.java"], capture_output=True, text=True)
|
| 42 |
+
if compile_process.returncode != 0:
|
| 43 |
+
return f"Compilation Error:\n{compile_process.stderr}"
|
| 44 |
+
|
| 45 |
+
run_process = subprocess.run(["java", "Main"], input=user_input, capture_output=True, text=True)
|
| 46 |
+
return run_process.stdout if run_process.returncode == 0 else f"Runtime Error:\n{run_process.stderr}"
|
| 47 |
+
|
| 48 |
+
# Function to execute C++ code
|
| 49 |
+
def execute_cpp(code, user_input):
|
| 50 |
+
with open("main.cpp", "w") as file:
|
| 51 |
+
file.write(code)
|
| 52 |
+
|
| 53 |
+
compile_process = subprocess.run(["g++", "main.cpp", "-o", "main"], capture_output=True, text=True)
|
| 54 |
+
if compile_process.returncode != 0:
|
| 55 |
+
return f"Compilation Error:\n{compile_process.stderr}"
|
| 56 |
+
|
| 57 |
+
run_process = subprocess.run(["./main"], input=user_input, capture_output=True, text=True)
|
| 58 |
+
return run_process.stdout if run_process.returncode == 0 else f"Runtime Error:\n{run_process.stderr}"
|
| 59 |
|
| 60 |
# Streamlit UI
|
| 61 |
+
st.title("💻 Multi-Language Code Runner")
|
| 62 |
+
st.write("Write your Python, Java, or C++ code and get the correct output!")
|
| 63 |
|
| 64 |
+
languages = ["Python", "Java", "C++"]
|
| 65 |
+
selected_lang = st.selectbox("Select Language:", languages)
|
| 66 |
+
|
| 67 |
+
code_input = st.text_area("Enter your code:", height=200)
|
| 68 |
user_input = st.text_area("Enter input values (one per line):", height=100)
|
| 69 |
|
| 70 |
if st.button("Run Code"):
|
| 71 |
if code_input.strip():
|
| 72 |
with st.spinner("Executing..."):
|
| 73 |
+
if selected_lang == "Python":
|
| 74 |
+
output = execute_python(code_input, user_input)
|
| 75 |
+
elif selected_lang == "Java":
|
| 76 |
+
output = execute_java(code_input, user_input)
|
| 77 |
+
elif selected_lang == "C++":
|
| 78 |
+
output = execute_cpp(code_input, user_input)
|
| 79 |
+
|
| 80 |
st.subheader("Output:")
|
| 81 |
st.code(output, language="plaintext")
|
| 82 |
else:
|
| 83 |
+
st.warning("⚠️ Please enter some code before running.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
|
| 86 |
# V1 without gemini api
|