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__':