File size: 964 Bytes
1b38efb
 
 
 
5add52e
1b38efb
5add52e
1b38efb
 
 
 
 
 
0f94234
6a4b0a8
1b38efb
 
6a4b0a8
1b38efb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c8e371b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from flask import Flask, request, jsonify
import sys
import io
import contextlib

app = Flask(__name__)

# Function to execute Python code safely
def execute_python_code(code):
    # Capturing the output of Python execution
    output = io.StringIO()
    sys.stdout = output
    sys.stderr = output

    try:
        # Executing the code
        exec(code)
    except Exception as e:
        output.write(f"Error: {e}")

    # Return the captured output
    return output.getvalue()

@app.route('/')
def index():
    return app.send_static_file('index.html')

@app.route('/execute', methods=['POST'])
def execute():
    data = request.get_json()
    code = data.get('code', '')
    
    # Execute the Python code and capture the result
    result = execute_python_code(code)
    
    # Send the result back to the frontend
    return jsonify({'result': result})

if __name__ == '__main__':
    # Use 0.0.0.0 to allow external access (necessary for Hugging Face Spa