Problem:
I’m working on a Flask application that allows users to input and execute Python code in real-time using SocketIO. but facing Error: ‘NoneType’ object is not subscriptable this error.
ide.py
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
import sys
from io import StringIO
app = Flask(__name__)
socketio = SocketIO(app)
variables = {}
@app.route("https://stackoverflow.com/")
def index():
return render_template('index.html')
@socketio.on('run_code')
def run_code(data):
code = data['code']
original_stdout = sys.stdout
sys.stdout = StringIO()
try:
lines = code.strip().split('\n')
for line in lines:
exec_globals = {'__builtins__': None}
exec_locals = variables
exec(line, exec_globals, exec_locals)
result = sys.stdout.getvalue().strip()
emit('code_result', {'output': result})
# Reset stdout for the next iteration
sys.stdout = StringIO()
except Exception as e:
error_message = str(e)
emit('code_result', {'error': error_message})
finally:
sys.stdout = original_stdout
if __name__ == '__main__':
socketio.run(app, debug=True)
facing this error one flask webpage
or is there any easy way?