-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
37 lines (26 loc) · 1014 Bytes
/
app.py
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
from flask import Flask, request, render_template
from io import StringIO
import sys
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
user_code = request.form['user_code']
result = 0
try:
#capture the output
original_stdout = sys.stdout
sys.stdout = StringIO()
#execute arbitrary code
exec(user_code)
#retrieve output
output = sys.stdout.getvalue()
#reset stdout
sys.stdout = original_stdout
result = output.strip()
except Exception as e:
result = f"Error: {str(e)}"
return render_template('index.html', result=result, user_code=user_code)
return render_template('index.html', result=None, user_code="# Write your Python code here and execute it.\nprint('Try me!')")
if __name__ == '__main__':
app.run(debug=True)