MLDeveloper commited on
Commit
51a2adc
·
verified ·
1 Parent(s): 6609537

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -17
app.py CHANGED
@@ -3,14 +3,28 @@ 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
@@ -21,14 +35,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:
@@ -39,19 +54,6 @@ if st.button("Run Code"):
39
 
40
 
41
 
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
  # V1 without gemini api
56
 
57
  # import streamlit as st
 
3
  import sys
4
  import io
5
 
6
+ def execute_code(code, user_input):
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
+ # 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
19
+ if input_counter < len(input_values):
20
+ value = input_values[input_counter]
21
+ input_counter += 1
22
+ return value
23
+ else:
24
+ raise ValueError("Not enough inputs provided.")
25
+
26
  try:
27
+ exec(code, {"input": mock_input}) # Run the user code with mocked input
28
  output = redirected_output.getvalue() # Get the output from buffer
29
  except Exception as e:
30
  output = f"Error: {str(e)}" # Capture and display any errors
 
35
 
36
  # Streamlit UI
37
  st.title("💻 Code Runner")
38
+ st.write("Write your Python code and provide input values if required.")
39
 
40
  code_input = st.text_area("Enter your Python code:", height=200)
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
+ output = execute_code(code_input, user_input) # Execute user code
47
  st.subheader("Output:")
48
  st.code(output, language="plaintext")
49
  else:
 
54
 
55
 
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  # V1 without gemini api
58
 
59
  # import streamlit as st