Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,100 +1,39 @@
|
|
| 1 |
|
| 2 |
-
|
| 3 |
import streamlit as st
|
| 4 |
-
import subprocess
|
| 5 |
import sys
|
| 6 |
import io
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
if not shutil.which("javac"):
|
| 14 |
-
os.system("apt-get update && apt-get install -y default-jdk g++")
|
| 15 |
-
|
| 16 |
-
# Function to execute Python code
|
| 17 |
-
def execute_python(code, user_input):
|
| 18 |
-
old_stdout = sys.stdout
|
| 19 |
-
redirected_output = io.StringIO()
|
| 20 |
-
sys.stdout = redirected_output
|
| 21 |
-
|
| 22 |
-
input_values = user_input.strip().split("\n")
|
| 23 |
-
|
| 24 |
-
input_counter = 0
|
| 25 |
-
def mock_input(prompt=""):
|
| 26 |
-
nonlocal input_counter
|
| 27 |
-
if input_counter < len(input_values):
|
| 28 |
-
value = input_values[input_counter]
|
| 29 |
-
input_counter += 1
|
| 30 |
-
return value
|
| 31 |
-
else:
|
| 32 |
-
raise ValueError("Not enough inputs provided.")
|
| 33 |
|
| 34 |
try:
|
| 35 |
-
exec(code, {
|
| 36 |
-
output = redirected_output.getvalue()
|
| 37 |
except Exception as e:
|
| 38 |
-
output = f"Error: {str(e)}"
|
| 39 |
finally:
|
| 40 |
-
sys.stdout = old_stdout
|
| 41 |
-
|
| 42 |
-
return output.strip()
|
| 43 |
-
|
| 44 |
-
# Function to execute Java code
|
| 45 |
-
def execute_java(code, user_input):
|
| 46 |
-
if not shutil.which("javac"):
|
| 47 |
-
return "Error: Java JDK is not installed. Please install it using 'apt-get install default-jdk'."
|
| 48 |
-
|
| 49 |
-
with open("Main.java", "w") as file:
|
| 50 |
-
file.write(code)
|
| 51 |
-
|
| 52 |
-
compile_process = subprocess.run(["javac", "Main.java"], capture_output=True, text=True)
|
| 53 |
-
if compile_process.returncode != 0:
|
| 54 |
-
return f"Compilation Error:\n{compile_process.stderr}"
|
| 55 |
-
|
| 56 |
-
run_process = subprocess.run(["java", "Main"], input=user_input.encode(), capture_output=True, text=True)
|
| 57 |
-
return run_process.stdout if run_process.returncode == 0 else f"Runtime Error:\n{run_process.stderr}"
|
| 58 |
|
| 59 |
-
#
|
| 60 |
-
def execute_cpp(code, user_input):
|
| 61 |
-
if not shutil.which("g++"):
|
| 62 |
-
return "Error: C++ Compiler is not installed. Please install it using 'apt-get install g++'."
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
if compile_process.returncode != 0:
|
| 69 |
-
return f"Compilation Error:\n{compile_process.stderr}"
|
| 70 |
-
|
| 71 |
-
run_process = subprocess.run(["./main"], input=user_input.encode(), capture_output=True, text=True)
|
| 72 |
-
return run_process.stdout if run_process.returncode == 0 else f"Runtime Error:\n{run_process.stderr}"
|
| 73 |
-
|
| 74 |
-
# Streamlit UI for Code Execution
|
| 75 |
-
st.title("💻 Multi-Language Code Runner")
|
| 76 |
-
st.write("Write your Python, Java, or C++ code and get the correct output!")
|
| 77 |
-
|
| 78 |
-
languages = ["Python", "Java", "C++"]
|
| 79 |
-
selected_lang = st.selectbox("Select Language:", languages)
|
| 80 |
-
|
| 81 |
-
code_input = st.text_area("Enter your code:", height=200)
|
| 82 |
-
user_input = st.text_area("Enter input values (one per line):", height=100)
|
| 83 |
|
| 84 |
if st.button("Run Code"):
|
| 85 |
if code_input.strip():
|
| 86 |
with st.spinner("Executing..."):
|
| 87 |
-
|
| 88 |
-
output = execute_python(code_input, user_input)
|
| 89 |
-
elif selected_lang == "Java":
|
| 90 |
-
output = execute_java(code_input, user_input)
|
| 91 |
-
elif selected_lang == "C++":
|
| 92 |
-
output = execute_cpp(code_input, user_input)
|
| 93 |
-
|
| 94 |
st.subheader("Output:")
|
| 95 |
st.code(output, language="plaintext")
|
| 96 |
else:
|
| 97 |
-
st.warning("⚠️ Please enter some code before running.")
|
|
|
|
| 98 |
|
| 99 |
|
| 100 |
|
|
|
|
| 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 or error message."""
|
| 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 safely
|
| 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
|
| 17 |
finally:
|
| 18 |
+
sys.stdout = old_stdout # Restore original stdout
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
return output.strip() # Return cleaned output
|
|
|
|
|
|
|
|
|
|
| 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:
|
| 35 |
+
st.warning("⚠️ Please enter some Python code before running.")
|
| 36 |
+
|
| 37 |
|
| 38 |
|
| 39 |
|